node-telegram-api/demo.js

48 lines
1.1 KiB
JavaScript
Raw Normal View History

2015-06-28 22:42:48 +00:00
var Bot = require('telegram-api');
// only require the message types you need, more coming soon!
var Message = require('telegram-api/types/Message');
var Question = require('telegram-api/types/Question');
2015-06-26 23:56:28 +00:00
var bot = new Bot({
2015-06-29 22:20:34 +00:00
token: '121143906:AAE6pcpBoARNZZjr3fUpvKuLInJ5Eee5Ajk'
2015-06-26 23:56:28 +00:00
});
bot.start().catch(err => {
console.error(err, '\n', err.stack);
2015-06-26 23:56:28 +00:00
});
// polling
bot.on('update', update => {
console.log('Polled\n', update);
});
const question = new Question({
text: 'How should I greet you?',
answers: [['Hey'], ['Hello, Sir'], ['Yo bro']]
});
2015-06-28 23:52:22 +00:00
bot.get(/Hi\sBot/, message => {
2015-06-26 23:56:28 +00:00
const id = message.chat.id;
2015-06-28 22:42:48 +00:00
question.to(id).reply(message.message_id);
2015-06-26 23:56:28 +00:00
bot.send(question).then(answer => {
const msg = new Message().to(id).text('Your answer: ' + answer.text);
bot.send(msg);
2015-06-26 23:56:28 +00:00
}, () => {
2015-06-28 01:04:18 +00:00
const msg = new Message().to(id).text('Invalid answer');
bot.send(msg);
2015-06-26 23:56:28 +00:00
});
});
const hello = new Message().text('Hello');
bot.command('start', message => {
bot.send(hello.to(message.chat.id));
2015-06-26 23:56:28 +00:00
});
const test = new Message().text('Test Command');
bot.command('test', message => {
bot.send(test.to(message.chat.id));
2015-06-26 23:56:28 +00:00
});