<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
  <channel>
    <title>Slack on R Views</title>
    <link>https://rviews.rstudio.com/tags/slack/</link>
    <description>Recent content in Slack on R Views</description>
    <generator>Hugo -- gohugo.io</generator>
    <language>en-us</language>
    <lastBuildDate>Thu, 18 Jun 2020 00:00:00 +0000</lastBuildDate>
    <atom:link href="https://rviews.rstudio.com/tags/slack/" rel="self" type="application/rss+xml" />
    
    
    
    
    <item>
      <title>R Can Pull the Fire Alarm!</title>
      <link>https://rviews.rstudio.com/2020/06/18/how-to-have-r-notify-you/</link>
      <pubDate>Thu, 18 Jun 2020 00:00:00 +0000</pubDate>
      
      <guid>https://rviews.rstudio.com/2020/06/18/how-to-have-r-notify-you/</guid>
      <description>
        

&lt;p&gt;&lt;em&gt;Brian Law is a customer success representative at RStudio and a new R Views contributor.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Jason Rich manages all US Data Engineering for PRA Group and studies computer science at Old Dominion University&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;There are times when it would be really nice to get an email from R. Maybe you have a long running job that you would just like to leave alone while you go off and do other things. When this happens, it would be nice if R notified you when it was done; something like calling out &amp;ldquo;dinner&amp;rsquo;s ready!&amp;rdquo;, but for work. Other times you may be monitoring a process for anomalies. Usually, everything&amp;rsquo;s fine, except when it&amp;rsquo;s not, and then we want a &amp;ldquo;fire alarm&amp;rdquo; to go off in R. Below we&amp;rsquo;ll walk through how to automate having R send you an: email, text message, &lt;code&gt;Slack&lt;/code&gt; message, or &lt;code&gt;Microsoft Teams&lt;/code&gt; message.&lt;/p&gt;

&lt;h3 id=&#34;email&#34;&gt;Email&lt;/h3&gt;

&lt;pre&gt;&lt;code class=&#34;language-r&#34;&gt;library(blastula)
# First let&#39;s build a rich HTML email using library(blastula)
owl &amp;lt;- compose_email(body = md(c(&amp;quot;Hello from Hogwarts. &amp;lt;br&amp;gt;&amp;lt;br&amp;gt; The polyjuice potion is complete!&amp;quot;)))
# Second, store credentials to later send our message via smtp (using gmail here but could be others)
create_smtp_creds_file(
  file = &amp;quot;gmail_creds&amp;quot;,
  user = &amp;quot;name@gmail.com&amp;quot;,
  provider = &amp;quot;gmail&amp;quot;
) # Note, a pop up will ask for the pwd for the user you provided
# Third, send the email (using gmail here but could be others)
owl %&amp;gt;%
  smtp_send(
    to = &amp;quot;someone@email.com&amp;quot;,
    from = &amp;quot;name@gmail.com&amp;quot;,
    subject = &amp;quot;Mischief Managed&amp;quot;,
    credentials = creds_file(&amp;quot;gmail_creds&amp;quot;)
  )
&lt;/code&gt;&lt;/pre&gt;

&lt;h3 id=&#34;text&#34;&gt;Text&lt;/h3&gt;

&lt;p&gt;If you prefer to get notifications via text messages then you can use the old trick to send an email that will convert into a text message to a phone number. Each mobile phone plan provider has a slightly different format for how to do this and so the first step is to google &amp;ldquo;how to send email to text&amp;rdquo; and look yours up, e.g. AT&amp;amp;T&amp;rsquo;s is the &lt;code&gt;ten-digit-phone-number@mms.att.net&lt;/code&gt;. Let&amp;rsquo;s run through an example.&lt;/p&gt;

&lt;pre&gt;&lt;code class=&#34;language-r&#34;&gt;owl2 &amp;lt;- &amp;quot;&amp;quot;
owl2 %&amp;gt;%
  smtp_send(
    to = &amp;quot;xxxxxxxxxx@mms.att.net&amp;quot;, 
    from = &amp;quot;name@gmail.com&amp;quot;,
    subject = &amp;quot;Mischief Managed&amp;quot;,
    credentials = creds_file(&amp;quot;gmail_creds&amp;quot;)
  )
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Note that the text message above will only render the subject line currently but you can tinker further.&lt;/p&gt;

