Learn R Programming

⚠️There's a newer version (3.0.0) of this package.Take me there.

telegram.bot

Develop a Telegram Bot with R

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.

Installation

You can install telegram.bot from CRAN:

install.packages("telegram.bot")

Or the development version from GitHub:

# install.packages("devtools")
devtools::install_github("ebeneditos/telegram.bot")

Make sure you have the devtools package updated.

Usage

The telegram.bot package is easy, fun and free to use! You can quickly build a chatbot with a few lines:

library(telegram.bot)

start <- function(bot, update){
  bot$sendMessage(chat_id = update$message$chat_id,
                  text = sprintf("Hello %s!", update$message$from$first_name))
}

updater <- Updater('YOUR TOKEN HERE')

updater$dispatcher$add_handler(CommandHandler('start', start))

updater$start_polling()

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 = bot_token('RBot'))
chat_id <- user_id('me')

# Get bot info
bot$getMe()

# Get updates
updates <- bot$getUpdates()

# Send message
bot$sendMessage(chat_id = chat_id,
                text = '*foo bold text*',
                parse_mode = 'Markdown')

# Send photo
bot$sendPhoto(chat_id = chat_id,
               photo = "https://telegram.org/img/t_logo.png")

# Send audio
bot$sendAudio(chat_id = chat_id,
              audio = "http://www.largesound.com/ashborytour/sound/brobob.mp3")

# Send document
bot$sendDocument(chat_id = chat_id,
                 document = "https://github.com/ebeneditos/telegram.bot/raw/gh-pages/docs/telegram.bot.pdf")

# Send sticker
bot$sendSticker(chat_id = chat_id,
                sticker = "https://www.gstatic.com/webp/gallery/1.webp")

# Send video
bot$sendVideo(chat_id = chat_id,
              video = "http://techslides.com/demos/sample-videos/small.mp4")

# Send gif
bot$sendAnimation(chat_id = chat_id,
                  animation = "https://media.giphy.com/media/sIIhZliB2McAo/giphy.gif")

# 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
bot$getUserProfilePhotos(user_id = chat_id)

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()).

Getting Started

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

You can also check these other resources:

Contributing

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

Attribution

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

Copy Link

Version

Install

install.packages('telegram.bot')

Monthly Downloads

504

Version

2.0.0

License

GPL-3

Issues

Pull Requests

Stars

Forks

Maintainer

Ernest Benedito

Last Published

November 10th, 2018

Functions in telegram.bot (2.0.0)

answerCallbackQuery

answerCallbackQuery
InlineQueryResult

InlineQueryResult
deleteMessage

deleteMessage
sendDocument

sendDocument
ForceReply

ForceReply
Filters

Filters
Update

Update
add_handler

add_handler
add_error_handler

add_error_handler
Updater

Updater
sendLocation

sendLocation
Handler

Handler
deleteWebhook

deleteWebhook
editMessageReplyMarkup

editMessageReplyMarkup
effective_chat

effective_chat
Bot

Bot
effective_message

effective_message
InlineKeyboardButton

InlineKeyboardButton
ReplyKeyboardRemove

ReplyKeyboardRemove
sendAudio

sendAudio
ReplyKeyboardMarkup

ReplyKeyboardMarkup
bot_token

bot_token
getWebhookInfo

getWebhookInfo
CallbackQueryHandler

CallbackQueryHandler
sendChatAction

sendChatAction
handle_update

handle_update
check_update

check_update
sendVideoNote

sendVideoNote
leaveChat

leaveChat
sendAnimation

sendAnimation
setWebhook

setWebhook
getUpdates

getUpdates
set_token

set_token
CommandHandler

CommandHandler
Dispatcher

Dispatcher
KeyboardButton

KeyboardButton
MessageHandler

MessageHandler
effective_user

effective_user
getUserProfilePhotos

getUserProfilePhotos
telegram.bot

telegram.bot
sendMessage

sendMessage
sendPhoto

sendPhoto
user_id

user_id
forwardMessage

forwardMessage
sendVoice

sendVoice
getFile

getFile
getMe

getMe
sendSticker

sendSticker
sendVideo

sendVideo
start_polling

start_polling
stop_polling

stop_polling
InlineKeyboardMarkup

InlineKeyboardMarkup
clean_updates

clean_updates
answerInlineQuery

answerInlineQuery