Options
All
  • Public
  • Public/Protected
  • All
Menu

Raccoonjs API

A simple nodejs module to interact with Telegram Bot API.

Install

$ yarn add raccoonjs-api

or

$ npm i raccoonjs-api

Documentation

See raccoonjs-api documentation here.

Tutorial

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.

Exposing localhost to the internet

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

Coding

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>"

  1. Create Project

     $ mkdir raccoonjs-api-example && cd raccoonjs-api-example
  2. Init Nodejs Project

     $ npm init --yes
  3. Install Raccoonjs-api

     $ npm install raccoonjs-api
  4. Create App

     $ touch index.js
  5. Open Project with Text Editor (vscode)

     $ code .
  6. 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}`);
     });
  7. It's time to try

     $ node index.js

    to see the results, try sending the "/hello" message to the bot and see the magic!

Deploy to Heroku

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.

Special Thanks

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.

Inspiration

Node Telegram Bot API

License

MIT

Index

Type aliases

CallbackQuery

CallbackQuery: { chat_instance: String; data?: string; from: User; id: string; message?: Message }

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.

Type declaration

  • chat_instance: String

    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?: string

    Optional. Data associated with the callback button. Be aware that a bad client can send arbitrary data in this field.

  • from: User

    Sender

  • id: string

    Unique identifier for this query

  • Optional message?: Message

    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

Chat

Chat: { first_name?: string; id: number; last_name?: string; type: string; username?: string }

This type represents a Chat.

Type declaration

  • Optional first_name?: string

    Optional. First name of the other party in a private chat

  • id: number

    Unique identifier for this chat

  • Optional last_name?: string

    Optional. Last name of the other party in a private chat

  • type: string

    Type of chat, can be either “private”, “group”, “supergroup” or “channel”

  • Optional username?: string

    Optional. Username, for private chats, supergroups and channels if available

ChatId

ChatId: number | string

This type represents a unique identifier for the target chat

CommandHandler

CommandHandler: (ctx: Message) => void

This type represents the CommandHandler

Type declaration

InlineKeyboardButton

InlineKeyboardButton: { callback_data?: string; text: string }

This object represents one button of an inline keyboard. You must use exactly one of the optional fields.

Type declaration

  • Optional callback_data?: string

    Optional. Data to be sent in a callback query to the bot when button is pressed, 1-64 bytes

  • text: string

    Label text on the button

InlineKeyboardMarkup

InlineKeyboardMarkup: { inline_keyboard: Array<Array<InlineKeyboardButton>> }

This object represents an inline keyboard that appears right next to the message it belongs to.

Type declaration

  • inline_keyboard: Array<Array<InlineKeyboardButton>>

    Array of button rows, each represented by an Array of InlineKeyboardButton objects

Message

Message: { chat: Chat; date: number; edit_date?: number; entities: Array<MessageEntity>; from?: User; message_id: number; photo?: Array<PhotoSize>; reply_markup?: InlineKeyboardMarkup; text?: string }

This object represents a message.

Type declaration

  • chat: Chat

    Conversation the message belongs to

  • date: number

    Date the message was sent in Unix time

  • Optional edit_date?: number

    Optional. Date the message was last edited in Unix time

  • entities: Array<MessageEntity>

    Optional. For text messages, special entities like usernames, URLs, bot commands, etc. that appear in the text

  • Optional from?: User

    Optional. Sender, empty for messages sent to channels

  • message_id: number

    Unique message identifier inside this chat

  • Optional photo?: Array<PhotoSize>

    Optional. Message is a photo, available sizes of the photo

  • Optional reply_markup?: InlineKeyboardMarkup
  • Optional text?: string

    Optional. For text messages, the actual UTF-8 text of the message, 0-4096 characters.

MessageEntity

MessageEntity: { length: number; offset: number; type: string; url?: string; user?: User }

This object represents one special entity in a text message. For example, hashtags, usernames, URLs, etc.

Type declaration

  • length: number

    Length of the entity in UTF-16 code units

  • offset: number

    Offset in UTF-16 code units to the start of the entity

  • type: string

    Type of the entity.

  • Optional url?: string

    Optional. For “text_link” only, url that will be opened after user taps on the text

  • Optional user?: User

    Optional. For “text_mention” only, the mentioned user

PhotoSize

PhotoSize: { file_id: string; file_size: number; file_unique_id: string; height: number; width: number }

This object represents one size of a photo or a file / sticker thumbnail.

Type declaration

  • file_id: string

    Identifier for this file, which can be used to download or reuse the file

  • file_size: number

    Optional. File size

  • file_unique_id: string

    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.

  • height: number

    Photo height

  • width: number

    Photo width

QueryHandler

QueryHandler: (query: CallbackQuery) => void

This type represents the QueryHandler

Type declaration

SendTextOpt

SendTextOpt: { parse_mode?: "HTML" | "Markdown"; reply_markup?: InlineKeyboardMarkup }

This type represents the optional parameters of SendText method

Type declaration

  • Optional parse_mode?: "HTML" | "Markdown"

    Send Markdown or HTML, if you want Telegram apps to show bold, italic, fixed-width text or inline URLs in your bot's message.

  • Optional reply_markup?: InlineKeyboardMarkup

    Additional interface options. A JSON-serialized object for an inline keyboard.

Update

Update: { callback_query?: CallbackQuery; edited_message?: Message; message?: Message; update_id: number }

This type represents an incoming update.

Type declaration

  • Optional callback_query?: CallbackQuery

    Optional. New incoming callback query

  • Optional edited_message?: Message

    Optional. New version of a message that is known to the bot and was edited

  • Optional message?: Message

    Optional. New incoming message of any kind — text, photo, sticker, etc.

  • update_id: number

    The update‘s unique identifier.

User

User: { first_name: string; id: number; is_bot: boolean; language_code?: string; last_name?: string; username?: string }

This type represents a User.

Type declaration

  • first_name: string

    User‘s or bot’s first name

  • id: number

    Unique identifier for this user or bot

  • is_bot: boolean

    True, if this user is a bot

  • Optional language_code?: string

    Optional. IETF language tag of the user's language

  • Optional last_name?: string

    Optional. User‘s or bot’s last name

  • Optional username?: string

    Optional. User‘s or bot’s username

editCaptionOpt

editCaptionOpt: { caption: string; parse_mode?: string; reply_markup?: InlineKeyboardMarkup }

This type represents the optional parameters of editCaption method

Type declaration

  • caption: string

    New caption of the message

  • Optional parse_mode?: string

    Send Markdown or HTML, if you want Telegram apps to show bold, italic, fixed-width text or inline URLs in your bot's message

  • Optional reply_markup?: InlineKeyboardMarkup

    Additional interface options. A JSON-serialized object for an inline keyboard.

editTextOpt

editTextOpt: { parse_mode?: string; reply_markup?: InlineKeyboardMarkup; text: string }

This type represents the optional parameters of editText method

Type declaration

  • Optional parse_mode?: string

    Send Markdown or HTML, if you want Telegram apps to show bold, italic, fixed-width text or inline URLs in your bot's message

  • Optional reply_markup?: InlineKeyboardMarkup

    Additional interface options. A JSON-serialized object for an inline keyboard.

  • text: string

    New text of the message

makeRequestParams

makeRequestParams: { method: string; multipart?: boolean; payload: any; token: string }

Type declaration

  • method: string
  • Optional multipart?: boolean
  • payload: any
  • token: string

sendPhotoOpt

sendPhotoOpt: { caption?: string; disable_notification?: boolean; parse_mode?: string }

This type represents the optional parameters of sendPhoto method

Type declaration

  • Optional caption?: string

    Optional Photo caption (may also be used when resending photos by file_id), 0-1024 characters

  • Optional disable_notification?: boolean

    Optional Sends the message silently. Users will receive a notification with no sound.

  • Optional parse_mode?: string

    Optional. Send Markdown or HTML, if you want Telegram apps to show bold, italic, fixed-width text or inline URLs in the media caption.

Variables

Const METHOD_URL

METHOD_URL: "https://api.telegram.org/bot%s/%s" = "https://api.telegram.org/bot%s/%s"

Const WEBHOOK_URL

WEBHOOK_URL: "https://api.telegram.org/bot%s/setWebhook?url=%s" = "https://api.telegram.org/bot%s/setWebhook?url=%s"

Functions

Const bold

  • bold(text: string): string
  • Use this function to create bold text

    Parameters

    • text: string

      target text

    Returns string

Const btn

  • btn(label: string, value: string): { callback_data: string; text: string }
  • Use this function to create a InlineKeyboardButton

    Parameters

    • label: string

      The label

    • value: string

      The callback_data value

    Returns { callback_data: string; text: string }

    • callback_data: string
    • text: string

Const italic

  • italic(text: string): string
  • Use this function to create italic text

    Parameters

    • text: string

      target text

    Returns string

Const link

  • link(label: string, uri: string): string
  • Use this function to create a link

    Parameters

    • label: string

      The label

    • uri: string

      The target url

    Returns string

makeRequest

  • makeRequest<T>(__namedParameters: { method: string; multipart: boolean; payload: any; token: string }): Promise<T>
  • Type parameters

    • T

    Parameters

    • __namedParameters: { method: string; multipart: boolean; payload: any; token: string }
      • method: string
      • multipart: boolean
      • payload: any
      • token: string

    Returns Promise<T>

Legend

  • Module
  • Object literal
  • Variable
  • Function
  • Function with type parameter
  • Index signature
  • Type alias
  • Type alias with type parameter
  • Enumeration
  • Enumeration member
  • Property
  • Method
  • Interface
  • Interface with type parameter
  • Constructor
  • Property
  • Method
  • Index signature
  • Class
  • Class with type parameter
  • Constructor
  • Property
  • Method
  • Accessor
  • Index signature
  • Inherited constructor
  • Inherited property
  • Inherited method
  • Inherited accessor
  • Protected property
  • Protected method
  • Protected accessor
  • Private property
  • Private method
  • Private accessor
  • Static property
  • Static method

Generated using TypeDoc