【问题标题】:Send data to a Discord webhook将数据发送到 Discord webhook
【发布时间】:2018-09-11 09:33:04
【问题描述】:

如果不使用 requests 模块,我如何向 Discord webhook 发送消息? 我试过以下代码:

import urllib2
import json

url = 'webhook url'
values = {"username": "Bot", "text": "This is a test message."}

data = json.dumps(values)
req = urllib2.Request(url, data)
response = urllib2.urlopen(req)
the_page = response.read()

这会返回以下错误:

urllib2.HTTPError: HTTP Error 403: Forbidden

【问题讨论】:

  • 这意味着您使用的应用未经授权可以制作任何 webhook。您可能需要在发出任何请求之前获取令牌。
  • 我在制作 webhook 时唯一由 Discord 提供的是 webhook url @SubhrajyotiDas
  • 您尝试的网址是什么?
  • 这是一个不和谐的 webhook url,格式为 discordapp.com/api/webhooks/<server ID>/<...>

标签: python python-2.7 urllib2 webhooks discord


【解决方案1】:

您的数据不正确 - 根据Discord API documentation,它应该是这样的:

values = {
    'username': 'Bot',
    'content': 'your message here'
}

请注意contentfileembeds 之一是必需的。否则 API 将不会接受您的请求。

【讨论】:

    【解决方案2】:

    我的第一个建议是以下步骤:

    1. 在所需的 Discord 频道中创建一个 webhook。
    2. 使用 discord.Webhook.from_url 方法从 Discord 提供给您的 URL 中获取 Webhook 对象。
    3. 使用discord.Webhook.send方法发送消息。

    另一种选择是像这样使用 discord.py (v2):

    from discord import SyncWebhook
    
    webhook = SyncWebhook.from_url("url-here")
    webhook.send("Hello World")
    

    【讨论】:

      【解决方案3】:

      您可以将消息发送到 Discord webhook。

      1. 用这个tutorial创建一个不和谐的网络钩子。

      2. 然后,使用 discord.Webhook.from_url 方法从 Discord 提供的 URL 中获取 Webhook 对象。

      3. 使用discord.Webhook.send 方法使用webhook 发送消息。

      如果你使用的是 discord.py 的第 2 版,这个 sn-p 将为你工作:

      from discord import SyncWebhook
      
      webhook = SyncWebhook.from_url("your-url")
      webhook.send("Hello")
      

      如果你不使用版本 2,你可以使用这个 sn-p:

      import requests
      from discord import Webhook, RequestsWebhookAdapter
      
      webhook = Webhook.from_url("youre-url", adapter=RequestsWebhookAdapter())
      webhook.send("Hello")
      

      【讨论】:

        猜你喜欢
        • 2021-09-18
        • 2020-11-26
        • 2020-07-08
        • 2020-03-20
        • 2019-01-16
        • 2020-04-11
        • 2019-03-05
        • 2019-10-21
        • 2020-09-25
        相关资源
        最近更新 更多