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
|
|
|
|
2015-06-28 23:49:09 +00:00
|
|
|
var bot = new Bot({
|
2015-07-02 21:51:34 +00:00
|
|
|
token: 'YOUR_KEY'
|
2015-06-26 23:56:28 +00:00
|
|
|
});
|
|
|
|
|
2015-06-29 01:00:29 +00:00
|
|
|
bot.start().catch(err => {
|
|
|
|
console.error(err, '\n', err.stack);
|
2015-06-26 23:56:28 +00:00
|
|
|
});
|
|
|
|
|
2015-06-28 23:49:09 +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
|
|
|
|
2015-06-28 23:49:09 +00:00
|
|
|
bot.send(question).then(answer => {
|
2015-06-29 01:00:29 +00:00
|
|
|
const msg = new Message().to(id).text('Your answer: ' + answer.text);
|
2015-06-28 23:49:09 +00:00
|
|
|
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');
|
2015-06-28 23:49:09 +00:00
|
|
|
bot.send(msg);
|
2015-06-26 23:56:28 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2015-06-28 23:49:09 +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
|
|
|
});
|
|
|
|
|
2015-06-28 23:49:09 +00:00
|
|
|
const test = new Message().text('Test Command');
|
2015-06-30 22:04:44 +00:00
|
|
|
|
2015-06-28 23:49:09 +00:00
|
|
|
bot.command('test', message => {
|
|
|
|
bot.send(test.to(message.chat.id));
|
2015-06-26 23:56:28 +00:00
|
|
|
});
|