diff --git a/README.md b/README.md index d91a120..ff00ad2 100644 --- a/README.md +++ b/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) diff --git a/demo-slow.gif b/demo-slow.gif new file mode 100644 index 0000000..b7c4b70 Binary files /dev/null and b/demo-slow.gif differ diff --git a/demo.js b/demo.js new file mode 100644 index 0000000..f2e2b53 --- /dev/null +++ b/demo.js @@ -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!'); +}); diff --git a/lib/index.js b/lib/index.js index 1497f1e..1de847c 100644 --- a/lib/index.js +++ b/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); diff --git a/lib/utils.js b/lib/utils.js index 333efa2..e69de29 100644 --- a/lib/utils.js +++ b/lib/utils.js @@ -1,3 +0,0 @@ -export function isNumber(string) { - return !isNaN(parseFloat(string)); -}