Bug fixes, Demo added
This commit is contained in:
parent
38790c5ce2
commit
b5f782edb6
10
README.md
10
README.md
@ -7,6 +7,9 @@ npm install telegram-api
|
||||
```
|
||||
|
||||
# Example
|
||||
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.
|
||||
|
||||
```javascript
|
||||
var Bot = require('telegram-api');
|
||||
|
||||
@ -14,7 +17,10 @@ var smartBot = new Bot({
|
||||
token: 'YOUR_KEY'
|
||||
});
|
||||
|
||||
smartBot.start();
|
||||
// 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) {
|
||||
@ -50,3 +56,5 @@ smartBot.api.getUserProfilePhotos
|
||||
```
|
||||
|
||||
This will result in:
|
||||
|
||||
![@JavaScriptBot](https://github.com/mdibaiee/node-telegram-api/raw/master/demo.gif)
|
||||
|
BIN
demo-slow.gif
Normal file
BIN
demo-slow.gif
Normal file
Binary file not shown.
After Width: | Height: | Size: 1.3 MiB |
39
demo.js
Normal file
39
demo.js
Normal file
@ -0,0 +1,39 @@
|
||||
var Bot = require('./index');
|
||||
|
||||
var smartBot = new Bot({
|
||||
token: '121143906:AAE6pcpBoARNZZjr3fUpvKuLInJ5Eee5Ajk'
|
||||
});
|
||||
|
||||
// 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;
|
||||
|
||||
// answers is in format of keyboard rows
|
||||
const question = 'How should I greet you?',
|
||||
answers = [['Hi'], ['Hello, Sir'], ['Yo bro']];
|
||||
|
||||
smartBot.replyTo(message.message_id).askQuestion(id, question, answers)
|
||||
.then(answer => {
|
||||
smartBot.message(id, 'Your answer: ' + answer);
|
||||
}, () => {
|
||||
smartBot.message(id, 'Invalid answer');
|
||||
});
|
||||
});
|
||||
|
||||
// Commands are in format `/command` or `/command@botusername` in groups
|
||||
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!');
|
||||
});
|
35
lib/index.js
35
lib/index.js
@ -1,5 +1,4 @@
|
||||
import API from './api';
|
||||
import * as _ from './utils';
|
||||
import {EventEmitter} from 'events';
|
||||
|
||||
const DEFAULTS = {
|
||||
@ -60,7 +59,7 @@ export default class Bot {
|
||||
});
|
||||
}.bind(this);
|
||||
|
||||
this.api.getMe().then(response => {
|
||||
return this.api.getMe().then(response => {
|
||||
this.info = response.result;
|
||||
poll();
|
||||
});
|
||||
@ -104,13 +103,19 @@ export default class Bot {
|
||||
}
|
||||
|
||||
this.on('update', function listener(result) {
|
||||
console.log(result);
|
||||
console.log(messageId);
|
||||
const update = result.find(({message}) => {
|
||||
return message.chat.id === chat &&
|
||||
message.reply_to_message.message_id === messageId;
|
||||
// if in a group, there will be a reply to this message
|
||||
console.log(message.chat.id, chat);
|
||||
if (chat < 0) {
|
||||
return message.chat.id === chat &&
|
||||
message.reply_to_message.message_id === messageId;
|
||||
} else {
|
||||
return message.chat.id === chat;
|
||||
}
|
||||
});
|
||||
|
||||
console.log(text, '=>', update);
|
||||
|
||||
if (update) {
|
||||
resolve(update);
|
||||
|
||||
@ -128,17 +133,21 @@ export default class Bot {
|
||||
|
||||
askQuestion(chat, title, answers = []) {
|
||||
return new Promise((resolve, reject) => {
|
||||
const rows = [answers];
|
||||
this.keyboard(rows, false, true).force()
|
||||
this.keyboard(answers, false, true).force()
|
||||
.message(chat, title).then(update => {
|
||||
const message = update.message;
|
||||
let answer;
|
||||
|
||||
if (_.isNumber(message.text)) {
|
||||
answer = answers[+message.text];
|
||||
} else {
|
||||
answer = answers.find(a => a === message.text);
|
||||
}
|
||||
console.log(message);
|
||||
answers.forEach(function find(a) {
|
||||
if (Array.isArray(a)) {
|
||||
a.forEach(find);
|
||||
}
|
||||
if (a === message.text) {
|
||||
answer = a;
|
||||
}
|
||||
});
|
||||
console.log(title, '=', answer);
|
||||
|
||||
if (answer) {
|
||||
resolve(answer, update);
|
||||
|
@ -1,3 +0,0 @@
|
||||
export function isNumber(string) {
|
||||
return !isNaN(parseFloat(string));
|
||||
}
|
Loading…
Reference in New Issue
Block a user