• Recent
  • Tags
  • Unsolved
  • Solved
  • MagicMirror² Repository
  • Documentation
  • 3rd-Party-Modules
  • Donate
  • Discord
  • Register
  • Login
MagicMirror Forum
  • Recent
  • Tags
  • Unsolved
  • Solved
  • MagicMirror² Repository
  • Documentation
  • 3rd-Party-Modules
  • Donate
  • Discord
  • Register
  • Login
A New Chapter for MagicMirror: The Community Takes the Lead
Read the statement by Michael Teeuw here.

MMM-Slack stop randomise messages

Scheduled Pinned Locked Moved Troubleshooting
14 Posts 4 Posters 7.1k Views 4 Watching
Loading More Posts
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    saibam
    last edited by Jan 23, 2018, 8:59 AM

    I am trying to get rid of the randomiser in MMM-Slack module. I want the messages to show in order from oldest to newest. I’m sure I’m approaching it the wrong way but I just can’t get it to work without the randomiser. Pls help!

    S M 2 Replies Last reply Jan 23, 2018, 12:00 PM Reply Quote 0
    • S Offline
      saibam @saibam
      last edited by saibam Jan 23, 2018, 12:01 PM Jan 23, 2018, 12:00 PM

      if(this.slackMessages.length > 0) {
           var randomMessageId = Math.floor(Math.random() * this.slackMessages.length);
           var randomMessage = this.slackMessages[randomMessageId].message;
      

      I want just ‘x’ instead of the ‘randomMessageId’ on the last line and can’t figure out how to do it.

      N 1 Reply Last reply Jan 23, 2018, 2:49 PM Reply Quote 0
      • N Offline
        ninjabreadman @saibam
        last edited by Jan 23, 2018, 2:49 PM

        Hi @saibam,

        Just change:

        var randomMessageId = Math.floor(Math.random() * this.slackMessages.length);
        

        to the following:

        var randomMessageId = 0;
        

        It should work (unrandomizing the order), so long as the slackMessages array is in ascending order. If not, and it’s in descending order, set to this.slackMessages.length - 1 instead of 0.

        Problem with config or JavaScript? Copy/paste it into JSHint.
        Check out the detailed walkthroughs on install, config, modules, etc.

        S 1 Reply Last reply Jan 24, 2018, 9:06 AM Reply Quote 0
        • S Offline
          saibam @ninjabreadman
          last edited by saibam Jan 24, 2018, 9:10 AM Jan 24, 2018, 9:06 AM

          @ninjabreadman thanks for the answer but this will only show the latest message. I solved it in a rather long but still nice way. The idea is to show the latest 10 messages and when a new message is sent it will show it instantly.
          currentMessageCount and currentMessgeId are global variables instantiated to 0. emoji_replace is my own function to include emojis in the message.

          if (currentMessageCount < this.slackMessages.length || currentMessageCount == 0){ 
          	currentMessageId = 0;
          	currentMessageCount = this.slackMessages.length;
          }
          var maxAllowed = 10;
          		
          var topMessages = this.slackMessages.slice(0, (maxAllowed - 1));
          		
          var maxCount = Math.min(maxAllowed, topMessages.length);
          		
          if (currentMessageId == maxCount - 1) { currentMessageId = 0; }
          		
          var currentMessage = topMessages[currentMessageId].message;
          currentMessage = emoji_replace(currentMessage);
          messageElement.innerHTML = currentMessage;
          currentMessageId ++;
          
          G 1 Reply Last reply Jan 28, 2018, 5:18 PM Reply Quote 0
          • G Offline
            gekberlin @saibam
            last edited by Jan 28, 2018, 5:18 PM

            @saibam That sounds pretty good. I wanted to achive the same for the last 5 messages. So i would simpley exchange the 10 for a 5. But where do I have to put the code into ? In the mmm-slack.js ?
            I would relay appreciate any help.

            S 1 Reply Last reply Jan 29, 2018, 9:01 AM Reply Quote 0
            • S Offline
              saibam @gekberlin
              last edited by Jan 29, 2018, 9:01 AM

              @gekberlin Yes. Change maxAllowed to 5. Put it in MMM-Slack.js under the getDom: function() where it creates the html elements. I also noticed that it didnt work as expected where I had (maxCount -1) and (maxAllowed - 1). I removed the -1 and it worked like a charm.

              G 1 Reply Last reply Jan 29, 2018, 6:17 PM Reply Quote 1
              • G Offline
                gekberlin @saibam
                last edited by Jan 29, 2018, 6:17 PM

                @saibam Thanks for your fast reply!
                I’ve put in the code but then I didn’t get any message displayed anymore.
                Did I have done something wrong ?

                	getDom: function() {
                		var messageElement = document.createElement('div');
                		if (currentMessageCount < this.slackMessages.length || currentMessageCount == 0){
                	currentMessageId = 0;
                	currentMessageCount = this.slackMessages.length;
                }
                var maxAllowed = 5;
                var topMessages = this.slackMessages.slice(0, (maxAllowed 0));
                var maxCount = Math.min(maxAllowed, topMessages.length);
                if (currentMessageId == maxCount 0) { currentMessageId = 0; }
                var currentMessage = topMessages[currentMessageId].message;
                currentMessage = emoji_replace(currentMessage);
                messageElement.innerHTML = currentMessage;
                currentMessageId ++;
                		}
                		return messageElement;
                	}
                });
                
                
                S 1 Reply Last reply Jan 30, 2018, 9:11 AM Reply Quote 0
                • S Offline
                  saibam @gekberlin
                  last edited by saibam Jan 30, 2018, 9:14 AM Jan 30, 2018, 9:11 AM

                  @gekberlin Yes.

                  currentMessage = emoji_replace(currentMessage);
                  

                  will not work as emoji_replace is my own function for replacing :smiley: with an emoji.
                  so just get rid of that line and it will work.

                  And you will also have to instantiate currentMessageId and currentMessageCount globally. I.e.

                  var currentMessageId = 0
                  var currentMessageCount = 0
                  

                  at the top of the js file.

                  1 Reply Last reply Reply Quote 0
                  • M Offline
                    mediathreat @saibam
                    last edited by Mar 23, 2018, 12:23 AM

                    @saibam Which Slack token do you use to connect to slack and do you have to put the leading # hash sign in the config file for the channel where it listens?

                    S 2 Replies Last reply Mar 27, 2018, 10:15 AM Reply Quote 0
                    • S Offline
                      saibam @mediathreat
                      last edited by Mar 27, 2018, 10:15 AM

                      @mediathreat we followed the link in the readme https://api.slack.com/tokens and from there we created our own app. I’m sorry I can’t find where I found the token in there, you will have to try and error your way :) I will keep looking some but can’t guarantee I find anything. Slack API pages are a mess imo. And no you don’t need the leading # in the config file.

                      1 Reply Last reply Reply Quote 0
                      • 1
                      • 2
                      • 1 / 2
                      • First post
                        Last post
                      Enjoying MagicMirror? Please consider a donation!
                      MagicMirror created by Michael Teeuw.
                      Forum managed by Sam, technical setup by Karsten.
                      This forum is using NodeBB as its core | Contributors
                      Contact | Privacy Policy