TeaBot
allows you to create highly interactive Telegram bots for Node.js with some additional cool features.
- Written for creating interactive bots
- Supports plugins
- Data storage in Redis, Aerospike (with plugins)
- Analytics botan.io (with plugins)
- Supports Inline Mode
- Supports
/command@BotName
commands - Has own wrapper over Telegram API to enhance existing functionality
Difference between TeaBot 1.x.x
and TeaBot 2.0.0
here.
$ npm install teabot --save
Simple echo bot:
const TeaBot = require('teabot')('TELEGRAM_BOT_TOKEN', 'TELEGRAM_BOT_NAME');
TeaBot.defineCommand(function (dialog, message) {
dialog.sendMessage('Echo: ' + message.text);
});
TeaBot.startPolling();
TeaBot
based on tg-yarl
(wrapper over Telegram Bot Api with additional features) package, it means TeaBot
inherits all methods from tg-yarl
.
Depending on what type of connection with Telegram is used (webhook
or long polling
), there are 2 methods to start the bot.
To work with webhook
.
- message (Object) - Message object received from Telegram using the webhook.
To work with long polling
.
- [options] (Object) - Polling options:
- offset (Integer) - Identifier of the first update to be returned (0 by default).
- limit (Integer) - Limits the number of updates to be retrieved (100 by default).
- timeout (Integer) - Timeout in seconds for long polling (60 by default).
Dialog object
stores bot current dialogue with the user
, as well as commands (full list here) for communication.
It can be obtained from the first parameter in defineCommand
and defineAction
callbacks, or directly from TeaBot
object.
Checks whether the user is in a state of action, and if so action name
will be returned, otherwise false
.
Start the action. Then all processes occur in defineAction
callbacks. Action callback will be called at the next incoming message.
- action (String) - Name of the action defined in
defineAction
.
Perform the action. Then all processes occur in defineAction
callbacks. Action callback will be called immediately
.
- action (String) - Name of the action defined in
defineAction
.
Ends the action and clears dialog.tempData
.
- [saveTemp] (Boolean) - If true, then
dialog.tempData
will not be cleared.
More dialog object
methods in docs.
Message object
stores processed incoming message, as well as its original copy.
It can be obtained from dialog.message
or from the second parameter in defineCommand
and defineAction
callbacks.
Returns command
or empty string.
It returns the rest of the message, if it contains a command
or the entire message
.
More message object
methods in docs.
Commands
always starts with /
.
- command (String|Array) - Command or an array of commands. Also supports wildcards (Use
*
to match zero or more characters. A pattern starting with!
will be negated). - callback (Function) - Callback which is invoked for this command/commands.
- callback (Function) - Callback which is invoked if the given command is not defined or is not available.
TeaBot
.defineCommand(['/start', '/help'], function (dialog, message) {
dialog.sendMessage('Hi there. This is a ' + message.getCommand() + ' command.');
})
.defineCommand('/hi*', function (dialog, message) { // wildcard
dialog.sendMessage('This command ' + message.getCommand() + ', starts with /hi');
})
.defineCommand(function (dialog) {
dialog.sendMessage('Send me /help for more information.');
});
You can define some actions
if you want to add interactivity to your bot. Or you want to split your code.
- action (String|Array) - Action or an array of actions. Also supports wildcards (Use
*
to match zero or more characters. A pattern starting with!
will be negated). - callback (Function) - Callback which is invoked for this action/actions when it starts.
TeaBot
.defineCommand('/help', function (dialog, message) {
if (message.getArgument()) {
dialog.performAction('/help:1'); // /help argument
} else {
dialog.startAction('/help:2').sendMessage('This is /help command.'); // /help
}
})
.defineCommand(function (dialog) {
dialog.sendMessage('Send me /help for more information.');
});
TeaBot
.defineAction('/help:*', function (dialog) { // wildcard
dialog.endAction().sendMessage('This is ' + dialog.getAction() + ' action'); // if /help was with argument, then /help:1 action, otherwise /help:2
});
- query (String|Array) - Query or an array of queries. Also supports wildcards (Use
*
to match zero or more characters. A pattern starting with!
will be negated). - callback (Function) - Callback which is invoked for this query/queries.
- callback (Function) - Callback which is invoked if the given query is not defined, is not available or is empty.
TeaBot
.inlineQuery('tay*', function (query) { // wildcard
query
.addGif(
{ gif_url: 'https://33.media.tumblr.com/tumblr_m3xrtsmgs11rn435g.gif', thumb_url: 'https://33.media.tumblr.com/tumblr_m3xrtsmgs11rn435g.gif', gif_width: 500, gif_height: 247 }
)
.addGif(
{ gif_url: 'http://blog.admissions.illinois.edu/wp-content/uploads/2015/08/Screaming-Taylor-Swift.gif', thumb_url: 'http://blog.admissions.illinois.edu/wp-content/uploads/2015/08/Screaming-Taylor-Swift.gif', gif_width: 480, gif_height: 267 }
)
.answer();
})
.inlineQuery(function (query) {
query
.addArticles([
{ title: 'Test 1', message_text: 'test' },
{ title: 'Test 2', message_text: 'test' },
{ title: 'Test 3', message_text: 'test' }
])
.answer();
});
More info about inline mode
in docs.
More info about query object
in docs.
At the moment TeaBot
supports only db
and analytics
plugins, but in the future there will be more.
- type (String) - Plugin type:
db
oranalytics
. - plugin (Object) - Object with plugin.
const TeaBot = require('teabot')('TELEGRAM_BOT_TOKEN', 'TELEGRAM_BOT_NAME');
TeaBot.use('analytics', require('teabot-botan')('BOTAN_TOKEN'));
TeaBot.defineCommand(function (dialog, message) {
dialog.sendMessage('Echo: ' + message.text); // all message events will be sent directly to botan.io
});
TeaBot.startPolling();
- DB:
- Redis - teabot-redis
- Aerospike - teabot-aerospike
- Analytics:
- Botan - teabot-botan
More info about plugins
in docs.
Information about how to write your own plugin here.
By default, no errors are displayed, except for those that may interfere start the bot. But with these methods you be able to handle errors by yourself.
Use this method in Promise
, Callback
functions and whenever you want.
- error (Object) - Error object.
When error occurs or when TeaBot.error()
is used, callback will be invoked.
- callback (Function) - Callback which is invoked when
TeaBot.error()
is called (including internal call).
TeaBot.onError(function (e) {
console.error('TeaBot error:', e.stack);
});
TeaBot.defineCommand(function (dialog, message) {
dialog.sendMessage('Echo: ' + message.text).then(function () {
throw new Error('Test error 1');
}).catch(TeaBot.error);
throw new Error('Test error 2');
});
- @WezaBot - strikeentco/WezaBot - weather bot written with TeaBot.
Other examples here.
The MIT License (MIT)
Copyright (c) 2015-2016 Alexey Bystrov