node-telegram-api/README.md

61 lines
1.6 KiB
Markdown
Raw Normal View History

2015-06-26 17:20:54 +00:00
# Telegram Bots
Create and control [Telegram bots](https://core.telegram.org/bots) easily
using the new [Telegram API](https://core.telegram.org/bots/api).
2015-06-26 17:20:54 +00:00
```
npm install telegram-api
2015-06-26 17:20:54 +00:00
```
# Example
2015-06-26 23:56:28 +00:00
Take a look at [demo.js](https://github.com/mdibaiee/node-telegram-api/blob/master/demo.js).
Also [@JavaScriptBot](https://telegram.me/JavaScriptBot), still work in progress.
2015-06-26 17:20:54 +00:00
```javascript
2015-06-26 22:36:59 +00:00
var Bot = require('telegram-api');
2015-06-26 17:20:54 +00:00
var smartBot = new Bot({
token: 'YOUR_KEY'
2015-06-26 17:20:54 +00:00
});
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-26 17:20:54 +00:00
const question = 'How should I greet you?',
answers = ['Hi', 'Hello, Sir', 'Yo bro'];
2015-06-26 22:36:59 +00:00
smartBot.replyTo(message.message_id).askQuestion(id, question, answers)
2015-06-26 17:20:54 +00:00
.then(answer => {
smartBot.message(id, 'Your answer: ' + answer);
2015-06-26 17:20:54 +00:00
}, () => {
smartBot.message(id, 'Invalid answer');
});
});
// Commands are in format `/command` or `/command@botusername` in groups
2015-06-26 17:20:54 +00:00
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!');
});
// You can access all API methods through the api property until we implement
// easier methods
smartBot.api.getUserProfilePhotos
2015-06-26 17:20:54 +00:00
```
This will result in:
2015-06-26 23:56:28 +00:00
![@JavaScriptBot](https://github.com/mdibaiee/node-telegram-api/raw/master/demo.gif)