Skip to content

Instantly share code, notes, and snippets.

@seco
Forked from NaotoKumagai/slack_test.ino
Created April 3, 2017 07:46
Show Gist options
  • Save seco/24ab516652e75decab9bdc28be359a7b to your computer and use it in GitHub Desktop.
Save seco/24ab516652e75decab9bdc28be359a7b to your computer and use it in GitHub Desktop.
SlackBOTのコードを少し改変
// https://github.com/urish/arduino-slack-bot
#include <Arduino.h>
#include <Hash.h>
#include <ESP8266WiFiMulti.h>
#include <ESP8266HTTPClient.h>
#include <WebSocketsClient.h>
#include <ArduinoJson.h>
#define WIFI_SSID "接続したいSSID"
#define WIFI_PASSWORD "SSIDのパスワード"
#define SLACK_SSL_FINGERPRINT "AB F0 5B A9 1A E0 AE 5F CE 32 2E 7C 66 67 49 EC DD 6D 6A 38" // If Slack changes their SSL fingerprint, you would need to update this
#define SLACK_BOT_TOKEN "BOTのトークン"
#define DEFAULT_CHANNEL "投稿したいチャンネルのID"
ESP8266WiFiMulti WiFiMulti;
WebSocketsClient webSocket;
long nextCmdId = 1;
bool connected = false;
bool helloSaid = false;
void webSocketEvent(WStype_t type, uint8_t *payload, size_t len) {
switch (type) {
case WStype_DISCONNECTED:
Serial.printf("[WebSocket] Disconnected :-( \n");
connected = false;
break;
case WStype_CONNECTED:
Serial.printf("[WebSocket] Connected to: %s\n", payload);
break;
case WStype_TEXT:
Serial.printf("[WebSocket] Message: %s\n", payload);
processSlackMessage((char*)payload);
break;
}
}
void processSlackMessage(char *payload) {
StaticJsonBuffer<1000> jsonBuffer;
JsonObject& root = jsonBuffer.parseObject(payload);
if (!root.success()) {
Serial.println("[Json] Parse failed");
return;
}
if(root.containsKey("type")) {
if(strcmp("hello", root["type"]) == 0) {
helloSaid = true;
} else if (strcmp("message", root["type"]) == 0) {
if(root.containsKey("text")) {
if(strcmp("hoge", root["text"]) == 0) {
sendMessage();
}
}
}
}
}
bool connectToSlack() {
// Step 1: Find WebSocket address via RTM API (https://api.slack.com/methods/rtm.start)
HTTPClient http;
http.begin("https://slack.com/api/rtm.start?token=" SLACK_BOT_TOKEN, SLACK_SSL_FINGERPRINT);
int httpCode = http.GET();
if (httpCode != HTTP_CODE_OK) {
Serial.printf("HTTP GET failed with code %d\n", httpCode);
return false;
}
WiFiClient *client = http.getStreamPtr();
client->find("wss:\\/\\/");
String host = client->readStringUntil('\\');
String path = client->readStringUntil('"');
path.replace("\\/", "/");
// Step 2: Open WebSocket connection and register event handler
Serial.println("WebSocket Host=" + host + " Path=" + path);
webSocket.beginSSL(host, 443, path, "", "");
webSocket.onEvent(webSocketEvent);
return true;
}
void sendPing() {
DynamicJsonBuffer jsonBuffer;
JsonObject& root = jsonBuffer.createObject();
root["type"] = "ping";
root["id"] = nextCmdId++;
String json;
root.printTo(json);
webSocket.sendTXT(json);
}
void sendMessage() {
DynamicJsonBuffer jsonBuffer;
JsonObject& root = jsonBuffer.createObject();
root["type"] = "message";
root["channel"] = DEFAULT_CHANNEL;
root["text"] = "fuga";
String json;
root.printTo(json);
webSocket.sendTXT(json);
}
void setup() {
Serial.begin(9600);
while (!Serial) {
delay(100);
}
Serial.setDebugOutput(true);
WiFiMulti.addAP(WIFI_SSID, WIFI_PASSWORD);
while (WiFiMulti.run() != WL_CONNECTED) {
delay(100);
}
configTime(3 * 3600, 0, "pool.ntp.org", "time.nist.gov");
}
unsigned long lastPing = 0;
void loop() {
webSocket.loop();
if (connected) {
// Send ping every 5 seconds, to keep the connection alive
if (millis() - lastPing > 5000) {
if(helloSaid) {
sendPing();
}
lastPing = millis();
}
} else {
// Try to connect / reconnect to slack
connected = connectToSlack();
if (!connected) {
delay(3000);
}
lastPing = millis();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment