xmpp: Use webhook.send

This commit is contained in:
PapaTutuWawa 2021-09-16 18:29:55 +02:00
parent edf70774a2
commit 3bafcb916c

View File

@ -16,7 +16,7 @@ from slixmpp.componentxmpp import ComponentXMPP
from slixmpp.exceptions import XMPPError, IqError from slixmpp.exceptions import XMPPError, IqError
from slixmpp.xmlstream import register_stanza_plugin from slixmpp.xmlstream import register_stanza_plugin
from slixmpp.jid import JID from slixmpp.jid import JID
from discord import Status from discord import Status, Embed
import requests import requests
from xmpp_discord_bridge.slixmpp.oob import OOBData from xmpp_discord_bridge.slixmpp.oob import OOBData
@ -194,16 +194,15 @@ class BridgeComponent(ComponentXMPP):
str(vcard))) str(vcard)))
# Aquire a webhook # Aquire a webhook
webhook_url = "" wh = ""
for webhook in await dchannel.webhooks(): for webhook in await dchannel.webhooks():
if webhook.name == "discord-xmpp-bridge": if webhook.name == "discord-xmpp-bridge":
webhook_url = webhook.url wh webhook
break break
if not webhook_url: if not wh:
webhook = await dchannel.create_webhook(name="discord-xmpp-bridge", wh = await dchannel.create_webhook(name="discord-xmpp-bridge",
reason="Bridging Discord and XMPP") reason="Bridging Discord and XMPP")
webhook_url = webhook.url self._webhooks[muc] = wh
self._webhooks[muc] = webhook_url
# TODO: Check if we need to set it # TODO: Check if we need to set it
# Make sure our virtual users can join # Make sure our virtual users can join
@ -237,42 +236,30 @@ class BridgeComponent(ComponentXMPP):
if not message["to"] == self._bot_jid_full: if not message["to"] == self._bot_jid_full:
return return
webhook = { content = message["body"]
"content": message["body"], embed = None
"username": message["from"].resource
}
if self._relay_xmpp_avatars and self._avatars.get_avatar_url(message["from"]):
webhook["avatar_url"] = self._avatars.get_avatar_url(message["from"])
# Look for mentions and replace them # Look for mentions and replace them
guild, channel = self._muc_map[muc] guild, channel = self._muc_map[muc]
for member in self._discord.get_guild(guild).get_channel(channel).members: for member in self._discord.get_guild(guild).get_channel(channel).members:
self._logger.debug("Checking %s", member.display_name) self._logger.debug("Checking %s", member.display_name)
if "@" + member.display_name in webhook["content"]: if "@" + member.display_name in content:
self._logger.debug("Found mention for %s. Replaceing.", self._logger.debug("Found mention for %s. Replaceing.",
member.display_name) member.display_name)
webhook["content"] = webhook["content"].replace("@" + member.display_name, content = content.replace("@" + member.display_name,
member.mention) member.mention)
if message["oob"]["url"] and message["body"] == message["oob"]["url"]: if message["oob"]["url"] and message["body"] == message["oob"]["url"]:
webhook["embed"] = [{ embed = Embed(url=message["oob"]["url"], type="rich")
"type": "rich",
"url": message["oob"]["url"]
}]
self._logger.debug("Webhook: %s", str(webhook)) try:
await self._webhooks[muc].send(content=content,
req = requests.post(self._webhooks[muc], username=message["from"].resource,
data=webhook, avatar_url=self._avatars.get_avatar_url(message["from"]),
headers={ embed=embed)
"Content-Type": "application/json" except Exception as err:
}) self._logger.error("Webhook execution failed: %s",
if req.status_code != 200: err)
self._logger.error("Failed to POST to %s with content %s: %s",
self._webhooks[muc],
str(webhook),
req.text)
def virtual_user_update_presence(self, muc, uid, pshow, pstatus=None): def virtual_user_update_presence(self, muc, uid, pshow, pstatus=None):
""" """