&lt;h3 id=&#34;slack&#34;&gt;Slack&lt;/h3&gt;

&lt;p&gt;Who knew that chat rooms would make such a comeback? If you use &lt;code&gt;Slack&lt;/code&gt; and want to send a message there, you can do so using R. Here we&amp;rsquo;ll walk through a stripped down method that posts messages directly to the &lt;code&gt;Slack&lt;/code&gt; API using an RStudio library &lt;code&gt;httr&lt;/code&gt;. If you want to get fancy there is also &lt;code&gt;library(slackr)&lt;/code&gt;, which has more control and features.&lt;/p&gt;

&lt;p&gt;There are a few steps to set things up. First, you will need a Slack account. Second, go to &lt;code&gt;api.slack.com/apps&lt;/code&gt; and login.&lt;/p&gt;

&lt;p&gt;&lt;img src=&#34;slack_1.png&#34; height = &#34;400&#34; width=&#34;600&#34;&gt;&lt;/p&gt;

&lt;p&gt;Third, click on &amp;ldquo;Create New App&amp;rdquo;. Fourth, on the left sidebar, click on &amp;ldquo;Incoming Webhooks&amp;rdquo; and make sure &amp;ldquo;Activate Incoming Webhooks&amp;rdquo; is &amp;ldquo;On&amp;rdquo;.&lt;/p&gt;

&lt;p&gt;&lt;img src=&#34;slack_2.png&#34; height = &#34;400&#34; width=&#34;600&#34;&gt;&lt;/p&gt;

&lt;p&gt;Then, scroll to the bottom of the page and click &amp;ldquo;Add New Webhook to WorkSpace&amp;rdquo;.&lt;/p&gt;

&lt;p&gt;&lt;img src=&#34;slack_3.png&#34; height = &#34;400&#34; width=&#34;600&#34;&gt;&lt;/p&gt;

&lt;p&gt;Next, choose what &lt;code&gt;Slack&lt;/code&gt; channel you want to generate a webhook to connect with, for example, your own &lt;code&gt;Slack&lt;/code&gt; username, or a more general &lt;code&gt;Slack&lt;/code&gt; channel at your company, like &amp;ldquo;cat_photos&amp;rdquo;.&lt;/p&gt;

&lt;p&gt;&lt;img src=&#34;slack_4.png&#34; height = &#34;400&#34; width=&#34;600&#34;&gt;&lt;/p&gt;

&lt;p&gt;Lastly, click on the &amp;ldquo;Copy&amp;rdquo; button to copy the webhook, which is what we&amp;rsquo;ll use in our R code below to actually send the message.&lt;/p&gt;

&lt;p&gt;&lt;img src=&#34;slack_3.png&#34; height = &#34;400&#34; width=&#34;600&#34;&gt;&lt;/p&gt;

&lt;pre&gt;&lt;code class=&#34;language-r&#34;&gt;library(httr)
test_msg &amp;lt;- list(text=&amp;quot;hello world!&amp;quot;)
hook_to_me &amp;lt;- &amp;quot;https://hooks.slack.com/services/some_long_hash&amp;quot;
POST(hook_to_me, encode = &amp;quot;json&amp;quot;, body = test_msg)
if (2 &amp;lt; 3) { # make this conditional aka a &amp;quot;fire alarm&amp;quot;
  hook_to_cats_channel &amp;lt;- &amp;quot;https://hooks.slack.com/services/some_long_hash&amp;quot;
  POST(hook_to_cats_channel, encode = &amp;quot;json&amp;quot;, body = test_msg)
}
&lt;/code&gt;&lt;/pre&gt;

&lt;h3 id=&#34;microsoft-teams&#34;&gt;Microsoft Teams&lt;/h3&gt;

