DEV Community

loading...
Cover image for Building a Discord bot with Azure Logic Apps

Building a Discord bot with Azure Logic Apps

rickvdbosch profile image Rick van den Bosch Originally published at rickvandenbosch.net ・3 min read

The Betatalks Discord

A few months ago we've started a Betatalks Discord server to build a community and enable our friends to
discuss all the Betatalks content we put out there. Think about the
Betatalks videos on YouTube, Betatalks live events and the
most recent addition: Betatalks the podcast.

To notify the corresponding Discord channels about new Betatalks videos or Betatalks the podcast episodes, I created Azure Logic Apps to
check for new additions and post them to the webhook. Here's how I built that.

Creating a Discord webhook

As Discord describes it:

Webhooks are a simple way to post messages from other apps and websites into Discord using internet magic.

When you create a Discord webhook you give it a name (1), select the channel the webhook posts to (2) and you optionally upload an avatar
for the webhook (3). Once you've done all that save your changes, copy the webhook URL (4) and you should be good to go!

Creating a Discord webhook

Calling Discord webhooks

The most basic form of calling a Discord webhook only needs either a message (in the content property), file contents (in the file
property) or an array of up to 10 embed objects (in the embeds property). This means a most basic, valid payload for a Discord webhook
looks like this:

{
    "content": "Hello world!"
}
Enter fullscreen mode Exit fullscreen mode

More information on the Discord webhook payload can be found in the
Discord developer portal.

Since calling the webhook is as easy as posting an HTTP request to an URL, you can easily test the content you're building using tools like
Postman, hoppscoth.io or Visual Studio Code using the
REST Client extension.

Building the Logic App

The podcast bot uses the RSS feed to determine if there are new episodes and calls the webhook. The YouTube one uses a
connector to YouTube. We'll use the latter as the example in this post.

Triggering the workflow

The selected trigger for the Logic App is YouTube's "When a video is uploaded by a channel". This enables you to select any of the channels
you've subscribed to on YouTube. You can configure how often the Logic App needs to check for new items and then the triggering part of the
Logic App is done.

Triggering the workflow

On one condition

The workflow should run on one condition. Or two actually, but they can be seen as one 😁. We only want to post content to the webhook if
the title of the video contains the term Betatalks but does not contain podcast. To do this, we add a Condition-action to our workflow.
We toLower the video's title and check the conditions we just specified.

Checking the condition

Calling the webhook

Now that we've defined our trigger and checked our conditions, we can configure calling the webhook. To do so we add an HTTP action. We
configure it to do a POST to the webhook endpoint with a body we configure based on dynamic content from the trigger. As we saw in the
example earlier we can create a json payload with a content property to hold the message we want to send.

Posting the information

And that's it! To see the resulting messages yourself, check our Betatalks Discord.

Hope this helps!

Discussion (0)

Forem Open with the Forem app