Promoting the use of R in the NHS

Blog Article

This post was originally published on this site

(This article was first published on R – Data Science Heroes Blog, and kindly contributed to R-bloggers)
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 notify by Telegram bot

This section is entirely based on the documentation of 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:


This should look something like this:

Integrating R and Telegram

Now restart R.

# install.packages("")

# 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.is_bot message.from.first_name message.from.last_name
1 639401623                  1       174860321               FALSE            admin                 admin
2 639401624                  2       174860321               FALSE            admin                 admin
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:
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:

Integrating R and Telegram

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

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

Get notify by sound

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

# install.packages("beepr")

## do some stuff, and...


Integrating R and Telegram

Thanks for reading 🚀

Blog | Linkedin | Twitter | 📗Data Science Live Book

To leave a comment for the author, please follow the link and comment on their blog: R – Data Science Heroes Blog. offers daily e-mail updates about R news and tutorials on topics such as: Data science, Big Data, R jobs, visualization (ggplot2, Boxplots, maps, animation), programming (RStudio, Sweave, LaTeX, SQL, Eclipse, git, hadoop, Web Scraping) statistics (regression, PCA, time series, trading) and more…

Comments are closed.