KeiruaProd

I help my clients acquire new users and make more money with their web businesses. I have ten years of experience with SaaS projects. If that’s something you need help with, we should get in touch!
< Back to article list

Envoyer une notification discord

Un petit snippet: ca sert de temps en temps d’envoyer des notifications. Voici comment le faire vers discord avec quelques lignes de python − en fait, une simple requête POST sur un webhook.

import requests

# Make a webhook for the channel you want to send things to:
# https://support.discord.com/hc/en-us/articles/228383668-Intro-to-Webhooks
# Server settings -> integrations -> webhook -> new webhook
webhook_url = "https://discord.com/api/webhooks/123456/some-more-stuff"
headers={'Content-Type': 'application/json'}
# Build your embeds with an online embed editor like 
# https://autocode.com/tools/discord/embed-builder/
data = {
  "embeds": [
    {
      "title": "⛽ A new blog post",
      "url": "https://www.keiruaprod.fr/blog/",
      "color": 15258703,
      "fields": [
        {
          "name": ":smile:  Reading time",
          "value": "```yaml\n5 minutes\n```",
          "inline": True
        },
        {
          "name": ":grinning:  Word count",
          "value": "```diff\n1000```",
          "inline": True
        }
      ],
      "timestamp": "2022-11-17T00:28:45.000Z"
    }
  ]
}
import json
try:
	r = requests.post(webhook_url, headers=headers, json=data)
	r.raise_for_status()
	print("success")
except Exception as e:
	print("error")
	print(e)