Skip to content

Instantly share code, notes, and snippets.

@ianklatzco
Created December 29, 2016 02:29
Show Gist options
  • Star 33 You must be signed in to star a gist
  • Fork 7 You must be signed in to fork a gist
  • Save ianklatzco/769d9e3a991dc2f443a2e105b0157117 to your computer and use it in GitHub Desktop.
Save ianklatzco/769d9e3a991dc2f443a2e105b0157117 to your computer and use it in GitHub Desktop.
sends messages to a discord channel using a bot via http POST
# post a message to discord api via a bot
# bot must be added to the server and have write access to the channel
# you may need to connect with a websocket the first time you run the bot
# use a library like discord.py to do so
import requests
import json
channelID = "your_id_goes_here" # enable dev mode on discord, right-click on the channel, copy ID
botToken = "your_token_here" # get from the bot page. must be a bot, not a discord app
baseURL = "https://discordapp.com/api/channels/{}/messages".format(channelID)
headers = { "Authorization":"Bot {}".format(botToken),
"User-Agent":"myBotThing (http://some.url, v0.1)",
"Content-Type":"application/json", }
message = "hello world"
POSTedJSON = json.dumps ( {"content":message} )
r = requests.post(baseURL, headers = headers, data = POSTedJSON)
@leoszn
Copy link

leoszn commented Aug 10, 2017

May I know how to send embedded message from JSON? What to put in the content? Can you help me up?

https://anidiotsguide.gitbooks.io/discord-js-bot-guide/examples/using-embeds-in-messages.html

@p0ntsNL
Copy link

p0ntsNL commented Feb 12, 2018

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

@0D1NTR33
Copy link

0D1NTR33 commented Jun 28, 2018

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?

@40163650
Copy link

@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

@0D1NTR33
Copy link

0D1NTR33 commented Jul 1, 2018

@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.

@dCosminn
Copy link

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

@LingleDev
Copy link

I keep getting a 405 Method Not Allowed...

@DevLucem
Copy link

this is such an expensive code displayed humbly

@ayushsatyam146
Copy link

ayushsatyam146 commented Jul 3, 2020

Can someone do the same with node.js ?

@nandaak
Copy link

nandaak commented Dec 7, 2020

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

@DevLucem
Copy link

Can someone do the same with node.js ?

please use Axios. its the same.

@tCp6ddIcc5e4EP69NJtTpo0waSuZgUXdgv9peLW

May I know how to send embedded message from JSON? What to put in the content? Can you help me up?

https://anidiotsguide.gitbooks.io/discord-js-bot-guide/examples/using-embeds-in-messages.html

Maybe this will be useful
https://leovoel.github.io/embed-visualizer/

@aqw3
Copy link

aqw3 commented Feb 18, 2021

How to add role or remove ?

@jiggyjo11
Copy link

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

@viluxes
Copy link

viluxes commented Jan 3, 2022

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

@xmrstickers
Copy link

nice and simple, perfect.

@dgsqf
Copy link

dgsqf commented Feb 17, 2024

I'm just getting 405 method forbidden using Postman

@dgsqf
Copy link

dgsqf commented Feb 17, 2024

Nvm I had forgotten the last part of the endpoint

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