Typing \leave in a MUC will leave the group chat

This commit is contained in:
moyamo 2015-11-27 22:01:16 +02:00
parent 8e08efabc9
commit bd74021ca6
3 changed files with 52 additions and 10 deletions

5
bot.py
View file

@ -179,7 +179,10 @@ class Bot():
\\getgroups get current groups from WA
following user commands are available:
\\lastseen request last online timestamp from buddy""")
\\lastseen request last online timestamp from buddy
following group commands are available
\\leave permanently leave group chat""")
def _fortune(self, database = '', prefix=''):
if os.path.exists("/usr/share/fortune/%s" % database):

View file

@ -564,15 +564,19 @@ class Session(YowsupApp):
self.msgIDs[waId] = MsgIDs( ID, waId)
else:
room = sender
try:
group = self.groups[self._lengthenGroupId(room)]
self.logger.info("Group Message from %s to %s Groups: %s",
group.nick , group , self.groups)
self.backend.handleMessage(
self.user, room, message.decode('utf-8'), group.nick
)
except KeyError:
self.logger.error('Group not found: %s', room)
if message[0] == '\\' and message[:1] != '\\\\':
self.logger.debug("Executing command %s in %s", message, room)
self.executeCommand(message, room)
else:
try:
group = self.groups[self._lengthenGroupId(room)]
self.logger.debug("Group Message from %s to %s Groups: %s",
group.nick , group , self.groups)
self.backend.handleMessage(
self.user, room, message.decode('utf-8'), group.nick
)
except KeyError:
self.logger.error('Group not found: %s', room)
if (".jpg" in message.lower()) or (".webp" in message.lower()):
if (".jpg" in message.lower()):
@ -629,6 +633,30 @@ class Session(YowsupApp):
self.logger.info("WA Message send to %s with ID %s", buddy, waId)
#self.sendTextMessage(sender + '@s.whatsapp.net', message)
def executeCommand(self, command, room):
if command == '\\leave':
self.logger.debug("Leaving room %s", room)
# Leave group on whatsapp side
self.leaveGroup(room)
# Delete Room on spectrum side
group = self.groups[room]
for jid in group.participants:
buddy = jid.split("@")[0]
self.logger.info("Added %s to room %s", buddy, room)
try:
nick = self.buddies[buddy].nick
except KeyError:
nick = buddy
if nick == "":
nick = buddy
if buddy == self.legacyName:
nick = group.nick
flags = protocol_pb2.PARTICIPANT_FLAG_ROOM_NOT_FOUND
self.backend.handleParticipantChanged(
self.user, nick, self._shortenGroupId(room), flags,
protocol_pb2.STATUS_NONE, buddy)
del self.groups[room]
def _requestLastSeen(self, buddy):

View file

@ -232,6 +232,16 @@ class YowsupApp(object):
entity = UnsubscribePresenceProtocolEntity(jid)
self.sendEntity(entity)
def leaveGroup(self, group):
"""
Permanently leave a WhatsApp group
Args:
- group: (str) the group id (e.g. 27831788123-144024456)
"""
entity = LeaveGroupsIqProtocolEntity([group + '@g.us'])
self.sendEntity(entity)
def setStatus(self, statusText):
"""
Send status to whatsapp
@ -595,6 +605,7 @@ class YowsupAppLayer(YowInterfaceLayer):
@ProtocolEntityCallback('message')
def onMessageReceived(self, entity):
self.logger.debug("Received Message: %s", entity)
if entity.getType() == MessageProtocolEntity.MESSAGE_TYPE_TEXT:
self.caller.onTextMessage(
entity._id,