Develop a 'Telegram Bot' with R

Provides a pure interface for the 'Telegram Bot API' <>. In addition to the pure API implementation, it features a number of tools to make the development of 'Telegram' bots with R easy and straightforward, providing an easy-to-use interface that takes some work off the programmer.

CRAN Downloads Travis CI Status AppVeyor Build Status Codecov License

This package provides a pure R interface for the Telegram Bot API. In addition to the pure API implementation, it features a number of tools to make the development of Telegram bots with R easy and straightforward, providing an easy-to-use interface that takes some work off the programmer.


You can install from CRAN:


Or the development version from GitHub:

# install.packages("devtools")


You can quickly build a chatbot with a few lines:

start <- function(bot, update){
  bot$sendMessage(chat_id = update$message$chat$id,
                  text = sprintf("Hello %s!", update$message$from$first_name))
updater <- Updater("TOKEN") + CommandHandler("start", start)
updater$start_polling() # Send '/start' to the bot

If you don't have a TOKEN, you can follow the steps explained below to generate one.

Telegram API Methods

One of the core instances from the package is Bot, which represents a Telegram Bot. You can find a full list of the Telegram API methods implemented in its documentation (?Bot), but here there are some examples:

# Initialize bot
bot <- Bot(token = "TOKEN")
# Get bot info
# Get updates
updates <- bot$getUpdates()
# Retrieve your chat id
# Note: you should text the bot before calling 'getUpdates'
chat_id <- updates[[1L]]$from_chat_id()
# Send message
bot$sendMessage(chat_id = chat_id,
                text = "foo *bold* _italic_",
                parse_mode = "Markdown")
# Send photo
bot$sendPhoto(chat_id = chat_id,
              photo = "")
# Send audio
bot$sendAudio(chat_id = chat_id,
              audio = "")
# Send document
bot$sendDocument(chat_id = chat_id,
                 document = "")
# Send sticker
bot$sendSticker(chat_id = chat_id,
                sticker = "")
# Send video
bot$sendVideo(chat_id = chat_id,
              video = "")
# Send gif
bot$sendAnimation(chat_id = chat_id,
                  animation = "")
# Send location
bot$sendLocation(chat_id = chat_id,
                 latitude = 51.521727,
                 longitude = -0.117255)
# Send chat action
bot$sendChatAction(chat_id = chat_id,
                   action = "typing")
# Get user profile photos
photos <- bot$getUserProfilePhotos(user_id = chat_id)
# Download user profile photo
file_id <- photos$photos[[1L]][[1L]]$file_id
bot$getFile(file_id, destfile = "photo.jpg")

Note that you can also send local files by passing their path instead of an URL. Additionaly, all methods accept their equivalent snake_case syntax (e.g. bot$get_me() is equivalent to bot$getMe()).

Generating an Access Token

To make it work, you'll need an access TOKEN (it should look something like 123456:ABC-DEF1234ghIkl-zyx57W2v1u123ew11). If you don't have it, you have to talk to @BotFather and follow a few simple steps (described here).

Recommendation: Following Hadley's API guidelines it's unsafe to type the TOKEN just in the R script. It's better to use environment variables set in .Renviron file.

So let's say you have named your bot RTelegramBot; you can open the .Renviron file with the R command:

file.edit(path.expand(file.path("~", ".Renviron")))

And put the following line with your TOKEN in your .Renviron:


If you follow the suggested R_TELEGRAM_BOT_ prefix convention you'll be able to use the bot_token function (otherwise you'll have to get these variable from Sys.getenv). Finally, restart R and you can then create the Updater object as:

updater <- Updater(token = bot_token("RTelegramBot"))

Getting Started

To get you started with, we recommend to take a look at its Wiki:

You can also check these other resources:

If you have any other doubt about the package, you can post a question on Stack Overflow under the r-telegram-bot tag or directly e-mail the package's maintainer.


The package is in a starting phase, so contributions of all sizes are very welcome. Please:


This package is inspired by Python's library python-telegram-bot, specially by its submodule telegram.ext.



  • Update processing optimized.
  • Bug affecting callback query answering fixed. Thanks to Nikita Strezhnev for reporting.
  • Documentation update.


  • New S3 methods:
    • Added + method for class TelegramObject, which eases the Updater construction (see The add operator).
    • Added !, & and | methods for class BaseFilter, which enables combining filters (see Advanced Filters).
  • Added destfile parameter for getFile method to download files to a local path.
  • Added username parameter for CommandHandler method with examples.
  • Added ErrorHandler class with examples.
  • Added from_chat_id and from_user_id methods for Update class.
  • Vignettes update:
    • Added The add operator.
    • Substituted Custom filters vignette with Advanced filters.
  • Minor bug fixes.
  • Documentation updated.


  • Fixed a bug that affected sending local files.
  • Renamed Filters instance to MessageFilters in order to avoid masking from utils::Filters object.
  • Added Set a Proxy vignette and examples.
  • Documentation updated.


  • The Bot instance has been totally updated so to be independent from TGBot. Therefore, all its API methods have been implemented, optimized and updated (e.g. adding timeout argument to the getUpdates function, so to use Long Polling). Additionaly, new methods have been added, the full list is:
    • answerCallbackQuery
    • answerInlineQuery
    • deleteMessage
    • deleteWebhook
    • editMessageReplyMarkup
    • forwardMessage
    • getFile
    • getMe
    • getUpdates
    • getUserProfilePhotos
    • getWebhookInfo
    • leaveChat
    • sendAnimation
    • sendAudio
    • sendChatAction
    • sendDocument
    • sendLocation
    • sendMessage
    • sendPhoto
    • sendSticker
    • sendVideo
    • sendVideoNote
    • sendVoice
    • setWebhook
  • Keyboard displaying, which includes parameter reply_markup from sendMessage function with its objects:
    • ReplyKeyboardMarkup
    • InlineKeyboardMarkup
    • ReplyKeyboardRemove
    • ForceReply
  • Support of inline mode with the answerInlineQuery method and the InlineQueryResult object.
  • New request_config parameter for Bot and Updater instances, which allows you to set additional configuration settings to be passed to the bot's POST requests, useful for users who would like to control the default timeouts and/or control the proxy used for http communication.
  • New functions such as clean_updates, set_token, bot_token and user_id (the last 3 inspired by the ones with same name from telegram).
  • Vignettes added:
    • Introduction
    • Building a Bot
    • Basic functionalities
    • Custom filters
  • LICENSE update from LGPL-3 to GPL-3.
  • Examples added for API methods and documentation updated.
  • Other minor fixes.


  • First release on CRAN.

Reference manual

It appears you don't have a PDF plugin for this browser. You can click here to download the reference manual.


2.3.0 by Ernest Benedito, 2 months ago

Report a bug at

Browse source code at

Authors: Ernest Benedito [aut, cre]

Documentation:   PDF Manual  

GPL-3 license

Imports R6, httr, jsonlite, curl

Suggests covr, testthat, knitr, rmarkdown

See at CRAN