&lt;p&gt;The process of connecting to &lt;code&gt;Microsoft Teams&lt;/code&gt; in similar to connecting to &lt;code&gt;Slack&lt;/code&gt; under the covers, using a webhook and leveraging the incoming webhook app, installed from the &lt;code&gt;Teams&lt;/code&gt; app store. To get started, let&amp;rsquo;s install the &lt;code&gt;teamr&lt;/code&gt; package from &lt;code&gt;CRAN&lt;/code&gt;, call the library and create our connection.&lt;/p&gt;

&lt;p&gt;Just like with &lt;code&gt;Slack&lt;/code&gt;, you will first need an organizational &lt;code&gt;Teams&lt;/code&gt; account. Secondly, navigate to the &lt;code&gt;add more apps&lt;/code&gt; button from within the desktop client, which is the ellipsis below the pinned apps on the left task bar.&lt;/p&gt;

&lt;p&gt;&lt;img src=&#34;teams_6.png&#34; height = &#34;400&#34; width=&#34;600&#34;&gt;&lt;/p&gt;

&lt;p&gt;Third, use the search bar, located in the upper left corner to search for the &lt;code&gt;Incoming Webhook&lt;/code&gt; app&lt;/p&gt;

&lt;p&gt;&lt;img src=&#34;teams_1.png&#34; height = &#34;400&#34; width=&#34;600&#34;&gt;&lt;/p&gt;

&lt;p&gt;click on the icon to bring up the install screen, where you can name the webhook, and assign it a specific channel within &lt;code&gt;Teams&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;img src=&#34;teams_2.png&#34; height = &#34;400&#34; width=&#34;600&#34;&gt;&lt;/p&gt;

&lt;p&gt;After following the prompts to name the connection and assign it to a channel, you are given the option of uploading an image so the webhook is easily recognizable when notifications are posted to your channel. Here, I have chosen to use an &lt;code&gt;RStudio&lt;/code&gt; logo, which we will see later,  makes it easier to know who, and/or from where, the post originates. After you are satisfied, click the done button in the bottom left corner.&lt;/p&gt;

&lt;p&gt;&lt;img src=&#34;teams_4.png&#34; height = &#34;400&#34; width=&#34;600&#34;&gt;&lt;/p&gt;

&lt;p&gt;The code to test your webhook in reasonably simple, and requires only six lines of code. Although, in practice, we leverage many of, if not all, the customizable features provided within the &lt;code&gt;teamr&lt;/code&gt; package. You can read more about these  feature either in the official documentation or on the &lt;code&gt;GitHub&lt;/code&gt; page &lt;a href=&#34;https://github.com/wwwjk366/teamr&#34;&gt;https://github.com/wwwjk366/teamr&lt;/a&gt;&lt;/p&gt;

&lt;pre&gt;&lt;code class=&#34;language-r&#34;&gt;library(teamr)
con &amp;lt;- connector_card$new(hookurl = &amp;quot;https://outlook.office.com/webhook/...&amp;quot;)
con$text(&amp;quot;This is the notification body!&amp;quot;)
con$title(&amp;quot;Message Title&amp;quot;)
con$add_link_button(&amp;quot;The GitHub repo for the teamr package&amp;quot;, &amp;quot;https://github.com/wwwjk366/teamr&amp;quot;)
con$send()
#[1] TRUE
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;The final step, after running this code, is to verify everything is working as it should. The above code snippet sends a notification to &lt;code&gt;Teams&lt;/code&gt;, with a link button pointing the package&amp;rsquo;s GitHub repo, from a webhook bot containing the &lt;code&gt;RStudio&lt;/code&gt; logo.&lt;/p&gt;

&lt;p&gt;&lt;img src=&#34;teams_5.png&#34; height = &#34;400&#34; width=&#34;600&#34;&gt;&lt;/p&gt;

&lt;p&gt;We hope you found this discussion helpful. The next time you wish there was a way to have &lt;code&gt;R&lt;/code&gt; notify you, you can incorporate one of these into your workflow.&lt;/p&gt;

        &lt;script&gt;window.location.href=&#39;https://rviews.rstudio.com/2020/06/18/how-to-have-r-notify-you/&#39;;&lt;/script&gt;
      </description>
    </item>
    
  </channel>
</rss>
