apple2idiot/examples/slack/ArduinoSlack.h

113 lines
2.8 KiB
C
Raw Normal View History

2021-09-11 23:48:28 +00:00
/*
Copyright (c) 2020 Brian Lough. All right reserved.
ArduinoSlack - An Arduino library to wrap the Slack API
MIT License
*/
#ifndef ArduinoSlack_h
#define ArduinoSlack_h
#include <Arduino.h>
#include <ArduinoJson.h>
#include <Client.h>
#define SLACK_ENABLE_SERIAL
//un-mark following line to enable debug mode
#define SLACK_ENABLE_DEBUG
#ifdef SLACK_ENABLE_SERIAL
#define SLACK_SERIAL(STR) Serial.print(STR)
#define SLACK_SERIAL_LN(STR) Serial.println(STR)
#else
#define SLACK_SERIAL(STR)
#define SLACK_SERIAL_LN(STR)
#endif
#ifdef SLACK_ENABLE_DEBUG
#define SLACK_DEBUG_SERIAL(STR) Serial.print(STR)
#define SLACK_DEBUG_SERIAL_LN(STR) Serial.println(STR)
#else
#define SLACK_DEBUG_SERIAL(STR)
#define SLACK_DEBUG_SERIAL_LN(STR)
#endif
#define SLACK_HOST "slack.com"
// Fingerprint valid from Tue, 13 Apr 2021 00:00:00 GMT
#define SLACK_FINGERPRINT "C3 CC ED 77 87 19 6D E7 76 5E AA A7 3D 67 7E CA 95 D2 46 E2"
#define SLACK_TIMEOUT 2000
#define SLACK_PRESENCE_AWAY "away"
#define SLACK_PRESENCE_AUTO "auto"
#define SLACK_USERS_PROFILE_SET_ENDPOINT "/api/users.profile.set"
#define SLACK_USERS_SET_PRESENCE_ENDPOINT "/api/users.setPresence?presence=%s"
#define SLACK_POST_MESSAGE_ENDPOINT "/api/chat.postMessage"
#define SLACK_CONVERSATIONS_HISTORY_ENDPOINT "/api/conversations.history"
2021-09-25 20:26:08 +00:00
#define SLACK_USERS_CONVERSATIONS_ENDPOINT "/api/users.conversations"
2021-09-11 23:48:28 +00:00
struct SlackProfile
{
char *displayName;
char *statusText;
char *statusEmoji;
int statusExpiration;
bool error;
};
struct SlackMessage
{
char *text;
char *username;
char *bot_id;
char *type;
char *ts;
bool error;
};
2021-09-25 20:26:08 +00:00
struct SlackUsersConversations
2021-09-11 23:48:28 +00:00
{
2021-09-25 20:26:08 +00:00
JsonObject channelsObj;
//String channelNames[22];
//String channelIds[22];
char *channelNames[17];
char *channelIds[17];
2021-09-11 23:48:28 +00:00
bool error;
};
class ArduinoSlack
{
public:
ArduinoSlack(Client &client, const char *bearerToken);
2021-09-25 20:26:08 +00:00
int makeGetRequest(const char *command);
2021-09-11 23:48:28 +00:00
int makePostRequest(const char *command, const char *body, const char *contentType = "application/json");
SlackProfile setCustomStatus(const char *text, const char *emoji, int expiration = 0);
SlackMessage postMessage(const char *channel, const char *text);
2021-09-25 20:26:08 +00:00
SlackUsersConversations usersConversations();
2021-09-11 23:48:28 +00:00
bool setPresence(const char *presence);
int portNumber = 443;
int profileBufferSize = 10000;
Client *client;
private:
const char *_bearerToken;
int getHttpStatusCode();
void skipHeaders(bool tossUnexpectedForJSON = true);
void closeClient();
2021-09-25 20:26:08 +00:00
const char *usersConversationsGET =
R"(%s?exclude_arc=True&limit=1)";
2021-09-11 23:48:28 +00:00
const char *setConvoHistoryBody =
R"({"channel": "%s", "limit": "%s"})";
const char *setMessageBody =
R"({"channel": "%s", "text": "%s"})";
const char *setEndpointBody =
R"({"profile": { "status_text": "%s","status_emoji": "%s","status_expiration": %d}})";
};
#endif