/ telegram

Integrating R and Telegram

Hi there!

tl;dr: Some models (deep learning) take a long time to finish. Even some data preparation scripts. We can be notified that the process ended by Telegram sending messages from R.

Get notified by Telegram bot

This section is entirely based on the documentation of telegram.bot package, by Ernest Benedito. Please visit the site to get used of the full capabilities of this package.

The idea of getting notify by Telegram is we can see the notification either on our cellphone or in the web version.

Step 1: Create a bot

Find @BotFather on telegram. Send the message: \start. Then \newbot. And follow the instructions.

Save the bot token and never share publicly.

Step 2: Set-up the bot

After your bot is created. You have to send the message \start. And the bot is finally configurated!

Step 3: Use it with R

Put the bot token in the .Renviron:

user_renviron <- path.expand(file.path("~", ".Renviron"))

This should look something like this:

Now restart R.

# install.packages("telegram.bot")

# Initiate the bot session using the token from the enviroment variable.
bot = Bot(token = bot_token('arbot_bot'))

# The first time, you will need the chat id (which is the chat where you will get the notifications)
updates = bot$getUpdates()
> updates
  update_id message.message_id message.from.id message.from.is_bot message.from.first_name message.from.last_name
1 639401623                  1       174860321               FALSE            admin                 admin
2 639401624                  2       174860321               FALSE            admin                 admin
  message.from.language_code message.chat.id message.chat.first_name message.chat.last_name message.chat.type message.date
1                      en-US       174860321            admin                 admin           private   1540571205
2                      en-US       174860321            admin                 admin           private   1540571208
  message.text  message.entities
1       /start 0, 6, bot_command
2        hello              NULL

Time to use in the R workflow! We will send a test message and a plot:

Note 1: chat_id=message.chat.id.
Note 2: R_TELEGRAM_BOT_{the name of your bot}

# Sending text
message_to_bot=sprintf('Process finished - Accuracy: %s', 0.99)

bot$sendMessage(chat_id = 174860321, text = message_to_bot)

# Sending image (we need to save it first)
my_plot=ggplot(mtcars, aes(x=mpg))  + geom_histogram(bins = 5)
ggplot2::ggsave("my_plot.png", my_plot)

bot$sendPhoto(chat_id = 174860321, photo = 'my_plot.png')

The results on telegram web:

Telegram in R

Note: I also tested: telegram package and it works. However the telegram.bot seems more complete due to the bot options.

Check the full list of options to interact with the bot 🤖.

Get notified by sound

Another way of getting notified is by producing a sound: 🔔 beep!

# install.packages("beepr")

## do some stuff, and...


Thanks for reading 🚀

Blog | Linkedin | Twitter | 📗 Data Science Live Book

Pablo Casas

Pablo Casas

Data Analysis ~ The art of finding order in data by browsing its inner information.

Read More
Integrating R and Telegram
Share this