benjick / meteor-telegram-bot

Telegram bot plugin for Meteor
24 stars 15 forks source link

Conversations Support #18

Closed shrmnk closed 8 years ago

shrmnk commented 8 years ago

Added basic conversations support

Also resolves #13

Snippet from Readme:

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 3 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);
        switch(obj.stage) {
            case 0:
                obj.personName = message;
                TelegramBot.send('Cool. What\'s your height?', chat_id);
                obj.stage++;
                break;

            case 1:
                obj.personHeight = message;
                // Sample markdown support - name will be bold
                TelegramBot.send('Nice. What do you work as, *' + obj.personName + '*?', chat_id, true);
                obj.stage++;
                break;

            case 2:
                obj.personJob = message;
                TelegramBot.send('Nice to meet you, ' + obj.personHeight + '-tall ' + obj.personJob + ' ' + obj.personName + '!', chat_id);
                break;
        }
        console.log('Conversation Status: ' + obj);
    }, {stage: 0, personName: "", personHeight: "", personJob: ""} );
    // The return in this listener will be the first prompt
    return "Hey, what's your name?";
});