Skip to content

cmd420/discord-webhooks

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

20 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

Discord Webhooks

A simple to use Discord webhooks wrapper made using Arduino C

Library progress

  • Basic implementation
  • Simple docs
  • Add more detailed examples
  • Send multiple embeds
  • General optimization
  • A better README file...

Changelog:

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.

Getting started

  • Download and install the library

  • Create a webhook:

    1. Go to server settings > Integrations


    server settings

    2. Click on "Create Webhook"


    create webhook

    3. Customize your webhook and click on "Save Changes"


    save changes

    4. Copy your Webhook URL


    copy url

  • Now that you have your webhook URL, start coding!


Examples

  • Basic example:

    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()
    {
    }
  • Using Embeds

    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);

    or send an embed with a message

        bool success = discord_webhook.send("Hey @everyone, check out the forecast!", embed);
    
        Serial.printf("Sending webhook %s\n", success ? "succeeded!" : "failed.");
    }

Docs

  • EmbedAuthor

    Embed author structure can be found here

    // example 
    EmbedAuthor author;

    Variables:

    • String name

      Author name.

    • String url

      Author url.

    • String icon_url

      Author icon url (often the author's pfp).



  • EmbedFooter

    Embed footer structure can be found here

    // example
    EmbedFooter footer;

    Variables:

    • String text

      Embed footer text.

    • String icon_url

      Embed footer icon url.



  • EmbedField

    Embed field structure can be found here

    // example
    EmbedField field;

    Variables:

    • String name

      Embed field name (more like title)

    • String value

      Embed field value (more like content)



  • EmbedImage

    Embed image structure can be found here

    // example
    EmbedImage image;

    Variables:

    • 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



  • DiscordEmbed

    Embed structure can be found here

    // example
    DiscordEmbed embed;

    Variables:

    • 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.

    Methods:

    • 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 exceed 4096 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: an EmbedImage 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: an EmbedAuthor 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: an EmbedFooter 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: an EmbedField 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 or false if the field is inline

      returns:

      • current embed

    • String toString(bool pretty)
      // 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);
      non-prettified
      {"title":"My Embed Title","description":"My embed description","footer":{"text":"my footer text"},"color":3355443}
      
      prettified
      {
          "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 value
      • green: green color value
      • blue: blue color value

      returns:

      • HEX value of the given RGB


  • DiscordWebhook

    // example
    DiscordWebhook discord_webhook;

    Variables:

    This is more like the webhook structure, can be found here


    • int type
      Webhook type, either 1, 2 or 3. Check out the types here

    • String name
      Webhook name (more like a username tbh)

    • String avatar
      Webhook avatar hash

    • String channel_id
      Webhook channel id (where it sends messages)

    • String guild_id
      Webhook guild id (where the webhook is present in)

    • String application_id
      Webhook application id

    • String token
      Webhook token at the end of the url

    Methods:

    • 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 else false

    • 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 else false

    • bool send(const String &text, const bool &tts)

      params:

      • text: discord message content
      • tts: true or false if the message is text to speech

      returns:

      • true if response status code == 204 else false

    • 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: a DiscordEmbed instance as the message embed

      returns:

      • true if response status code == 204 else false

    • 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 content
      • embed: a DiscordEmbed instance as the message embed

      returns:

      • true if response status code == 204 else false

    • 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 else false

About

A discord webhooks wrapper written in Arduino

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages