<p><div><div><div><div data-show-faces="false" data-height="21" data-width="100" data-layout="button_count" data-send="true" data-href="https://www.r-bloggers.com/integrating-r-and-telegram/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="https://blog.datascienceheroes.com/get-notify-when-an-r-script-finishes-on-telegram/"> R - Data Science Heroes Blog</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<div><img alt="Integrating R and Telegram" src="https://i2.wp.com/blog.datascienceheroes.com/content/images/2018/11/Screen-Shot-2018-11-07-at-11.04.09.png?w=456&ssl=1" data-recalc-dims="1">
<p>Hi there!</p>
<p>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 <a rel="nofollow" href="https://telegram.org/" target="_blank">Telegram</a> sending messages from R.</p>
<h2>Get notify by Telegram bot</h2>
<p>This section is entirely based on the documentation of <a rel="nofollow" href="https://ebeneditos.github.io/telegram.bot/" target="_blank">telegram.bot</a> package, by Ernest Benedito. Please visit the site to get used of the full capabilities of this package.</p>
<p>The idea of getting notify by Telegram is we can see the notification either on our cellphone or in the <a rel="nofollow" href="https://web.telegram.org/" target="_blank">web version</a>.</p>
<h3>Step 1: Create a bot</h3>
<p>Find <code>@BotFather</code> on telegram. Send the message: <code>\start</code>. Then <code>\newbot</code>. And follow the instructions.</p>
<p>Save the bot token and never share publicly.</p>
<h3>Step 2: Set-up the bot</h3>
<p>After your bot is created. You have to send the message <code>\start</code>. And the bot is finally configurated!</p>
<h3>Step 3: Use it with R</h3>
<p>Put the bot token in the <code>.Renviron</code>:</p>
<pre><code>user_renviron &lt;- path.expand(file.path(&quot;~&quot;, &quot;.Renviron&quot;))
file.edit(user_renviron) 
</code></pre>
<p>This should look something like this:</p>
<p><img alt="Integrating R and Telegram" src="https://i0.wp.com/blog.datascienceheroes.com/content/images/2018/10/token.png?w=456&ssl=1" data-recalc-dims="1"></p>
<p>Now restart R.</p>
<pre><code># install.packages(&quot;telegram.bot&quot;)
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()
</code></pre>
<pre><code>> 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
</code></pre>
<p><strong>Time to use in the R workflow!</strong> We will send a test message and a plot:</p>
<p>Note 1: <code>chat_id</code>=<code>message.chat.id</code>.<br>
Note 2: <code>R_TELEGRAM_BOT_{the name of your bot}</code></p>
<pre><code># 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(&quot;my_plot.png&quot;, my_plot)

bot$sendPhoto(chat_id = 174860321, photo = 'my_plot.png')
</code></pre>
<p>The results on telegram web:</p>
<p><img alt="Integrating R and Telegram" src="https://i0.wp.com/blog.datascienceheroes.com/content/images/2018/10/Screen-Shot-2018-10-27-at-11.44.48.png?w=456&ssl=1" data-recalc-dims="1"></p>
<p>Note: I also tested: <a rel="nofollow" href="https://github.com/lbraglia/telegram" target="_blank">telegram</a> package and it works. However the <a rel="nofollow" href="https://ebeneditos.github.io/telegram.bot/" target="_blank">telegram.bot</a> seems more complete due to the bot options.</p>
<p>Check the full list of options to interact with the bot <img alt="🤖" src="https://s.w.org/images/core/emoji/2.4/72x72/1f916.png" class="wp-smiley">.</p>
<p></p>
<h2>Get notify by sound</h2>
<p>Another way of getting notified is by producing a sound: <img alt="🔔" src="https://s.w.org/images/core/emoji/2.4/72x72/1f514.png" class="wp-smiley"> <em>beep</em>!</p>
<pre><code># install.packages(&quot;beepr&quot;)
library(beepr)

## do some stuff, and...

beep()
beep()
</code></pre>
<p><img alt="Integrating R and Telegram" src="https://i0.wp.com/blog.datascienceheroes.com/content/images/2018/10/Screen-Shot-2018-10-27-at-12.14.42.png?w=300&ssl=1" data-recalc-dims="1"></p>
<hr>
<p>Thanks for reading <img alt="🚀" src="https://s.w.org/images/core/emoji/2.4/72x72/1f680.png" class="wp-smiley"></p>
<p><a rel="nofollow" href="https://blog.datascienceheroes.com/get-notify-when-an-r-script-finishes-on-telegram/blog.datascienceheroes.com" target="_blank">Blog</a> | <a rel="nofollow" href="https://www.linkedin.com/in/pcasas/" target="_blank">Linkedin</a> | <a rel="nofollow" href="https://twitter.com/pabloc_ds" target="_blank">Twitter</a> | <img alt="📗" src="https://s.w.org/images/core/emoji/2.4/72x72/1f4d7.png" class="wp-smiley"> <a rel="nofollow" href="http://livebook.datascienceheroes.com/" target="_blank">Data Science Live Book</a></p>
</div>
	<p>
<div>
<div>To <strong>leave a comment</strong> for the author, please follow the link and comment on their blog: <strong><a href="https://blog.datascienceheroes.com/get-notify-when-an-r-script-finishes-on-telegram/"> R - Data Science Heroes Blog</a></strong>.</div>
<hr>
<a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers.com</a> offers <strong><a rel="nofollow" href="https://feedburner.google.com/fb/a/mailverify?uri=RBloggers">daily e-mail updates</a></strong> about <a rel="nofollow" href="https://www.r-project.org/" title="The R Project for Statistical Computing">R</a> news and <a rel="nofollow" href="https://www.r-bloggers.com/search/tutorial" title="R tutorials">tutorials</a> on topics such as: <a rel="nofollow" href="https://www.r-bloggers.com/search/data%20science" title="Data science">Data science</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/Big%20Data" title="Big Data">Big Data, <a rel="nofollow" href="https://www.r-users.com/" title="R jobs">R jobs</a>, visualization (<a rel="nofollow" href="https://www.r-bloggers.com/search/ggplot2" title="ggplot and ggplot2 tutorials">ggplot2</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/boxplot" title="Boxplots using lattice and ggplot2 tutorials">Boxplots</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/map" title="Maps and gis">maps</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/animation" title="Animation in R">animation</a>), programming (<a rel="nofollow" href="https://www.r-bloggers.com/search/RStudio" title="RStudio IDE for R">RStudio</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/sweave" title="Sweave and literate programming">Sweave</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/LaTeX" title="LaTeX in R">LaTeX</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/SQL" title="SQL and databases">SQL</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/eclipse" title="Eclipse IDE for R">Eclipse</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/git" title="git and github, Version Control System">git</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/hadoop" title="Large data in R using Hadoop">hadoop</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/Web+Scraping" title="Web Scraping of google, facebook, yahoo, twitter and more using R">Web Scraping</a>) statistics (<a rel="nofollow" href="https://www.r-bloggers.com/search/regression" title="Regressions and ANOVA analysis tutorials">regression</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/PCA" title="principal component analysis tutorial">PCA</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/time+series" title="Time series">time series</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/trading" title="finance trading">trading</a>) and more...
<img width="1" alt="" src="http://feeds.feedburner.com/~r/RBloggers/~4/EDYL32cycCY" height="1"></a></div>