This post is mostly for me to come back to when I need it but thought it would be helpful for others too.

Discord allows anyone to create webhooks which send messages to specific channels in their server. They also allow you to create “embeds” which can be different content.

The Discohook site is a great way of seeing what an embed would look like.

Firstly, have the httparty Gem installed in your app.

As an example, here’s a webhook I use in Florence Bot for when a new YouTube video is live

# Your Discord webhook URL. Keep this secret.
url = "https://discord.com/api/webhooks/abc123"

embeds = [
  {
    type: "rich",
    # Title is picked from the model that contains the video info
    title: ["New Fremily YouTube Video", title].join("\n\n"),
    # Shows the date at the bottom of the embed
    timestamp: published_at,
    image: {
      # Links to a YouTube thumbnail
      url: thumbnail
    },
    # Links to the YouTube video
    url: link,
    # A small description of the video with a link
    description: "#{description} \n\nWatch at #{link}"
  }
]

response = HTTParty.post(url, body: {embeds: embeds}.to_json, headers: { 'Content-Type' => 'application/json', 'Accept' => 'application/json'})

That code renders like so:

Discord YouTube Example

I think it looks much better than the normal embed when you paste a YouTube link.