2015-06-26 17:20:54 +00:00
|
|
|
# Telegram Bots
|
2015-06-26 22:33:30 +00:00
|
|
|
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
|
|
|
```
|
2015-06-26 22:33:30 +00:00
|
|
|
npm install telegram-api
|
2015-06-26 17:20:54 +00:00
|
|
|
```
|
|
|
|
|
2015-06-30 07:47:14 +00:00
|
|
|
If you are cloning this repository, remember to run `npm install` to install dependencies.
|
|
|
|
|
2015-06-28 22:54:41 +00:00
|
|
|
The code is well documented. I'm trying to integrate JSDoc / ESDoc into our repository for an easy to access documentation.
|
|
|
|
|
2015-06-30 07:47:14 +00:00
|
|
|
---
|
|
|
|
|
2015-06-29 23:54:32 +00:00
|
|
|
All Telegram API methods are accessible through `api` property of bots.
|
|
|
|
|
|
|
|
```javascript
|
|
|
|
bot.api.sendPhoto({
|
|
|
|
chat_id: 99999,
|
|
|
|
photo: 'some_id',
|
|
|
|
caption: 'dancing monkey'
|
|
|
|
});
|
|
|
|
```
|
|
|
|
|
|
|
|
Some API methods have been simplified using what we call *types*. See the example below.
|
|
|
|
|
2015-06-26 17:20:54 +00:00
|
|
|
# Example
|
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');
|
|
|
|
|
2015-06-28 23:49:09 +00:00
|
|
|
var bot = new Bot({
|
2015-06-26 22:33:30 +00:00
|
|
|
token: 'YOUR_KEY'
|
2015-06-26 17:20:54 +00:00
|
|
|
});
|
|
|
|
|
2015-06-29 01:01:41 +00:00
|
|
|
bot.start().catch(err => {
|
|
|
|
console.error(err, '\n', err.stack);
|
2015-06-28 23:49:09 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
// polling
|
|
|
|
bot.on('update', update => {
|
|
|
|
console.log('Polled\n', update);
|
2015-06-26 23:56:28 +00:00
|
|
|
});
|
2015-06-26 22:33:30 +00:00
|
|
|
|
2015-06-28 23:49:09 +00:00
|
|
|
const question = new Question({
|
|
|
|
text: 'How should I greet you?',
|
|
|
|
answers: [['Hey'], ['Hello, Sir'], ['Yo bro']]
|
|
|
|
});
|
2015-06-28 22:42:48 +00:00
|
|
|
|
2015-06-28 23:52:22 +00:00
|
|
|
bot.get(/Hi\sBot/, message => {
|
2015-06-28 23:49:09 +00:00
|
|
|
const id = message.chat.id;
|
2015-06-26 17:20:54 +00:00
|
|
|
|
2015-06-28 23:49:09 +00:00
|
|
|
question.to(id).reply(message.message_id);
|
2015-06-26 17:20:54 +00:00
|
|
|
|
2015-06-28 23:49:09 +00:00
|
|
|
bot.send(question).then(answer => {
|
2015-06-29 01:01:41 +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 17:20:54 +00:00
|
|
|
}, () => {
|
2015-06-28 22:42:48 +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 17:20:54 +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 17:20:54 +00:00
|
|
|
});
|
|
|
|
|
2015-06-28 23:49:09 +00:00
|
|
|
const test = new Message().text('Test Command');
|
|
|
|
bot.command('test', message => {
|
|
|
|
bot.send(test.to(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
|
|
|
|
|
2015-06-29 22:20:59 +00:00
|
|
|
- [x] Webhook support
|
2015-06-28 22:48:44 +00:00
|
|
|
- [ ] BulkMessage Type
|
|
|
|
- [ ] File Type
|
|
|
|
- [ ] Sticker Type
|
|
|
|
- [ ] Location Type
|
|
|
|
- [ ] Contact Type
|
|
|
|
- [ ] Allow remote control of bots (TCP maybe)
|
2015-06-28 22:54:41 +00:00
|
|
|
- [ ] YOUR IDEAS! [Fill an issue](https://github.com/mdibaiee/node-telegram-api/issues)
|