Integrating R and Telegram

[This article was first published on R - Data Science Heroes Blog, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

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 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"))
file.edit(user_renviron) 

This should look something like this:

Now restart R.

# install.packages("telegram.bot")
library(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)
library(ggplot2)
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 telegram.bot 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")
library(beepr)

## do some stuff, and...

beep()
beep()

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.

R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)