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 \\getgroups get current groups from WA
following user commands are available: 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=''): def _fortune(self, database = '', prefix=''):
if os.path.exists("/usr/share/fortune/%s" % database): if os.path.exists("/usr/share/fortune/%s" % database):

View file

@ -564,9 +564,13 @@ class Session(YowsupApp):
self.msgIDs[waId] = MsgIDs( ID, waId) self.msgIDs[waId] = MsgIDs( ID, waId)
else: else:
room = sender room = sender
if message[0] == '\\' and message[:1] != '\\\\':
self.logger.debug("Executing command %s in %s", message, room)
self.executeCommand(message, room)
else:
try: try:
group = self.groups[self._lengthenGroupId(room)] group = self.groups[self._lengthenGroupId(room)]
self.logger.info("Group Message from %s to %s Groups: %s", self.logger.debug("Group Message from %s to %s Groups: %s",
group.nick , group , self.groups) group.nick , group , self.groups)
self.backend.handleMessage( self.backend.handleMessage(
self.user, room, message.decode('utf-8'), group.nick self.user, room, message.decode('utf-8'), group.nick
@ -630,6 +634,30 @@ class Session(YowsupApp):
self.logger.info("WA Message send to %s with ID %s", buddy, waId) self.logger.info("WA Message send to %s with ID %s", buddy, waId)
#self.sendTextMessage(sender + '@s.whatsapp.net', message) #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): def _requestLastSeen(self, buddy):
def onSuccess(buddy, lastseen): def onSuccess(buddy, lastseen):

View file

@ -232,6 +232,16 @@ class YowsupApp(object):
entity = UnsubscribePresenceProtocolEntity(jid) entity = UnsubscribePresenceProtocolEntity(jid)
self.sendEntity(entity) 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): def setStatus(self, statusText):
""" """
Send status to whatsapp Send status to whatsapp
@ -595,6 +605,7 @@ class YowsupAppLayer(YowInterfaceLayer):
@ProtocolEntityCallback('message') @ProtocolEntityCallback('message')
def onMessageReceived(self, entity): def onMessageReceived(self, entity):
self.logger.debug("Received Message: %s", entity)
if entity.getType() == MessageProtocolEntity.MESSAGE_TYPE_TEXT: if entity.getType() == MessageProtocolEntity.MESSAGE_TYPE_TEXT:
self.caller.onTextMessage( self.caller.onTextMessage(
entity._id, entity._id,