telegram-node-bot
Advanced tools
Comparing version 2.0.6 to 2.0.7
@@ -28,2 +28,3 @@ 'use strict' | ||
} | ||
_when(commands, controller) { | ||
@@ -39,4 +40,12 @@ if (typeof commands == 'string') { | ||
this.routes = this.routes.sort((a, b) => { | ||
var aMaxCommandLength = a.commands.reduce((a, b) => a.length > b.length ? a : b).length | ||
var bMaxCommandLength = b.commands.reduce((a, b) => a.length > b.length ? a : b).length | ||
return bMaxCommandLength > aMaxCommandLength | ||
}) | ||
return this | ||
} | ||
_otherwise(controller) { | ||
@@ -47,2 +56,3 @@ this.routes['otherwise'] = controller | ||
} | ||
routeTo(chatId, command) { | ||
@@ -59,5 +69,7 @@ this.waitingCallbacks[chatId] = null | ||
} | ||
controller(name, controller) { | ||
this.controllers[name] = controller | ||
} | ||
call(method, params, callback) { | ||
@@ -78,2 +90,3 @@ callback = callback || Function() | ||
} | ||
sendPhoto(chatId, photo, options, callback) { | ||
@@ -110,2 +123,3 @@ var preparedOptions = this._prepareOptions(options, callback, { | ||
} | ||
sendPhotoFromUrl(chatId, url, options, callback) { | ||
@@ -118,3 +132,2 @@ callback = callback || Function() | ||
wstream.on('finish', () => { | ||
@@ -136,2 +149,3 @@ self.sendPhoto(chatId, fs.createReadStream(__dirname + '/temp/' + fileName), options, (body, err) => { | ||
} | ||
sendDocument(chatId, document, options, callback) { | ||
@@ -164,2 +178,3 @@ var preparedOptions = this._prepareOptions(options, callback, { | ||
} | ||
sendDocumentFromUrl(chatId, url, document, caption, callback) { | ||
@@ -188,2 +203,3 @@ callback = callback || Function() | ||
} | ||
sendMessage(chatId, text, options, callback) { | ||
@@ -206,2 +222,3 @@ var preparedOptions = this._prepareOptions(options, callback, { | ||
} | ||
sendLocation(chatId, latitude, longitude, options, callback) { | ||
@@ -215,2 +232,3 @@ var preparedOptions = this._prepareOptions(options, callback, { | ||
} | ||
sendAudio(chatId, audio, options, callback) { | ||
@@ -247,2 +265,28 @@ var preparedOptions = this._prepareOptions(options, callback, { | ||
} | ||
sendAudioFromUrl(chatId, url, options, callback) { | ||
callback = callback || Function() | ||
var fileName = Math.random().toString(16) + ".mp3" | ||
var wstream = fs.createWriteStream(__dirname + '/temp/' + fileName) | ||
var self = this | ||
wstream.on('finish', () => { | ||
self.sendAudio(chatId, fs.createReadStream(__dirname + '/temp/' + fileName), options, (body, err) => { | ||
fs.unlink(__dirname + '/temp/' + fileName) | ||
if (!err) { | ||
callback(body) | ||
} else { | ||
callback(undefined, err) | ||
} | ||
}) | ||
}) | ||
req.get({ | ||
url: url, | ||
pipe: wstream | ||
}) | ||
} | ||
forwardMessage(chatId, fromChatId, messageId, callback) { | ||
@@ -255,2 +299,3 @@ this.call('forwardMessage', { | ||
} | ||
getFile(fileId, callback) { | ||
@@ -261,2 +306,3 @@ this.call('getFile', { | ||
} | ||
sendChatAction(chatId, action, callback) { | ||
@@ -268,2 +314,3 @@ this.call('sendChatAction', { | ||
} | ||
getUserProfilePhotos(userId, options, callback) { | ||
@@ -275,2 +322,3 @@ var preparedOptions = this._prepareOptions(options, callback, { | ||
} | ||
sendSticker(chatId, sticker, options, callback) { | ||
@@ -307,2 +355,3 @@ var preparedOptions = this._prepareOptions(options, callback, { | ||
} | ||
sendVoice(chatId, voice, options, callback) { | ||
@@ -339,2 +388,3 @@ var preparedOptions = this._prepareOptions(options, callback, { | ||
} | ||
sendVideo(chatId, video, options, callback) { | ||
@@ -371,2 +421,3 @@ var preparedOptions = this._prepareOptions(options, callback, { | ||
} | ||
_getUpdates(callback, offset) { | ||
@@ -396,2 +447,3 @@ var offset = offset ? offset : 0 | ||
} | ||
_processCommand(update) { | ||
@@ -435,15 +487,13 @@ if (update.inline_query) { | ||
for (var commandIndex in route.commands) { | ||
for (var commandIndex in route.commands.sort((a, b) => b.length - a.length)) { | ||
var command = route.commands[commandIndex] | ||
scope['args'] = message ? message.replace(command, '').trim() : null | ||
scope.args = message ? message.replace(command, '').trim() : null | ||
scope.query = this._prepareBotQuery(command, message) | ||
if (message && message.indexOf(command) > -1) { | ||
if (( message && message.indexOf(command) > -1 ) || scope.query) { | ||
this.controllers[route.controller](scope) | ||
if (this.controllersCommands[route.controller]) { | ||
for (var contextCommandIndex in this.controllersCommands[route.controller]) { | ||
if (message && message.indexOf(this.controllersCommands[route.controller][contextCommandIndex].command) > -1) { | ||
this.controllersCommands[route.controller][contextCommandIndex].callback(scope) | ||
} | ||
} | ||
if (this.controllersCommands[route.controller] && this.controllersCommands[route.controller][command]) { | ||
this.controllersCommands[route.controller][command](scope) | ||
} | ||
@@ -453,3 +503,6 @@ | ||
} else { | ||
if (this.router.routes['otherwise'] && commandIndex == route.commands.length - 1 && routeIndex == this.router.routes.length - 1) { | ||
if (this.router.routes['otherwise'] && | ||
commandIndex == route.commands.length - 1 && | ||
routeIndex == this.router.routes.length - 1) { | ||
this.controllers[this.router.routes['otherwise']](scope) | ||
@@ -463,2 +516,3 @@ | ||
} | ||
_prepareOptions(options, callback, params) { | ||
@@ -484,2 +538,33 @@ callback = callback || Function() | ||
} | ||
_prepareBotQuery(mask, query) { | ||
var mask = mask.split(' ') | ||
var query = query.split(' ') | ||
var result = {} | ||
if (mask.length == query.length) { | ||
for (var i = 0; i < query.length; i++) { | ||
switch (mask[i][0]) { | ||
case '/': | ||
if (mask[i] !== query[i]) { | ||
return undefined | ||
} | ||
result.command = query[i] | ||
break | ||
case ':': | ||
result[mask[i].replace(':', '')] = query[i] | ||
break | ||
} | ||
} | ||
} | ||
if (!result.command) | ||
return undefined | ||
return result | ||
} | ||
_createScope(chatId, user, update) { | ||
@@ -494,3 +579,3 @@ var self = this | ||
runMenu: self._runMenu, | ||
routeTo: function(command) { | ||
routeTo: function (command) { | ||
self.waitingCallbacks[this.chatId] = null | ||
@@ -504,4 +589,4 @@ update['message']['text'] = command | ||
'sendPhoto', 'sendPhotoFromUrl', 'sendDocument', 'sendDocumentFromUrl', | ||
'sendMessage', 'sendLocation', 'sendAudio', 'forwardMessage', 'sendChatAction', | ||
'sendSticker', 'sendSticker', 'sendVoice', 'sendVideo', 'waitForRequest' | ||
'sendMessage', 'sendLocation', 'sendAudio', 'sendAudioFromUrl', 'forwardMessage', | ||
'sendChatAction', 'sendSticker', 'sendSticker', 'sendVoice', 'sendVideo', 'waitForRequest' | ||
] | ||
@@ -515,2 +600,3 @@ | ||
} | ||
_runForm(formData, callback) { | ||
@@ -558,2 +644,3 @@ var i = 0 | ||
} | ||
_runMenu(menuData) { | ||
@@ -626,3 +713,4 @@ var startMessage = menuData.message | ||
} | ||
for (_command, callback) { | ||
for(_command, callback) { | ||
for (var routeIndex in this.router.routes) { | ||
@@ -636,23 +724,6 @@ var route = this.router.routes[routeIndex] | ||
if (!this.controllersCommands[route.controller]) { | ||
this.controllersCommands[route.controller] = [] | ||
this.controllersCommands[route.controller] = {} | ||
} | ||
var empty = true | ||
this.controllersCommands[route.controller].forEach((item) => { | ||
if (item.command == _command) { | ||
empty = false | ||
} | ||
}) | ||
if (empty == true) { | ||
this.controllersCommands[route.controller].push({ | ||
command: command, | ||
callback: callback | ||
}) | ||
} else { | ||
for (var commandIndex in this.controllersCommands[route.controller]) { | ||
if (this.controllersCommands[route.controller][commandIndex].command == _command) { | ||
this.controllersCommands[route.controller][commandIndex].callback = callback | ||
} | ||
} | ||
} | ||
this.controllersCommands[route.controller][command] = callback | ||
} | ||
@@ -662,5 +733,7 @@ } | ||
} | ||
waitForRequest(chatId, callback) { | ||
this.waitingCallbacks[chatId] = callback | ||
} | ||
start() { | ||
@@ -673,2 +746,3 @@ this._getUpdates((updates) => { | ||
} | ||
_asyncEach(array, callback) { | ||
@@ -684,5 +758,7 @@ var i = 0 | ||
} | ||
inlineMode(handler) { | ||
this.inlineRequestsHandler = handler | ||
} | ||
answerInlineQuery(inlineQueryId, results, options, callback) { | ||
@@ -699,2 +775,3 @@ if (!results[0].id) { | ||
} | ||
paginatedAnswer(inlineQuery, data, answersPerPage, options) { | ||
@@ -731,2 +808,3 @@ var slicedData = data | ||
} | ||
_createInlineScope(inlineQuery) { | ||
@@ -740,4 +818,4 @@ inlineQuery.answer = this.answerInlineQuery.bind(this, inlineQuery.id) | ||
module.exports = function(token) { | ||
module.exports = function (token) { | ||
return new Telegram(token) | ||
} | ||
} |
{ | ||
"name": "telegram-node-bot", | ||
"version": "2.0.6", | ||
"version": "2.0.7", | ||
"description": "Modile for creating Telegram bots.", | ||
@@ -5,0 +5,0 @@ "main": "lib/telegram.js", |
@@ -58,3 +58,18 @@ # telegram-bot | ||
Sometimes your commands have some args, you can declire them like this: | ||
```js | ||
tg.router. | ||
when(['/sum :num1 :num2'], 'SumController') | ||
``` | ||
After they will be in 'query' property of scope: | ||
```js | ||
tg.controller('SumController', ($) => { | ||
tg.for('/sum :num1 :num2', ($) => { | ||
$.sendMessage(parseInt($.query.num1) + parseInt($.query.num2)) | ||
}) | ||
}) | ||
``` | ||
Let's say you have some login logic in controller probably you need to route user to login 'page'. | ||
@@ -239,2 +254,4 @@ For that case you have routeTo function: | ||
- message | ||
- args ( if you have command '/test' and user will send you '/test 1' 'args' will contain 1 | ||
- query ( object of args ) | ||
@@ -264,2 +281,3 @@ ## Methods List | ||
sendDocumentFromUrl(chatId, url) | ||
sendAudioFromUrl(chatId, url) | ||
call(method, params) | ||
@@ -385,2 +403,2 @@ ``` | ||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. | ||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
227711
654
401
6