node-telegram-api/demo.js

41 lines
1.0 KiB
JavaScript
Raw Normal View History

2015-06-26 23:56:28 +00:00
var Bot = require('./index');
var smartBot = new Bot({
2015-06-27 00:35:49 +00:00
token: 'YOUR_KEY'
2015-06-26 23:56:28 +00:00
});
// getMe is called before polling starts, setting info property of bot
smartBot.start().then(() => {
console.log(smartBot.info);
});
// You can use regular expressions, too
smartBot.get('Hi', function(update) {
const message = update.message;
const id = message.chat.id;
// answers is in format of keyboard rows
const question = 'How should I greet you?',
answers = [['Hi'], ['Hello, Sir'], ['Yo bro']];
2015-06-27 00:56:07 +00:00
smartBot.replyTo(message.message_id)
.askQuestion(id, question, answers)
2015-06-26 23:56:28 +00:00
.then(answer => {
smartBot.message(id, 'Your answer: ' + answer);
}, () => {
smartBot.message(id, 'Invalid answer');
});
});
// Commands are in format `/command` or `/command@botusername` in groups
smartBot.command('test', update => {
const message = update.message;
const id = message.chat.id;
smartBot.message(id, 'Test command');
});
smartBot.command('start', update => {
smartBot.message(update.message.chat.id, 'Hello!');
});