Telegram bot API. Flow-compatible. Without unnecessary explicit dependencies in runtime.
npm install --save tgapi
import BotClient from 'tgapi';
import sendRequest from 'tgapi/sendRequest';
const token = 'bla-bla-bla';
const bot = new BotClient(token, sendRequest);
bot.getMe()
.then(userObject => console.log(userObject));
bot.getUpdates({ offset: 100500 })
.then(updatesArray => console.log(updatesArray));
sendRequest
is the default implementation of the method of sending http requests to Telegram
server. You can to use another. sendRequest
must receive the bot token as first argument and
Telegram API method parameters and must returns Promise
.
bot.on('updateReceived', update => console.log(update));
bot.startWatchUpdates(1);
// Will check updates each 1 second and emit updateReceived event
// after each one update.
If you want to use webhooks, you can use this http-server:
import BotClient from 'tgapi';
import sendRequest from 'tgapi/sendRequest';
import pureServer from 'tgapi/pureServer';
const bot = new BotClient('your token', sendRequest);
const server = pureServer(bot, 'your/webhook/path');
server.listen(80, () => console.log(
'Webhook are available on http://localhost/your/webhook/path'
));
bot.on('updateReceived', update => console.log(update));
Creates a promise
that will be resolved if the update predicate returns true
or will be rejected if the timeout has
expired. Timeout default value is 300000
ms (5 min). You can disable timeout by setting this value
to 0
, but it creates memory leak danger.
const predicate = (update) => (
update.message &&
update.message.text === 'Hello'
);
// Set timeout to 10 min
bot.createReaction(1000 * 60 * 10)(predicate)
.then(update => bot.sendMessage({
chat_id: update.message.chat.id,
text: 'Hi!',
}))
.catch(() => console.log('Nobody wants to greet me. =('));
type CreateInlineButton =
(text: string, timeout?: number = 1000 * 60 * 5) =>
(buttonId?: string) => {
markup: InlineKeyboardButton,
promise: Promise<CertainButtonPressedEvent>,
};
Creates a InlineKeyboardButton
markup
and Promice that pending the press event. Promise resolves with
inlineButtonPressed/<buttonId>
event:
const createHelloButton = bot.createButton('Hi!', 1000 * 60 * 10);
const createOkButton = bot.createButton('OK!');
const createNoButton = bot.createButton('No, sorry.');
const helloButton = createHelloButton(Math.random());
const okButton = createOkButton();
const noButton = createNoButton();
bot.sendMessage({
chat_id, text: 'Hello?',
reply_markup: { inline_keyboard: [[helloButton.markup]] },
});
helloButton.promise
.then(() => bot.sendMessage({
chat_id, text: 'Let\'s talk?',
reply_markup: { inline_keyboard: [[
okButton.markup,
noButton.markup,
]] },
}));
okButton.promise.then(() => bot.sendMessage({ chat_id, text: 'What\'s your name?' }));
noButton.promise.then(() => bot.sendMessage({ chat_id, text: 'OK... =(' }));
Emitted each any update. Receives Update type.
Emitted each any bot command. Receives CommandEvent type.
Emitted each specific bot command. Receives CommandEvent type. Example:
bot.on('commandReceived/start', sendHelloMessage);
Emitted each callback_query
update. Receives ButtonPressedEvent type.
Emitted each callback_query
update with specified buttonId
. Receives
CertainButtonPressedEvent type. For emit this event
callback_query.data
must be JSON
object winth string or number buttonId
property:
{ "buttonId": 1,
"anyAnotherProperty": "value" }
CommandEvent
typeupdate
— Updatecommand
— Command text. For example/say
for message/say Hey you!
args
— Text after command. For exampleHey you!
for message/say Hey you!
ButtonPressedEvent
typeCertainButtonPressedEvent
type
-
getUpdates
-
setWebhook
-
deleteWebhook
-
getWebhookInfo
-
getMe
-
sendMessage
-
forwardMessage
-
sendPhoto
-
sendAudio
-
sendDocument
-
sendSticker
-
sendVideo
-
sendVoice
-
sendLocation
-
sendVenue
-
sendContact
-
sendChatAction
-
getUserProfilePhotos
-
getFile
-
kickChatMember
-
leaveChat
-
unbanChatMember
-
getChat
-
getChatAdministrators
-
getChatMembersCount
-
getChatMember
-
answerCallbackQuery
-
answerInlineQuery
-
sendGame
-
setGameScore
-
getGameHighScores