A simple to use Discord webhooks wrapper made using Arduino C
- Basic implementation
- Simple docs
- Add more detailed examples
- Send multiple embeds
- General optimization
- A better README file...
I won't bother keeping track of things that change so I'll just write whatever is new:
- Now you can send multiple embeds.
- Added multiple embeds example.
- Made embed functions return the current object instead of the pointer.
-
Download and install the library
-
Create a webhook:
-
Now that you have your webhook URL, start coding!
-
Include the needed libraries
#include <ESP8266WiFi.h> #include <ESP8266WiFiMulti.h> #include "DiscordWebhook.h"
Set your credentails
#define SSID "<your ssid>" #define PASS "<your password>" #define DISCORD_WEBHOOK_URL "<your discord webhook url>"
Define your variables and connect to WiFi
ESP8266WiFiMulti wifi_multi; DiscordWebhook discord_webhook; void connect_to_wifi() { WiFi.mode(WIFI_STA); wifi_multi.addAP(SSID, PASS); Serial.printf("Connecting to %s\n", SSID); while (wifi_multi.run() != WL_CONNECTED) delay(50); Serial.println("Connected to WiFi"); }
Run setup
void setup() { Serial.begin(115200); connect_to_wifi(); discord_webhook.begin(DISCORD_WEBHOOK_URL); ...
Start sending webhooks
bool success = discord_webhook.send("Hello World!"); Serial.printf("Sending webhook %s\n", success ? "succeeded!" : "failed."); } // end of setup
Full code:
#include <ESP8266WiFi.h> #include <ESP8266WiFiMulti.h> #include "DiscordWebhook.h" #define SSID "<your ssid>" #define PASS "<your password>" #define DISCORD_WEBHOOK_URL "<your discord webhook url>" ESP8266WiFiMulti wifi_multi; DiscordWebhook discord_webhook; void connect_to_wifi() { WiFi.mode(WIFI_STA); wifi_multi.addAP(SSID, PASS); Serial.printf("Connecting to %s\n", SSID); while (wifi_multi.run() != WL_CONNECTED) delay(50); Serial.println("Connected to WiFi"); } void setup() { Serial.begin(115200); connect_to_wifi(); discord_webhook.begin(DISCORD_WEBHOOK_URL); bool success = discord_webhook.send("Hello World!"); Serial.printf("Sending webhook %s\n", success ? "succeeded!" : "failed."); } void loop() { }
-
void setup() { Serial.begin(115200); connect_to_wifi(); discord_webhook.begin(DISCORD_WEBHOOK_URL); DiscordEmbed embed; embed.setTitle("Weather Forecast") .setColor(DiscordEmbed::rgbToHex(225, 232, 237)) .addField("Now", "Clear π", false) .addField("6:00", "Sunny β", true) .addField("7:00", "Cloudy β", true); EmbedField field1; field1.name = "8:00"; field1.value = "Rainy π§"; field1.is_inline = true; embed.addField(field1); EmbedFooter footer; footer.text = "Watch out for a possible snowfall"; embed.setFooter(footer); bool success = discord_webhook.send(embed);
bool success = discord_webhook.send("Hey @everyone, check out the forecast!", embed); Serial.printf("Sending webhook %s\n", success ? "succeeded!" : "failed."); }
-
Embed author structure can be found here
// example EmbedAuthor author;
-
String name
Author name.
-
String url
Author url.
-
String icon_url
Author icon url (often the author's pfp).
-
-
Embed footer structure can be found here
// example EmbedFooter footer;
-
String text
Embed footer text.
-
String icon_url
Embed footer icon url.
-
-
Embed field structure can be found here
// example EmbedField field;
-
String name
Embed field name (more like title)
-
String value
Embed field value (more like content)
-
-
Embed image structure can be found here
// example EmbedImage image;
-
String url
Embed image source url
-
String proxy_url
Embed image proxied url
-
unsigned int height
Embed image height
-
unsigned int width
Embed image width
-
-
Embed structure can be found here
// example DiscordEmbed embed;
-
static const size_t embed_size = 4096;
Determines the
DynamicJsonDocument
size which is a member variable used to store all json data related to the embed you're currently creating.NOTE: Discord embeds have predetermined limits and the default embed size in the program is way less than the limits (for memory purposes). You can find the embed limits here.
-
DiscordEmbed setTitle(const String &title)
const String embed_title = "Door Alarm"; embed.setTitle(embed_title);
Sets the embed title.
params:
title
: the embed title, ofc
returns:
- current embed
-
DiscordEmbed setTitle(const String &title, const String &url)
const String embed_title = "Door Alarm"; const String embed_url = "https://my-website.com/door-alarm/status"; embed.setTitle(embed_title, embed_url);
Sets the embed title with url, hence making it a redirectable title link. For example:
(pretend this is an embed)
Door Alarm embed description some embed fields params:
-
title
: the embed title -
url
: redirect url
returns:
- current embed
-
-
DiscordEmbed setDescription(const String &description)
// example const String embed_desc = "Alarm didn't get triggered in the past 3 hours"; embed.setDescription(embed_desc)'
Sets the embed description, the thing below the title.
params:
description
: whatever text you want to put here, but make sure it doesn't exceed4096
characters. (You may have to edit the embed size)
returns:
- current embed
-
DiscordEmbed setColor(const int &color)
// example const int embed_color = 0xFFFF00; embed.setColor(embed_color);
Sets the embed accent color.
params:
color
: embed accent color in hex
returns:
- current embed
-
DiscordEmbed setImage(const String &image_url)
// example const String embed_image = "https://picsum.photos/300/200"; embed.setImage(embed_image);
Sets the embed image url.
params:
image_url
: self explanatory, I guess?
returns:
- current embed
-
DiscordEmbed setImage(const EmbedImage &embed_image)
// example EmbedImage embed_image; embed_image.url = "https://picsum.photos/300/200"; embed_image.height = 100; embed_image.width = 150; embed.setImage(embed_image);
Sets the embed image.
params:
embed_image
: anEmbedImage
instance
returns:
- current embed
-
DiscordEmbed setAuthor(const EmbedAuthor & author)
// example EmbedAuthor author; author.name = "slacker"; author.icon_url = "https://cdn.discordapp.com/avatars/738274800211984426/57b3da0b4df7cfbb616ef63941dd984d.webp"; embed.setAuthor(author);
Sets the embed author.
params:
author
: anEmbedAuthor
instance
returns:
- current embed
-
DiscordEmbed setFooter(const EmbedFooter &footer)
// example EmbedFooter footer; footer.text = "footer text"; embed.setFooter(footer);
Sets the embed footer.
params:
footer
: anEmbedFooter
instance
returns:
- current embed
-
DiscordEmbed setFooter(const String &text)
// example embed.setFooter("my footer text");
Sets the embed footer text.
params:
text
: again, self explanatory
returns:
- current embed
-
DiscordEmbed addField(const EmbedField &field)
// example EmbedField field; field.name = "Status"; field.value = "working π’"; field.is_inline = false; embed.addField(field);
Adds a field to the embed.
params:
field
: anEmbedField
instance
returns:
- current embed
-
DiscordEmbed addField(const String &name, const String &value, cont bool &is_inline)
// example const String field_name = "Battery"; const String field_value = "low π΄"; const bool field_is_inline = true; embed.addField(field_name, field_value, field_is_inline);
Adds a field to the embed.
params:
name
: embed name (title)value
: embed value (content)is_inline
:true
orfalse
if the field is inline
returns:
- current embed
-
String toString(bool pretty)
non-prettified// example embed.setTitle("My Embed Title") .setDescription("My embed description") .setFooter("my footer text") .setColor(0x333333); String my_embed = embed.toString(false); String my_pretty_embed = embed.toString(true); Serial.println(my_embed); Serial.println(my_pretty_embed);
prettified{"title":"My Embed Title","description":"My embed description","footer":{"text":"my footer text"},"color":3355443}
{ "title": "My Embed Title", "description": "My embed description", "footer": { "text": "my footer text" }, "color": 3355443 }
-
static int rgbToHex(const int &red, const int &green, const int &blue)
// example embed.setColor(DiscordEmbed::rgbToHex(151, 250, 148));
Converts RGB value to HEX.
params:
red
: red color valuegreen
: green color valueblue
: blue color value
returns:
- HEX value of the given RGB
-
-
// example DiscordWebhook discord_webhook;
This is more like the webhook structure, can be found here
-
Webhook type, either 1, 2 or 3. Check out the types here
int type
-
Webhook name (more like a username tbh)
String name
-
Webhook avatar hash
String avatar
-
Webhook channel id (where it sends messages)
String channel_id
-
Webhook guild id (where the webhook is present in)
String guild_id
-
Webhook application id
String application_id
-
Webhook token at the end of the url
String token
-
void begin(const String &url)
// example const String WEBHOOK_URL = "https://discord.com/api/webhooks/<webhook id>/<webhook token>"; discord_webhook.begin(WEBHOOK_URL);
Stores your webhook url for later use.
params:
url
: discord webhook URL
returns:
- nothing
-
bool DiscordWebhook::get()
// example bool success = discord_webhook.get(); if (!success) return; Serial.println(discord_webhook.name); Serial.println(discord_webhook.avatar); Serial.println(discord_webhook.guild_id);
Gets the current webhook info. Gets called inside
begin
.returns:
true
if fetching info is successful elsefalse
-
bool send(const String &text)
// example discord_webhook.send("Hello world!");
Sends a POST request to the webhook url.
params:
text
: discord message content
returns:
true
if response status code == 204 elsefalse
-
bool send(const String &text, const bool &tts)
params:
text
: discord message contenttts
:true
orfalse
if the message is text to speech
returns:
true
if response status code == 204 elsefalse
-
bool send(const DiscordEmbed &embed)
// example DiscordEmbed embed; embed.setTitle("Sensor Readings") .addField("10:00", "low - 23β, high - 25β", false) .addField("11:00", "low - 24.7β, high - 27β", false) .setColor(DiscordEmbed::rgbToHex(255, 255, 255)); discord_webhook.send(embed);
Send discord embed.
params:
embed
: aDiscordEmbed
instance as the message embed
returns:
true
if response status code == 204 elsefalse
-
bool send(const String &text, const DiscordEmbed &embed)
// example String content = "Reading from sensor every `1 hour`"; DiscordEmbed embed; embed.setTitle("Sensor Readings") .addField("10:00", "low - 23β, high - 25β", false) .addField("11:00", "low - 24.7β, high - 27β", false) .setColor(DiscordEmbed::rgbToHex(255, 255, 255)); discord_webhook.send(content, embed);
Send an embed with text above it as the message content.
params:
text
: discord message contentembed
: aDiscordEmbed
instance as the message embed
returns:
true
if response status code == 204 elsefalse
-
bool &isValid()
// example if (!discord_webhook.isValid()) return; Serial.println(discord_webhook.name); Serial.println(discord_webhook.avatar); Serial.println(discord_webhook.guild_id);
Returns if the webhook data is valid/present.
returns:
true
if the webhook data is valid/present elsefalse
-