Pepper Slack messenger

Sending Slack messages by voice thanks to Pepper
Pepper Slack messenger

What if Pepper could help you increase your productivity at work? By linking the robot to a Slack channel, we will be able to send and receive messages by voice control thanks to Pepper.

Sending Slack messages via Pepper’s chatbot

This article is part of the API Challenge: Quentin, an intern at SBRE, is plugging various third-party technologies (web services or IoT devices) into Pepper. For each technology, he has one week to test it, make a demo on Pepper, and write up his experience. This is a way of checking how easy it is to integrate technology with Pepper.

How much time do you spend at work responding to your emails and messages on the different work-related channels you use? Wouldn’t it be nice to be able to answer these messages by voice while working on more important stuff? Well, thanks to Pepper, this is now something you can do!

For my second mission in the course of the API challenge, I granted my Pepper the ability to send messages I speak to a slack channel. This enhanced my understanding of the different web requests protocols (REST and WebSocket) and credentials handling. It also allowed me to build a sweet and pretty handy application for my personal Pepper, thus extending its abilities along with my experience. Here’s how I proceeded...

1. The Slack API, creating a Slack App and sending messages with Pepper

Slack has an inbuilt and yet simple API that is pretty helpful if you want to create apps functioning inside Slack such as chatbots or calendar apps.

I first created a dedicated Slack workspace and channel, not to pollute my coworker's workspace with tests and added an app to that workspace.

After a bit of research on the Slack API’s documentation, looking for a way to create an access between an Android Studio app and this Slack channel and to give the channel’s credentials to my app. I found a pretty easy and direct way to proceed. Indeed, Slack allows you to create, via the app, special URLs called Incoming Webhooks where your Android app can send REST requests in the form of JSON files to the slack app. I used Retrofit to pass a Post request to the webhook URL, similarly to what I did with the Worldwide holiday manager API on the previous article of the API challenge.

class SlackPepperAccess {
    const val urlBase = ""
    //create the retrofit client as usual

    interface SlackPepperPost {
        @Headers("Content-Type: application/json")
        fun postSlackMessage(@Body text: SlackMessage ): Call<ResponseBody>

When a message is sent from a webhook to the channel, a response is always sent back, even if the message wasn’t correctly sent. This is pretty useful to verify if everything worked as it should. I used this response in a test class. When everything works fine, the response body should always be a single String: “ok”.

val response = slackPepperAccess.send("hello my dudes!")
assert(response.toString() == "ok")

Thus, I managed pretty simply to send messages to the channel with voice-command, using a qiChat chatbot and an executor to link my code to it, as usual. If you don’t know how to do that, you should check on the Dev Center’s lesson about linking Qichat and code. And that’s it!

2. Going further: making a dynamic app, alerting you when messages are received

Now, thanks to this little app, I don’t have to type my Slack messages anymore which is perfect for a lazy interns like me! But why stop there?

What could be done next would be to enable Pepper to dynamically alert me whenever a user posts a message on the channel and then ask me if I want to answer. Unfortunately, this kind of dynamic interaction between two apps can hardly be done using REST requests, especially since an update from the Slack API which stopped supporting outgoing webhooks. Instead, the Slack API offers two different solutions:

  • the Events API;
  • the Real Time Manager (RTM) API.

The Event API is used to create a link between the Slack API and an online service located under another URL, this is not our case. So what about the RTM API? It is a WebSocket-based API used to create a “bridge” between your app and the Slack API. If you don’t know what a WebSocket is, this article explains it pretty well.

But, before starting to look for online libraries to use the RTM API on android studio, there is another issue to handle: how to provide credentials (unlike when using the Webhook, which uses a simple auto-generated URL)

I had to dig up a few information in the Slack API documentation. For authentication, Slack uses the OAuth 2.0 protocol. It is a token-based protocol which means that you need a special authentication token to enable the WebSocket connection. I obtained it by accessing a URL containing the app’s client id found in the app’s basic information, the team id found in the workspace information and the scopes you want your app to access.

val urlAuth = "$client_id&team=$team&install_redirect=oauth&scope=channels:history,chat:write:user"

The final step would be to open the WebSocket and the RTM connexion and to ensure that this connection is correctly made before sending and receiving messages from a Slack channel and you’ll never have to open your Slack tab again!

If you’re willing to learn how this can be done, I invite you to check on the Slack documentation about the RTM API and on these java libraries I found on github:

The list of the references that has been used to solve this API Challenge