首页 > 解决方案 > 使用 discord.py 时,如何从(gitlab 通知)webhook 获取消息内容?

问题描述

这个问题之前已经被问过(How to get the message content printing out of a discord webbook! in python),但已经关闭。所以我希望我有权就这个话题提出一个新问题?

以下情景:

我们使用来自 gitlab ( https://docs.gitlab.com/ee/user/project/integrations/discord_notifications.html ) 的不和谐通知服务来使用不和谐向我们的不和谐频道之一发布新问题/推送到主通知。 py。该频道还通过机器人链接到另一个聊天,因此两个聊天始终具有相同的内容。到目前为止,这是有效的。

当我现在尝试获取print(message.content)来自 gitlab 的消息时,它是空的,即使它显示一条不和谐的消息。基本结构如下所示:

import discord
import re

class MyClient(discord.Client):
    async def on_ready(self):
        print('Logged on as {0}!'.format(self.user))

    async def on_message(self, message):
        # modifying and preparing messages for sync with second chat
        # I removed this because it shouldn't have anything to do with the problem

        # trying to find the message content
        print(message.author)
        print(message.content)
        print(message)
        print('----------')
        
        # write messages into database
    

client = MyClient()
client.run('TOKEN')

print(message)返回类似的东西<Message id=1234 channel=<TextChannel id=5678 name='some-name' position=1 nsfw=False news=False category_id=9012> type=<MessageType.default: 0> author=<User id=3456 name='Bot Name' discriminator='0000' bot=True> flags=<MessageFlags value=0>>

我确实阅读了 API 参考的大部分内容(https://discordpy.readthedocs.io/en/latest/api.html),但找不到任何东西。虽然我无法确定 webhook 部分的正面或反面(https://discordpy.readthedocs.io/en/latest/api.html#webhook-support)。

任何想法如何从来自 webhook 的消息中获取消息内容?它必须在某个地方...

提前谢谢你,克里斯蒂安

2020 年 10 月 23 日更新

我尝试通过https://pipedream.com/创建事件侦听器来获取有关从 GitLab 发送到 Discord 的信息的更多详细信息,但它没有记录任何事件(设置为“新问题”,创建了几个),而侦听器对于不和谐(关于新消息)工作正常。

设置如下:

  1. 在 Discord 中创建 Webhook ( https://discord.com/api/webhooks/[id]/[token] )
  2. 将上述 Webhook 输入 GitLab 中的设置,并从众多触发器中进行选择。

在这种情况下,我不知道如何获取 GitLab 发送给 Discord 的标头数据。我还使用 Postman 检查了 Discord 提供的 Webhook - 它仅返回 Bots 的基本统计信息。

标签: pythondiscord.py

解决方案


GitLab 将消息内容作为嵌入发送(https://discordpy.readthedocs.io/en/latest/api.html#embed

就我而言,我正在寻找的信息就在我眼前。您可以使用message.embeds[0].description.

我无法对嵌入的 API 描述做出正面或反面,但似乎我也从未测试过它的作用,然后开始绕圈子。


推荐阅读