3

sends messages to a discord channel using a bot via http POST

 8 months ago
source link: https://gist.github.com/ianklatzco/769d9e3a991dc2f443a2e105b0157117
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

sends messages to a discord channel using a bot via http POST · GitHub

Instantly share code, notes, and snippets.

sends messages to a discord channel using a bot via http POST

also wondering how this script can be changed to send an image instead

Cool, nice gist, but I have an error {"code": 40001, "message": "Unauthorized"}.
I've added bot to the server and set premissions to send messages.
Do you know how to fix it without using any libraries?

@MxShift you need to connect to the API via a gateway before it will let you send messages, it says:

Before using this endpoint, you must connect to and identify with a gateway at least once.

These links should help
https://discordapp.com/developers/docs/topics/gateway#gateways
https://discordapp.com/developers/docs/topics/gateway#get-gateway

@40163650 Thanks. I found that webhooks is easier solution for just sending messages.

import json
import requests


def sendToDiscord(webhook_url, message):
    """
    Post a message to discord API via a Webhook.
    """
    payload = {
        "content": message
    }
    headers = {
        'Content-Type': 'application/json',
    }
    response = requests.post(webhook_url, data=json.dumps(payload), headers=headers)
    return response

@p0nt using webhook, images can be send just by adding url to image to payload

payload = {
  "content": message,
  "embeds": [
    {
      "image": {
        "url": image_url
      }
    }
  ]
}

Also full possible structure of webhook message you can find here.

Is it possible to use email and password instead of token?

I keep getting a 405 Method Not Allowed...

this is such an expensive code displayed humbly

Can someone do the same with node.js ?

How to read messages on a date from a channel?. Could someone post a sample get request using a webhook? Sorry newbie here

Can someone do the same with node.js ?

please use Axios. its the same.

How to add role or remove ?

Bot token must be specified with a bot in the beginning of the string. Example: Authorization: Bot MTk4NjIyNDgzNDcxOTI1MjQ4.Cl2FMQ.ZnCjm1XVW7vRze4b7Cq4se7kKWs

How to send with this some commands with slash '/'?

nice and simple, perfect.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK