node-telegram-api/demo.js

52 lines
1.2 KiB
JavaScript
Raw Permalink Normal View History

var Bot = require('telegram-api').default;
2015-06-28 22:42:48 +00:00
// 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-07-04 15:53:37 +00:00
token: '114687409:AAEVpfOmGI7xNKiKzVA9LBcOO9INpIwnvDI'
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');
2015-06-30 22:04:44 +00:00
2015-07-05 13:36:27 +00:00
bot.command('test <subject> [count|number] ...rest', message => {
bot.send(test.to(message.chat.id).text(message.args.subject));
2015-06-26 23:56:28 +00:00
});
// to stop a bot
// bot.stop();