node-telegram-api/demo.js

55 lines
1.6 KiB
JavaScript
Raw Normal View History

2015-06-26 23:56:28 +00:00
var Bot = require('./index');
2015-06-28 01:04:18 +00:00
var Message = require('./lib/classes/Message');
var Question = require('./lib/classes/Question');
process.on('uncaughtException', function (err) {
console.error((new Date()).toUTCString() + ' uncaughtException:', err.message);
console.error(err.stack);
process.exit(1);
});
2015-06-26 23:56:28 +00:00
var smartBot = new Bot({
2015-06-28 01:04:18 +00:00
token: '121143906:AAE6pcpBoARNZZjr3fUpvKuLInJ5Eee5Ajk'
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;
2015-06-28 01:04:18 +00:00
var question = new Question().to(id)
.text('How should I greet you?')
.answers([['Hi'], ['Hello, Sir'], ['Yo bro']])
.reply(message.message_id);
2015-06-26 23:56:28 +00:00
2015-06-28 01:04:18 +00:00
smartBot.send(question).then(answer => {
const msg = new Message().to(id).text('Your answer: ' + answer);
smartBot.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');
smartBot.send(msg);
2015-06-26 23:56:28 +00:00
});
});
// Commands are in format `/command` or `/command@botusername` in groups
smartBot.command('test', update => {
const message = update.message;
const id = message.chat.id;
2015-06-28 01:04:18 +00:00
// options object => Telegram API
smartBot.send(new Message({
chat_id: id,
text: 'Test Command'
}));
2015-06-26 23:56:28 +00:00
});
smartBot.command('start', update => {
2015-06-28 01:04:18 +00:00
// chainable methods => easier
smartBot.send(new Message().to(update.message.chat.id).text('Hello'));
2015-06-26 23:56:28 +00:00
});