Building a Discord bot with Azure Logic Apps
Tags: azure, discord, logicapps, youtube
Published: 2021-05-06 09:26:16

## The Betatalks Discord

A few months ago we've started a [Betatalks Discord server](https://discord.gg/KTkCXQTgjR) to build a community and enable our friends to
discuss all the Betatalks content we put out there. Think about the
[Betatalks videos](https://www.youtube.com/playlist?list=PLCLCtgDNNiJR_LDx6RT8X50VrKAH3_49B) on YouTube, Betatalks *live* events and the
most recent addition: [Betatalks the podcast](https://podcast.betatalks.nl/).

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](https://www.rickvandenbosch.net/images/posts/discord-bot-01.webp)

## 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:

```json
{
    "content": "Hello world!"
}
```

More information on the Discord webhook payload can be found in the
[Discord developer portal](https://discord.com/developers/docs/resources/webhook#execute-webhook).

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](https://www.postman.com), [hoppscoth.io](https://hoppscotch.io/) or Visual Studio Code using the
[REST Client](https://marketplace.visualstudio.com/items?itemName=humao.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](https://docs.microsoft.com/en-us/azure/connectors/apis-list) 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](https://www.rickvandenbosch.net/images/posts/discord-bot-02.webp)

### 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](https://www.rickvandenbosch.net/images/posts/discord-bot-03.webp)

### 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](https://www.rickvandenbosch.net/images/posts/discord-bot-04.webp)

And that's it! To see the resulting messages yourself, check our [Betatalks Discord](https://discord.gg/KTkCXQTgjR).

Hope this helps!