Fixed examples, added changeNickname and changeThreadEmoji, changed changeGroupTitle back to changeThreadTitle

I also removed the parameter `set_default_events` from __init__, since
it's not really necessary
Also added testing of examples and simple testing of listen functions
This commit is contained in:
Mads Marquart
2017-05-26 18:48:37 +02:00
parent d2741ca419
commit 39eafa5a3e
6 changed files with 173 additions and 87 deletions

View File

@@ -4,15 +4,15 @@ from fbchat import log, Client
# Subclass fbchat.Client and override required methods
class EchoBot(Client):
def onMessage(self, mid, author_id, message, thread_id, thread_type, ts, metadata, msg):
def onMessage(self, author_id, message, thread_id, thread_type, **kwargs):
self.markAsDelivered(author_id, thread_id)
self.markAsRead(author_id)
log.info("Message from {} in {} ({}): {}".format(author_id, thread_id, thread_type.name, message)))
log.info("Message from {} in {} ({}): {}".format(author_id, thread_id, thread_type.name, message))
# If you're not the author, echo
if author_id != self.uid:
self.sendMessage(message, thread_id=thread_id, thread_type=thread_type)
client = EchoBot("<email>", "<password>")
client.listen()
client.listen()

View File

@@ -18,39 +18,37 @@ old_nicknames = {
}
class KeepBot(Client):
def onColorChange(self, mid, author_id, new_color, thread_id, thread_type, ts, metadata, msg):
def onColorChange(self, author_id, new_color, thread_id, thread_type, **kwargs):
if old_thread_id == thread_id and old_color != new_color:
log.info("{} changed the thread color. It will be changed back".format(author_id))
self.changeThreadColor(old_color, thread_id=thread_id)
def onEmojiChange(self, mid, author_id, new_emoji, thread_id, thread_type, ts, metadata, msg):
def onEmojiChange(self, author_id, new_emoji, thread_id, thread_type, **kwargs):
if old_thread_id == thread_id and new_emoji != old_emoji:
log.info("{} changed the thread emoji. It will be changed back".format(author_id))
# Not currently possible in `fbchat`
# self.changeThreadEmoji(old_emoji, thread_id=thread_id)
self.changeThreadEmoji(old_emoji, thread_id=thread_id)
def onPeopleAdded(self, added_ids, author_id, thread_id, msg):
def onPeopleAdded(self, added_ids, author_id, thread_id, **kwargs):
if old_thread_id == thread_id and author_id != self.uid:
log.info("{} got added. They will be removed".format(added_ids))
for added_id in added_ids:
self.removeUserFromGroup(added_id, thread_id=thread_id)
def onPersonRemoved(self, removed_id, author_id, thread_id, msg):
def onPersonRemoved(self, removed_id, author_id, thread_id, **kwargs):
# No point in trying to add ourself
if old_thread_id == thread_id and removed_id != self.uid and author_id != self.uid:
log.info("{} got removed. They will be re-added".format(removed_id))
self.addUsersToGroup(removed_id, thread_id=thread_id)
def onTitleChange(self, mid, author_id, new_title, thread_id, thread_type, ts, metadata, msg):
def onTitleChange(self, author_id, new_title, thread_id, thread_type, **kwargs):
if old_thread_id == thread_id and old_title != new_title:
log.info("{} changed the thread title. It will be changed back".format(author_id))
self.changeGroupTitle(old_title, thread_id=thread_id)
self.changeThreadTitle(old_title, thread_id=thread_id, thread_type=thread_type)
def onNicknameChange(self, mid, author_id, changed_for, new_nickname, thread_id, thread_type, ts, metadata, msg):
if old_thread_id == thread_id and changed_for in old_nicknames:
def onNicknameChange(self, author_id, changed_for, new_nickname, thread_id, thread_type, **kwargs):
if old_thread_id == thread_id and changed_for in old_nicknames and old_nicknames[changed_for] != new_nickname:
log.info("{} changed {}'s' nickname. It will be changed back".format(author_id, changed_for))
# Not currently possible in `fbchat`
# self.changeNickname(old_nicknames[changed_for], changed_for, thread_id=thread_id, thread_type=thread_type)
self.changeNickname(old_nicknames[changed_for], changed_for, thread_id=thread_id, thread_type=thread_type)
client = KeepBot("<email>", "<password>")
client.listen()

View File

@@ -4,13 +4,13 @@ from fbchat import log, Client
from fbchat.models import *
class RemoveBot(Client):
def onMessage(self, mid, author_id, message, thread_id, thread_type, ts, metadata, msg):
def onMessage(self, author_id, message, thread_id, thread_type, **kwargs):
# We can only kick people from group chats, so no need to try if it's a user chat
if message == 'Remove me!' and thread_type == ThreadType.GROUP:
log.info("{} will be removed from {}".format(author_id, thread_id)))
self.removeUserFromGroup(user_id, thread_id=thread_id)
log.info("{} will be removed from {}".format(author_id, thread_id))
self.removeUserFromGroup(author_id, thread_id=thread_id)
else:
log.info("Message from {} in {} ({}): {}".format(author_id, thread_id, thread_type.name, message)))
log.info("Message from {} in {} ({}): {}".format(author_id, thread_id, thread_type.name, message))
client = RemoveBot("<email>", "<password>")
client.listen()
client.listen()