Webhooks 101: A Beginner’s Guide with Step-by-Step Discord Integration
Konstantin Borimechkov
Founding Engineer @ Mandel AI | Writer of ‘The Excited Engineer’ Newsletter
?? 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.
?? 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? ?? ??
Why not use regular HTTP request/response?
?? Real world use-cases of webhooks
Stripe
Slack
Shopify
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 ??
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:
... 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! ??
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!
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.
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?