benjick / meteor-telegram-bot

Telegram bot plugin for Meteor
24 stars 15 forks source link

Conversations Support #16

Closed shrmnk closed 8 years ago

shrmnk commented 8 years ago

Added basic conversations support

Conversations

You can monitor all incoming chat messages from a user using conversations.

Below is a basic example showing how a Bot can ask for 2 responses and print them out. The conversation is initiated by the user using /start.

TelegramBot.addListener('/start', function(command, username, messageraw) {
    TelegramBot.startConversation(username, messageraw.chat.id, function(username, message, chat_id) {
        var obj = _.find(TelegramBot.conversations[chat_id], obj => obj.username == username);
        console.log('Conversation Status: ' + obj);
        switch(obj.stage) {
            case 0:
                TelegramBot.send('You have first responded with: ' + message + '\nWhat else do you want to say?', chat_id);
                obj.first_message = message;
                obj.stage++;
                break;

            case 1:
                TelegramBot.send('You then said: ' + message + '\nReply to repeat what you said', chat_id);
                obj.second_message = message;
                break;

            case 2:
                TelegramBot.send("1. " + obj.first_message + "\n2. " + obj.second_message);
                TelegramBot.endConversation(username, chat_id);
                break;
        }
    }, {stage: 0, first_message: "", second_message: ""} );
});
shrmnk commented 8 years ago

Created branch conversations with above commits