Sending messages to a Slack or telegram channel using ESP8266.

I was recently trying to create a presence alert system using an arduino. My first thought was to use two pro-minis with a pair of hc-12 long range transmitters.I only had one pro-mini lying around at the time so this was a no go. Second idea was to use this RF led control board, hack the remote using couple of opto-isolators and reduce the amount of MCU(s) required to just one. After a futile attempt to make the remote work reliably over a long distance (20m). I gave up on this idea too. At last I noticed an unused esp8266 board in my armory so I decided to give it a go.


PART – 1a : Slack With IFTTT.

This method I suppose is best for majority of people reading this post.

IFTTT is an awesome web-service which lets you connect two apps together using api calls hiding all the post/get ugliness from the users.

  • Sign up for IFTTT.
  • Create a new service by searching for webhooks in the IF part.
  • Enter your event name and create a trigger.
  • Select slack in the THEN part. Link your slack account with IFTTT.
  • Fill in the details like channel name , UID etc.
  • After creating the hook, go to webhooks>documentation>Make a POST or GET web request.
  • Copy your webhooks url after filling in the previously entered trigger word.
  • Enter your wifi ssid and password in this part of the code >
#ifndef STASSID
#define STASSID "YOUR_SSID"
#define STAPSK  "YOUR_PASSWORD"
#endif
  • Remove the host part of your webhooks url and copy that into this part of code > ( remove https://maker.ifttt.com form its head)
String url = "YOUR webhooks url without https://maker.ifttt.com part";

And that’s it ! Upload the sketch and wait some time before reciving a push notification on your phone.


Part 1b : Telegram with IFTTT.

  • Create a new service by searching for webhooks in the IF part.
  • Enter your event name and create a trigger.
  • Select telegram in the THEN part. Link your telegram account with IFTTT.
  • Optional -> create a separate telegram channel for your bot. Give IFTTT admin privileges to the channel and select the new channel while configuring the IFTTT trigger.
  • Copy your webhooks url after filling in the previously entered trigger word.
  • Enter your wifi ssid and password in this part of the code >
#ifndef STASSID
#define STASSID "YOUR_SSID"
#define STAPSK  "YOUR_PASSWORD"
#endif
  • Remove the host part of your webhooks url and copy that into this part of code > ( remove https://maker.ifttt.com form its head)
String url = "YOUR webhooks url without https://maker.ifttt.com part";
  • Upload code to the esp board.

Part 2 : Telegram with bot-father.

Using IFTTT as a middle manager adds some bit of (~1.5s) latency to the process. The faster way is to use telegram’s own bot api (botfather) to send messages.

  • Open telegram and search for botfather.
  • Tap start and create a new bot by sending /newbot on the chat.
  • Give your bot a name and a username. After this you will receive your api access keys like this ->
  • Find your user id by searching for IDBot and texting /getid to it.
  • Enter your wifi ssid and password in this part of the code >
#ifndef STASSID
#define STASSID "YOUR_SSID"
#define STAPSK  "YOUR_PASSWORD"
#endif
  • Enter the bot details, user id and your message to this part of the code >
String botId = "your-bot-id";
String msg = "your-message";
  • Upload the code and you are done.

Part 3 : Slack with webHooks.

This is by far the fastest and most reliable method I’ve found. Slack’s bot api has a unique webHooks addon which allows you to post in a slack channel by using a simple https POST request.

  • Login to your slack account and click on create a new app.
  • Give it a name and select your your workspace.
  • In the add features and functionality section select Incoming webhooks.
  • Turn the slider on and select add a new hook to workspace.
  • Select channel and click on allow.
  • You will get a webhook url like this ->
  • Remove the host part of your webhooks url and copy that into this part of code > ( remove https://hooks.slack.com form its head)
String url = "YOUR webhooks url without https://hooks.slack.com";
  • Enter your wifi ssid and password to this part of the code >
#ifndef STASSID
#define STASSID "YOUR_SSID"
#define STAPSK  "YOUR_PASSWORD"
#endif
  • Upload the code and you are done.

Part 4 : Explaining the code.

All the above examples work by sending an https POST or GET request to the host server using an esp8266 library called WifiClientSecure.

Learn more about how http works on esp here.

Now explaining the crucial bits of the code >

const char fingerprint[] PROGMEM = "C1 0D 53 49 D2 3E E5 2B A2 61 D5 9E 6F 99 0D 3D FD 8B B2 B3";

This is the sha-1 fingerprint of the website we are requesting info from. You can get the sha-1 address of any website using https by looking at its ssl certificate. It helps esp know if the website it is visiting is the real deal or a phishing duplicate.

SSL certificate of api.slack.com
  Serial.begin(115200);
  Serial.println();
  Serial.print("connecting to ");
  Serial.println(ssid);
  WiFi.mode(WIFI_STA);
  WiFi.begin(ssid, password);
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
  Serial.println("");
  Serial.println("WiFi connected");
  Serial.println("IP address: ");
  Serial.println(WiFi.localIP());
}

Connects to the wifi network and notifies it in the serial monitor.

WiFiClientSecure client;
client.setFingerprint(fingerprint);

Declares a WifiClientSecure object and sets its fingerprint.

//Slack webhook example.    
client.print(String("POST ") + url + " HTTP/1.1\r\n" +
                 "Host: " + host + "\r\n" +
                 "User-Agent: ArduinoIoT/1.0\r\n" +
                 "Connection: close\r\n" +
                 "Content-Type: application/json\r\n" +
                 "Content-Length: 24\r\n\r\n" +
                 "{\"text\":\"Hello, World!\"}"
                );

This is a simple http post request. You’ll notice every parameter ends with \r\n. And there is double \r\n before sending json data used to specify its beginning to the server.

The json data itself is very different to what we are used to seeing in the modern times. Converting your json data to this format is crucial. Here’s how to do it with a help of a tool called postman.

  • Click on the code button on the right side to display codes in different languages.
  • Select C(libURL) and use this part as your json data in the sketch.

endThoughts.

You can now hookup any sensor module to your esp and program it to trigger a push notification alert on your phone.

The time latency varies among these methods as follows >

1a – IFTTT + Slack~3.2s
1b – IFTTT+ Telegram~3.4s
2 – Telegram bot api~2.5s
3 – Slack bot api~1.8s

All the the code and example sketches for the project is on its github page