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
|
# 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
|
```javascript
|
||||||
var Bot = require('telegram-api');
|
var Bot = require('telegram-api');
|
||||||
|
|
||||||
@ -14,7 +17,10 @@ var smartBot = new Bot({
|
|||||||
token: 'YOUR_KEY'
|
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
|
// You can use regular expressions, too
|
||||||
smartBot.get('Hi', function(update) {
|
smartBot.get('Hi', function(update) {
|
||||||
@ -50,3 +56,5 @@ smartBot.api.getUserProfilePhotos
|
|||||||
```
|
```
|
||||||
|
|
||||||
This will result in:
|
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!');
|
||||||
|
});
|
29
lib/index.js
29
lib/index.js
@ -1,5 +1,4 @@
|
|||||||
import API from './api';
|
import API from './api';
|
||||||
import * as _ from './utils';
|
|
||||||
import {EventEmitter} from 'events';
|
import {EventEmitter} from 'events';
|
||||||
|
|
||||||
const DEFAULTS = {
|
const DEFAULTS = {
|
||||||
@ -60,7 +59,7 @@ export default class Bot {
|
|||||||
});
|
});
|
||||||
}.bind(this);
|
}.bind(this);
|
||||||
|
|
||||||
this.api.getMe().then(response => {
|
return this.api.getMe().then(response => {
|
||||||
this.info = response.result;
|
this.info = response.result;
|
||||||
poll();
|
poll();
|
||||||
});
|
});
|
||||||
@ -104,13 +103,19 @@ export default class Bot {
|
|||||||
}
|
}
|
||||||
|
|
||||||
this.on('update', function listener(result) {
|
this.on('update', function listener(result) {
|
||||||
console.log(result);
|
|
||||||
console.log(messageId);
|
|
||||||
const update = result.find(({message}) => {
|
const update = result.find(({message}) => {
|
||||||
|
// 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 &&
|
return message.chat.id === chat &&
|
||||||
message.reply_to_message.message_id === messageId;
|
message.reply_to_message.message_id === messageId;
|
||||||
|
} else {
|
||||||
|
return message.chat.id === chat;
|
||||||
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
console.log(text, '=>', update);
|
||||||
|
|
||||||
if (update) {
|
if (update) {
|
||||||
resolve(update);
|
resolve(update);
|
||||||
|
|
||||||
@ -128,17 +133,21 @@ export default class Bot {
|
|||||||
|
|
||||||
askQuestion(chat, title, answers = []) {
|
askQuestion(chat, title, answers = []) {
|
||||||
return new Promise((resolve, reject) => {
|
return new Promise((resolve, reject) => {
|
||||||
const rows = [answers];
|
this.keyboard(answers, false, true).force()
|
||||||
this.keyboard(rows, false, true).force()
|
|
||||||
.message(chat, title).then(update => {
|
.message(chat, title).then(update => {
|
||||||
const message = update.message;
|
const message = update.message;
|
||||||
let answer;
|
let answer;
|
||||||
|
|
||||||
if (_.isNumber(message.text)) {
|
console.log(message);
|
||||||
answer = answers[+message.text];
|
answers.forEach(function find(a) {
|
||||||
} else {
|
if (Array.isArray(a)) {
|
||||||
answer = answers.find(a => a === message.text);
|
a.forEach(find);
|
||||||
}
|
}
|
||||||
|
if (a === message.text) {
|
||||||
|
answer = a;
|
||||||
|
}
|
||||||
|
});
|
||||||
|
console.log(title, '=', answer);
|
||||||
|
|
||||||
if (answer) {
|
if (answer) {
|
||||||
resolve(answer, update);
|
resolve(answer, update);
|
||||||
|
@ -1,3 +0,0 @@
|
|||||||
export function isNumber(string) {
|
|
||||||
return !isNaN(parseFloat(string));
|
|
||||||
}
|
|
Loading…
Reference in New Issue
Block a user