dervondenbergen / giphy-gitter

get the perfect gif in your gitter activity feed
15 stars 4 forks source link

multiple channels / chats #2

Open dervondenbergen opened 9 years ago

dervondenbergen commented 9 years ago

Pass an array of room names to gitter.room in the config instead of a string.

Code has to be changed to listen to all channels and put the right gif in the right channel.

This would enable the possibility to set up only one giphy-gitter instance for multiple channels. Right now there has to be a new an instance for each channel.

adrienZ commented 7 years ago

index.js


// ...

config.gitter.rooms.forEach( ( currentRoom, roomIndex) => {
  // gitter room name from config gets joined, to receive the room id on start
  gitter.rooms.join( currentRoom.name , function(err, room) {

    if (err) {
      console.log('Not possible to join the room: ', err);
      return;
    }

    currentRoom.id = room.id;

    // start the message listener
    listenToMessages(currentRoom);

  })
})

function listenToMessages (currentRoom) {

  gitter.rooms.find(currentRoom.id).then(function(room) {

    var events = room.streaming().chatMessages();

    // The 'snapshot' event is emitted once, with the last messages in the room 
    events.on('snapshot', function(snapshot) {
      console.log(snapshot.length + ' messages in the snapshot');
    });

    // event gets called, when a new message gets written in the configured channel
    events.on('chatMessages', function(message) {

      // the bot only evaluates new messages, no updates or other changes
      if (message.operation === 'create') {

        var data = message.model;
        var text = data.text;

        // text contains the giphy command
        if (text.match(config.giphy.regex)) {

          var user = data.fromUser.username;
          var search = text.replace(config.giphy.regex, '');

          // replace underscores and colons to spaces because emojis
          search = search.replace(/_|:/g, ' ').trim();

          // if there is search text, search after it
          if (search) {

            giphy.search({q: search, limit: 20}, function (err, result, res) {

              // if there are results, send a random one
              if (result.data.length) {

                var image = result.data[parseInt(Math.random()*result.data.length)];

                var imgurl = image.images.original.url;

                var feedcontent =  '@' + user + ' : __'+ search +'__ \n\n[!['+search+'](' + imgurl + ')](' + image.url + ')';

                send(feedcontent, room);

              } else {

                // otherwise send a message, that there are no gifs with that search
                var help = '@' + user + ': there is no gif with those words';

                send(help, room);

              }

            });

          } else {

            // otherwise send an explanation to user
            var help = '@' + user + ': use `/giphy` with a word, to get a gif related to that word, eg. `/giphy cats hats`';

            send(help, room);

          }

        }

      }

    });
  });

}

// ...

config.example.js


// ...

    rooms: [
      // name of the gitter rooms, you want to retrieve and send the messages
      { name: 'bullgit/bombing'},
      { name: 'bullgit/random'},
    ],

// ...