Comparing version 1.0.0 to 1.1.0
@@ -21,3 +21,3 @@ var TeaBot = require('../main'); | ||
if (message) { | ||
Bot.start(message); | ||
Bot.receive(message); | ||
} | ||
@@ -24,0 +24,0 @@ res.status(200).end(); |
@@ -47,2 +47,2 @@ var TeaBot = require('../main'); | ||
Bot.startPooling(); // for webhook see webhook.js example | ||
Bot.startPooling(); // for webhook see ex1.webhook.js example |
@@ -9,3 +9,3 @@ var TeaBot = require('../main'); | ||
if (message.isCommand()) { | ||
if (message.getArgument() == '') { | ||
if (!message.getArgument()) { | ||
dialog.sendMessage('It\'s just a command.'); | ||
@@ -20,2 +20,2 @@ } else { | ||
Bot.startPooling(); // for webhook see webhook.js example | ||
Bot.startPooling(); // for webhook see ex1.webhook.js example |
@@ -31,2 +31,2 @@ var TeaBot = require('../main'); | ||
Bot.startPooling(); // for webhook see webhook.js example | ||
Bot.startPooling(); // for webhook see ex1.webhook.js example |
@@ -18,3 +18,4 @@ 'use strict'; | ||
config._getUpdate(_this, function(err) { | ||
_this.url = config.url; | ||
_this.url || (_this.url = config.url); | ||
_this._actions = config.actions; | ||
_this.message = new Message(data); | ||
@@ -43,11 +44,5 @@ config._preprocess(_this.message); | ||
var subAction = action.getLevel(count); | ||
subAction._func(_this); | ||
if (config.analytics && !config.options.analytics.manualMode) { | ||
config.track(_this.userId, _this.message, 'Subaction: ' + subAction.name); | ||
} | ||
subAction._func('Subaction: ' + subAction.name, _this); | ||
} else { | ||
action._func(_this); | ||
if (config.analytics && !config.options.analytics.manualMode) { | ||
config.track(_this.userId, _this.message, 'Action: ' + action.name); | ||
} | ||
action._func('Action: ' + action.name, _this); | ||
} | ||
@@ -69,4 +64,2 @@ } | ||
} | ||
//return this.action; | ||
}; | ||
@@ -132,3 +125,3 @@ | ||
Chat.prototype.startAction = function(data) { | ||
Chat.prototype.startAction = function(data, perform) { | ||
if (!data) { | ||
@@ -140,5 +133,18 @@ this.action = false; | ||
this.action.getLevel(level).setSubAction(data); | ||
if (perform) { | ||
var action = this._actions[this.action.name] || false; | ||
if (action) { | ||
var subAction = action.getLevel(level + 1); | ||
subAction._func('Subaction: ' + subAction.name, this); | ||
} | ||
} | ||
} | ||
} else { | ||
this.action = new Action(data); | ||
if (perform) { | ||
var action = this._actions[this.action.name] || false; | ||
if (action) { | ||
action._func('Action: ' + action.name, this); | ||
} | ||
} | ||
} | ||
@@ -149,4 +155,7 @@ | ||
Chat.prototype.endAction = function() { | ||
this.clearTempData(); | ||
Chat.prototype.endAction = function(saveTemp) { | ||
if (!saveTemp) { | ||
this.clearTempData(); | ||
} | ||
this.action = false; | ||
@@ -153,0 +162,0 @@ return this; |
71
main.js
@@ -84,4 +84,7 @@ 'use strict'; | ||
if (this.db) { | ||
var data = JSON.parse(JSON.stringify(dialog)); | ||
data.action = (dialog.action) ? dialog.action.getNames() : []; | ||
var data = { | ||
userData: dialog.userData, | ||
tempData: dialog.tempData, | ||
action: (dialog.action) ? dialog.action.getNames() : [], | ||
}; | ||
this.db.put(dialog.chatId + '_' + dialog.userId, data).catch(function(e) { | ||
@@ -135,16 +138,4 @@ console.error(e.stack); | ||
Tea.prototype._pooling = function() { | ||
//this._bool || this._reinit(); | ||
var _this = this; | ||
/*function callback(data) { | ||
var chatId = data.chat.id; | ||
var userId = data.from.id; | ||
_this.dialogs[chatId] || (_this.dialogs[chatId] = {}); | ||
_this.dialogs[chatId][userId] || (_this.dialogs[chatId][userId] = new Chat(chatId, userId)); | ||
_this.dialogs[chatId][userId]._processing(data, _this); | ||
}*/ | ||
_this.getUpdates(_this.offset, _this.limit, _this.timeout).then(function(update) { | ||
@@ -154,4 +145,3 @@ var result = update.result; | ||
for (var i = 0; i < l; i++) { | ||
//callback(result[i].message); | ||
_this.start(result[i].message); | ||
_this.receive(result[i].message); | ||
if (i === (l - 1)) { | ||
@@ -167,3 +157,3 @@ _this.offset = result[i].update_id + 1; | ||
Tea.prototype.start = function(data) { | ||
Tea.prototype.receive = Tea.prototype.start = function(data) { | ||
this._bool || this._reinit(); | ||
@@ -242,6 +232,36 @@ | ||
for (var i = 0; i < data.length; i++) { | ||
this.actions[data[i]] || (this.actions[data[i]] = new ConfigAction(data[i], 1, callback, subAction)); | ||
if (!this.actions[data[i]]) { | ||
this.actions[data[i]] = new ConfigAction(data[i], 1, callback, subAction); | ||
var cb = this.actions[data[i]]._func; | ||
this.actions[data[i]]._func = function() { | ||
if (arguments && arguments.length === 2) { | ||
var dialog = arguments[1]; | ||
if (this.analytics && !this.options.analytics.manualMode) { | ||
this.track(dialog.userId, dialog.message, arguments[0]); | ||
} | ||
cb.call(null, dialog); | ||
} else if (arguments && arguments.length === 1) { | ||
cb.call(null, arguments[0]); | ||
} | ||
}; | ||
} | ||
} | ||
} else if (data && typeof data === 'string') { | ||
this.actions[data] || (this.actions[data] = new ConfigAction(data, 1, callback, subAction)); | ||
if (!this.actions[data]) { | ||
this.actions[data] = new ConfigAction(data, 1, callback, subAction); | ||
var cb = this.actions[data]._func; | ||
this.actions[data]._func = function() { | ||
if (arguments && arguments.length === 2) { | ||
var dialog = arguments[1]; | ||
if (this.analytics && !this.options.analytics.manualMode) { | ||
this.track(dialog.userId, dialog.message, arguments[0]); | ||
} | ||
cb.call(null, dialog); | ||
} else if (arguments && arguments.length === 1) { | ||
cb.call(null, arguments[0]); | ||
} | ||
}; | ||
} | ||
} else { | ||
@@ -354,2 +374,15 @@ throw new Error('Action must be string or array.'); | ||
this.subAction = new ConfigAction(action, this.level + 1, callback, subAction); | ||
var cb = this.subAction._func; | ||
this.subAction._func = function() { | ||
if (arguments && arguments.length === 2) { | ||
var dialog = arguments[1]; | ||
if (this.analytics && !this.options.analytics.manualMode) { | ||
this.track(dialog.userId, dialog.message, arguments[0]); | ||
} | ||
cb.call(null, dialog); | ||
} else if (arguments && arguments.length === 1) { | ||
cb.call(null, arguments[0]); | ||
} | ||
}; | ||
} else { | ||
@@ -356,0 +389,0 @@ throw new Error('SubAction must be string.'); |
{ | ||
"name": "teabot", | ||
"author": "Alexey Bystrov <strikeentco@gmail.com>", | ||
"version": "1.0.0", | ||
"version": "1.1.0", | ||
"description": "TeaBot - a way to build interactive Telegram bots.", | ||
@@ -6,0 +6,0 @@ "keywords": [ |
@@ -27,3 +27,3 @@ teabot | ||
* [Start methods](#start-methods) | ||
* [start(message)](#botstartmessage) | ||
* [receive(message)](#botreceivemessage) | ||
* [startPooling([options])](#botstartpoolingoptions) | ||
@@ -35,4 +35,4 @@ * [Analytics](#bot-analytics) | ||
* [inAction()](#dialoginaction) | ||
* [startAction(action)](#dialogstartactionaction) | ||
* [endAction()](#dialogendaction) | ||
* [startAction(action, [perform])](#dialogstartactionaction-perform) | ||
* [endAction([saveTemp])](#dialogendactionsavetemp) | ||
* [User data](#dialoguserdata) | ||
@@ -194,3 +194,3 @@ * [getUserData(property)](#dialoggetuserdataproperty) | ||
### Bot.start(message) | ||
### Bot.receive(message) | ||
@@ -239,3 +239,3 @@ To work with webhook. | ||
### dialog.startAction(action) | ||
### dialog.startAction(action, [perform]) | ||
@@ -246,9 +246,14 @@ Start the action. Then all processes occur in `defineAction` or `defineSubAction` callbacks. | ||
* **action** (*String*) - Name of action defined in `defineAction` or `defineSubAction` for start. | ||
* **[perform]** (*Boolean*) - If true, the action callback will be called immediately. Otherwise, it will happen at the next incoming message. | ||
### dialog.endAction() | ||
### dialog.endAction([saveTemp]) | ||
Ends the action and clears `dialog.tempData`. | ||
[Example with action](https://github.com/strikeentco/teabot/tree/master/examples/ex3.action.js) | ||
#### Params | ||
* **[saveTemp]** (*Boolean*) - If true, then `dialog.tempData` will not be cleared. | ||
[Example with action: 1st way](https://github.com/strikeentco/teabot/tree/master/examples/ex3-1.action.js) | ||
[Example with action: 2nd way](https://github.com/strikeentco/teabot/tree/master/examples/ex3-2.action.js) | ||
## dialog.userData | ||
@@ -255,0 +260,0 @@ |
52721
19
909
663