$ yarn add raccoonjs-api
or
$ npm i raccoonjs-api
See raccoonjs-api documentation here.
In this tutorial we will first use localhost
as a server to develop the Telegram bot, but at the end of this tutorial we will try to change localhost
to Heroku as a server.
In order for bots to receive incoming updates from the Telegram server, localhost
needs to be exposed to the internet using Ngrok.
$ ngork http <PORT>
In this tutorial we use PORT = 5000
Here we will make a simple bot that makes the bot can respond back when the user sends certain commands to the bot. For example, when a user sends a message like this "/hello"
, the bot will respond "Hello, <your_name>"
Create Project
$ mkdir raccoonjs-api-example && cd raccoonjs-api-example
Init Nodejs Project
$ npm init --yes
Install Raccoonjs-api
$ npm install raccoonjs-api
Create App
$ touch index.js
Open Project with Text Editor (vscode)
$ code .
Write a simple bot
// index.js
// import the module
const { TelegramAPI } = require("raccoonjs-api");
// A unique authentication token see: https://core.telegram.org/bots/api#making-requests
const YOUR_TOKEN = "123456:ABC-DEF1234ghIkl-zyx57W2v1u123ew11";
// Ngrok forwarding secure url (prefix with 'https')
const YOUR_HOST = "https://<unique_number>.ngrok.io";
const port = 5000;
// create bot instance
const bot = new TelegramAPI(YOUR_TOKEN, YOUR_HOST);
// start the webhook then the bot can receive incoming updates
// it is recommended to use a secret path in the URL, e.g. YOUR_HOST/<YOUR_TOKEN>. Because no one else knows your bot token.
bot.startWebhook("/secretPath", port, () => {
console.log("Webhook started!");
});
// define a command message
bot.cmd("hello", ctx => {
const { chat } = ctx;
bot.sendText(chat.id, `Hello ${chat.first_name}`);
});
It's time to try
$ node index.js
to see the results, try sending the "/hello" message to the bot and see the magic!
Follow the official tutorial from Heroku. Deploying Node.js Apps on Heroku
Note: Because this bot application is not a web application, but a process it is necessary to add the following script to the Procfile file.
worker: node index.js
Note: Before actually deploying an application to Heroku it is recommended to save variables such as token or other credentials to the environment variables. See.
PT Privy Identitas Digital (PrivyID)
Thanks for giving the internship opportunity as a Back End Developer, this project began when PrivyID gave the task of making a Telegram bot to assist in the Stand-up Meeting.
Global identifier, uniquely corresponding to the chat to which the message with the callback button was sent. Useful for high scores in games.
Optional. Data associated with the callback button. Be aware that a bad client can send arbitrary data in this field.
Sender
Unique identifier for this query
Optional. Message with the callback button that originated the query. Note that message content and message date will not be available if the message is too old
This type represents a Chat.
Optional. First name of the other party in a private chat
Unique identifier for this chat
Optional. Last name of the other party in a private chat
Type of chat, can be either “private”, “group”, “supergroup” or “channel”
Optional. Username, for private chats, supergroups and channels if available
This type represents a unique identifier for the target chat
This type represents the CommandHandler
This object represents one button of an inline keyboard. You must use exactly one of the optional fields.
Optional. Data to be sent in a callback query to the bot when button is pressed, 1-64 bytes
Label text on the button
This object represents an inline keyboard that appears right next to the message it belongs to.
Array of button rows, each represented by an Array of InlineKeyboardButton objects
This object represents a message.
Conversation the message belongs to
Date the message was sent in Unix time
Optional. Date the message was last edited in Unix time
Optional. For text messages, special entities like usernames, URLs, bot commands, etc. that appear in the text
Optional. Sender, empty for messages sent to channels
Unique message identifier inside this chat
Optional. Message is a photo, available sizes of the photo
Optional. For text messages, the actual UTF-8 text of the message, 0-4096 characters.
This object represents one special entity in a text message. For example, hashtags, usernames, URLs, etc.
Length of the entity in UTF-16 code units
Offset in UTF-16 code units to the start of the entity
Type of the entity.
Optional. For “text_link” only, url that will be opened after user taps on the text
Optional. For “text_mention” only, the mentioned user
This object represents one size of a photo or a file / sticker thumbnail.
Identifier for this file, which can be used to download or reuse the file
Optional. File size
Unique identifier for this file, which is supposed to be the same over time and for different bots. Can't be used to download or reuse the file.
Photo height
Photo width
This type represents the QueryHandler
This type represents the optional parameters of SendText method
Send Markdown or HTML, if you want Telegram apps to show bold, italic, fixed-width text or inline URLs in your bot's message.
Additional interface options. A JSON-serialized object for an inline keyboard.
This type represents an incoming update.
Optional. New incoming callback query
Optional. New version of a message that is known to the bot and was edited
Optional. New incoming message of any kind — text, photo, sticker, etc.
The update‘s unique identifier.
This type represents a User.
User‘s or bot’s first name
Unique identifier for this user or bot
True, if this user is a bot
Optional. IETF language tag of the user's language
Optional. User‘s or bot’s last name
Optional. User‘s or bot’s username
This type represents the optional parameters of editCaption method
New caption of the message
Send Markdown or HTML, if you want Telegram apps to show bold, italic, fixed-width text or inline URLs in your bot's message
Additional interface options. A JSON-serialized object for an inline keyboard.
This type represents the optional parameters of editText method
Send Markdown or HTML, if you want Telegram apps to show bold, italic, fixed-width text or inline URLs in your bot's message
Additional interface options. A JSON-serialized object for an inline keyboard.
New text of the message
This type represents the optional parameters of sendPhoto method
Optional Photo caption (may also be used when resending photos by file_id), 0-1024 characters
Optional Sends the message silently. Users will receive a notification with no sound.
Optional. Send Markdown or HTML, if you want Telegram apps to show bold, italic, fixed-width text or inline URLs in the media caption.
Use this function to create bold text
target text
Use this function to create a InlineKeyboardButton
The label
The callback_data value
Use this function to create italic text
target text
Use this function to create a link
The label
The target url
Generated using TypeDoc
This object represents an incoming callback query from a callback button in an inline keyboard. If the button that originated the query was attached to a message sent by the bot, the field message will be present.