Webhooks 101: A Beginner’s Guide with Step-by-Step Discord Integration

Webhooks 101: A Beginner’s Guide with Step-by-Step Discord Integration

?? In today’s tech-driven world, where we rely on getting information quickly, it’s essential for software engineers to know what webhooks are and how they function!

You are actually using webhooks more than you think. Every time you get a push-notification on your phone, that’s a webhook! ??

Let’s elaborate on that by first defining what’s the definition of a webhook?

Webhooks are a way for one system to send real-time data to another system as soon as an event occurs.
Great infographic by itzone.com

?? Why webhooks?

In order to better explain this lets run trough a simple example, where we need to decide whether we should use webhooks or regular HTTP request/response??

The example: Imagine we are working on a social media app and we need to implement a feature that will notify users once they receive a DM:

In this case, webhooks will be the way to go, but why? ?? ??

  • Real-time Updates - When a user receives a DM, the server can immediately send a notification to the user’s device without the need for the device to repeatedly check for new messages.
  • Efficiency - With a webhook the server sends the notification only when there is relevant information.
  • Reduced Latency - Since each milisecond is of importance when we speak about receiving DM notifications, webhooks serve very well here by eliminating the delay between the event (receiving a DM) and the notification
  • Scalability - they will distribute notifications efficiently to only the users who need them, reducing the overall load on the server.
  • Async processing - webhooks enable the server to handle the notification independently of the user’s device.

Why not use regular HTTP request/response?

  • Real-time updates will be made by the client, who constantly needs to make requests to the server, to check if there is anything new. This introduced polling overhead.
  • Increased Latency as the client may not be aware of new messages until the next scheduled poll (API call).
  • Increasing the Server Load by constantly running API requests for the multitude of users, especially when we have many active users!
  • Polling consumes more resources, both on the client side (due to frequent requests) and on the server side (due to handling unnecessary requests).


Summary of the difference between both approaches

?? Real world use-cases of webhooks

Stripe

  • Approach: Stripe, a payment processing platform, uses webhooks to notify merchants about events related to payments, subscriptions, and other transaction-related activities.
  • Resource: Stripe Webhooks Documentation

Slack

  • Approach: Slack employs webhooks for real-time notifications within its collaboration platform. Developers can use webhooks to integrate external services with Slack channels.
  • Resource: Slack Webhooks Documentation

Shopify

  • Approach: Shopify, an e-commerce platform, leverages webhooks to notify merchants about events like new orders, customer updates, and inventory changes.
  • Resource: Shopify Webhooks Documentation

P.S. Shopify have one of my favourite documentations! Have a look, it’s worth it! ??


??♂? Lets build our own webhook!

We’ll create a Discord webhook to send a message to a channel, notifying its members that we’ve just uploaded a new Medium blog post!

First, go to Discord and configure a webhook like so ??

  1. Go to the channel/server where you want to send messages.
  2. Click on the ‘Server’ drop-down and then on ‘Server Settings’
  3. Navigate to the “Apps/Integrations” tab and click “Create Webhook.”
  4. Customize the name and avatar for your webhook. Copy the webhook URL.

The end result should be something like this:

Now, lets create a method that will interact with this webhook ??

import requests

def send_discord_message(webhook_url, message):
    payload = {'content': message}
    headers = {'Content-Type': 'application/json'}

    response = requests.post(webhook_url, json=payload, headers=headers)

    if response.status_code == 204:
        print('Message sent successfully!')
    else:
        print(f'Failed to send message. Status code {response.status_code}')        

We can use the above method to notify the Discord channel members that we’ve uploaded a new blog in Medium! To do so, we would need to provide the Webhook URL from Discord and use it for the webhook_url param, and also give a custom message to tell the members about the news!


For now, you can run the script by simply adding this below the code ??

message = 'We have just published a new Medium blog'
send_discord_message(webhook_url, message)        

After running the python script that should look like this:

Discord Webhook Bot

... you can go to your Discord channel and see that the message has been indeed sent! ??

?? Typically, you would trigger the script in response to a specific event in your system. For example, with logic for publishing this blog via code, you would execute this script after calling the Medium.publish method ??!


?? That wraps up our beginner-friendly introduction to webhooks. I trust you now have a clearer understanding of them.

Feel free to ask if any questions come to mind! ??


Aamir Sohail

Generative AI | Computer Vision | NLP | Machine Learning | AI Agents System Automation

10 个月

Basically I am working on Google sheet and set a trigger on google sheet for specific criteria. It's working fine and send message in a channel but i want to DM a specific person. Below is the message which i already returning in channel. Hey! there,??? <@1127920079170973776>! ?? ???Just a quick heads-up: It's time to give your spreadsheet a little love and update it. ???[Link here] and let's keep things rolling smoothly. ???? ???Thank You!

Aamir Sohail

Generative AI | Computer Vision | NLP | Machine Learning | AI Agents System Automation

10 个月

Thank you Konstantin Borimechkov for such useful blog. Is there any way to DM a person using webkook. Instead of any channel.

Viktor Kolev

Software Engineer

1 年

Thank you for the great article, Koko. Does the webhook convert the client phone into a server itself? How do the webhooks work under the hood?

要查看或添加评论,请登录

Konstantin Borimechkov的更多文章

社区洞察