Manage DM and group chat

This commit is contained in:
Luke Murphy 2021-01-10 16:31:17 +01:00
parent 43821cbd1b
commit 9767c7337e
No known key found for this signature in database
GPG Key ID: 5E2EF5A63E3718CC
2 changed files with 34 additions and 4 deletions

View File

@ -10,11 +10,25 @@ class WhisperBot(Bot):
will then do this on your behalf and not reveal your identity. This is nice will then do this on your behalf and not reveal your identity. This is nice
when you want to communicate with the group somewhat anonymously. when you want to communicate with the group somewhat anonymously.
The bot accepts messages in the following form.
whisper:<room>:<message>
So, I might write it like so.
whisper:myroom@muc.foo.com:hey, i actually really like avril lavigne!
""" """
def react(self, message): def react(self, message):
"""Receive direct messages and pass them to group chats.""" """Receive direct messages and pass them to group chats."""
pass if message.type == "groupchat":
return
if "whisper" in message.body:
_, room, whisper = message.body.split(":")
body = f"*whispers* {whisper}"
self.reply(to=room, body=body, type="groupchat")
WhisperBot() WhisperBot()

View File

@ -26,6 +26,14 @@ class EasyMessage:
def receiver(self): def receiver(self):
return self.message["to"] return self.message["to"]
@property
def nickname(self):
return self.message["mucnick"]
@property
def type(self):
return self.message["type"]
class Bot(ClientXMPP): class Bot(ClientXMPP):
CONFIG_FILE = "bot.conf" CONFIG_FILE = "bot.conf"
@ -70,8 +78,8 @@ class Bot(ClientXMPP):
def init_bot(self): def init_bot(self):
"""Initialise bot with connection details.""" """Initialise bot with connection details."""
jid = self.config["bot"]["jid"] jid = self.config["bot"]["jid"]
passwd = self.config["bot"]["password"] password = self.config["bot"]["password"]
ClientXMPP.__init__(self, jid, passwd) ClientXMPP.__init__(self, jid, password)
def register_xmpp_event_handlers(self): def register_xmpp_event_handlers(self):
"""Register functions against specific XMPP event handlers.""" """Register functions against specific XMPP event handlers."""
@ -89,9 +97,17 @@ class Bot(ClientXMPP):
self.send_presence() self.send_presence()
self.get_roster() self.get_roster()
room = self.config["bot"].get("room")
nick = self.config["bot"].get("nick")
if room and nick:
self.plugin["xep_0045"].join_muc(room, nick)
def groupchat_message(self, message): def groupchat_message(self, message):
"""Handle groupchat_message event.""" """Handle groupchat_message event."""
pass if message["type"] in ("groupchat", "normal"):
if message["mucnick"] != self.config["bot"]["nick"]:
self.react(EasyMessage(message))
def register_xmpp_plugins(self): def register_xmpp_plugins(self):
"""Register XMPP plugins that the bot supports.""" """Register XMPP plugins that the bot supports."""