node-telegram-api/README.md

91 lines
2.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
2015-06-28 22:42:48 +00:00
telegram-api is in beta, your feedback is appreciated, please [fill an issue](https://github.com/mdibaiee/node-telegram-api/issues)
2015-06-27 00:33:47 +00:00
for any bugs you find or any suggestions you have.
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).
2015-06-28 22:42:48 +00:00
[@JavaScriptBot](https://telegram.me/JavaScriptBot) runs on `demo.js`, you can test it.
2015-06-26 23:56:28 +00:00
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
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');
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);
});
2015-06-28 22:42:48 +00:00
// Create a new question
// answers is a keyboard layout as defined in Telegram API
// we're going to reuse this by modifying it's target
const question = new Question()
.text('How should I greet you?')
.answers([['Hey'], ['Hello, Sir'], ['Yo bro']]);
// Called when a message starting with Hi is received
// You can use Regular Expressions, too
// update is an Update object as defined in Telegram API
smartBot.get('Hi', update => {
const message = update.message;
2015-06-26 17:20:54 +00:00
2015-06-28 22:42:48 +00:00
question.to(message.chat.id).reply(message.message_id);
2015-06-26 17:20:54 +00:00
2015-06-28 22:42:48 +00:00
// Send the question, returns a promise, resolves on valid answer,
// rejects in case of an invalid answer
smartBot.send(question).then(answer => {
const msg = new Message().to(id).text('Your answer: ' + answer);
smartBot.send(msg);
2015-06-26 17:20:54 +00:00
}, () => {
2015-06-28 22:42:48 +00:00
const msg = new Message().to(id).text('Invalid answer');
smartBot.send(msg);
2015-06-26 17:20:54 +00:00
});
});
2015-06-28 22:42:48 +00:00
// Commands are in the format `/command` or `/command@botusername` in groups
const test = new Message().text('Test Command');
2015-06-26 17:20:54 +00:00
smartBot.command('test', update => {
const message = update.message;
const id = message.chat.id;
2015-06-28 22:42:48 +00:00
smartBot.send(test.to(id));
2015-06-26 17:20:54 +00:00
});
2015-06-28 22:42:48 +00:00
const hello = new Message().text('Hello');
2015-06-26 17:20:54 +00:00
smartBot.command('start', update => {
2015-06-28 22:42:48 +00:00
smartBot.send(hello.to(update.message.chat.id));
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)
2015-06-28 22:42:48 +00:00
# Bots using this module
[@JavaScriptBot](https://telegram.me/JavaScriptBot)
# Todo
- [] BulkMessage Type
- [] File Type
- [] Sticker Type
- [] Location Type
- [] Contact Type
- [] Allow remote control of bots (TCP maybe)
- YOUR IDEAS! [Fill an issue](https://github.com/mdibaiee/node-telegram-api/issues)