From 78949e8ad5907a6ddaa16767020243fc150c8985 Mon Sep 17 00:00:00 2001 From: Mads Marquart Date: Wed, 8 Jan 2020 10:45:41 +0100 Subject: [PATCH] Update examples Only `import fbchat`, and update to initialize Client using Session --- examples/basic_usage.py | 21 +++++++++++++++------ examples/echobot.py | 10 ++++++---- examples/fetch.py | 11 ++++++----- examples/interract.py | 38 ++++++++++++++++++++++---------------- examples/keepbot.py | 13 +++++++------ examples/removebot.py | 16 ++++++++++------ 6 files changed, 66 insertions(+), 43 deletions(-) diff --git a/examples/basic_usage.py b/examples/basic_usage.py index b64f30e..f5c8900 100644 --- a/examples/basic_usage.py +++ b/examples/basic_usage.py @@ -1,10 +1,19 @@ -from fbchat import Client -from fbchat.models import * +import fbchat -client = Client("", "") +# Log the user in +session = fbchat.Session.login("", "") -print("Own id: {}".format(client.uid)) +print("Own id: {}".format(sesion.user_id)) -client.send(Message(text="Hi me!"), thread_id=client.uid, thread_type=ThreadType.USER) +# Create helper client class +client = fbchat.Client(session) -client.logout() +# Send a message to yourself +client.send( + fbchat.Message(text="Hi me!"), + thread_id=session.user_id, + thread_type=fbchat.ThreadType.USER, +) + +# Log the user out +session.logout() diff --git a/examples/echobot.py b/examples/echobot.py index 2a4ae0f..4935248 100644 --- a/examples/echobot.py +++ b/examples/echobot.py @@ -1,7 +1,7 @@ -from fbchat import Client +import fbchat # Subclass fbchat.Client and override required methods -class EchoBot(Client): +class EchoBot(fbchat.Client): def on_message(self, author_id, message_object, thread_id, thread_type, **kwargs): self.mark_as_delivered(thread_id, message_object.uid) self.mark_as_read(thread_id) @@ -13,5 +13,7 @@ class EchoBot(Client): self.send(message_object, thread_id=thread_id, thread_type=thread_type) -client = EchoBot("", "") -client.listen() +session = fbchat.Session.login("", "") + +echo_bot = EchoBot(session) +echo_bot.listen() diff --git a/examples/fetch.py b/examples/fetch.py index 26be45b..0c9518f 100644 --- a/examples/fetch.py +++ b/examples/fetch.py @@ -1,8 +1,9 @@ -from itertools import islice -from fbchat import Client -from fbchat.models import * +import itertools +import fbchat -client = Client("", "") +session = fbchat.Session.login("", "") + +client = fbchat.Client(session) # Fetches a list of all users you're currently chatting with, as `User` objects users = client.fetch_all_users() @@ -65,5 +66,5 @@ print("thread's type: {}".format(thread.type)) # Print image url for 20 last images from thread. images = client.fetch_thread_images("") -for image in islice(image, 20): +for image in itertools.islice(image, 20): print(image.large_preview_url) diff --git a/examples/interract.py b/examples/interract.py index 2d2ed27..5b26108 100644 --- a/examples/interract.py +++ b/examples/interract.py @@ -1,37 +1,43 @@ -from fbchat import Client -from fbchat.models import * +import fbchat -client = Client("", "") +session = fbchat.Session.login("", "") + +client = fbchat.Client(session) thread_id = "1234567890" -thread_type = ThreadType.GROUP +thread_type = fbchat.ThreadType.GROUP # Will send a message to the thread -client.send(Message(text=""), thread_id=thread_id, thread_type=thread_type) +client.send( + fbchat.Message(text=""), thread_id=thread_id, thread_type=thread_type +) # Will send the default `like` emoji client.send( - Message(emoji_size=EmojiSize.LARGE), thread_id=thread_id, thread_type=thread_type + fbchat.Message(emoji_size=fbchat.EmojiSize.LARGE), + thread_id=thread_id, + thread_type=thread_type, ) # Will send the emoji `👍` client.send( - Message(text="👍", emoji_size=EmojiSize.LARGE), + fbchat.Message(text="👍", emoji_size=fbchat.EmojiSize.LARGE), thread_id=thread_id, thread_type=thread_type, ) # Will send the sticker with ID `767334476626295` client.send( - Message(sticker=Sticker("767334476626295")), + fbchat.Message(sticker=fbchat.Sticker("767334476626295")), thread_id=thread_id, thread_type=thread_type, ) # Will send a message with a mention client.send( - Message( - text="This is a @mention", mentions=[Mention(thread_id, offset=10, length=8)] + fbchat.Message( + text="This is a @mention", + mentions=[fbchat.Mention(thread_id, offset=10, length=8)], ), thread_id=thread_id, thread_type=thread_type, @@ -40,7 +46,7 @@ client.send( # Will send the image located at `` client.send_local_image( "", - message=Message(text="This is a local image"), + message=fbchat.Message(text="This is a local image"), thread_id=thread_id, thread_type=thread_type, ) @@ -48,14 +54,14 @@ client.send_local_image( # Will download the image at the URL ``, and then send it client.send_remote_image( "", - message=Message(text="This is a remote image"), + message=fbchat.Message(text="This is a remote image"), thread_id=thread_id, thread_type=thread_type, ) # Only do these actions if the thread is a group -if thread_type == ThreadType.GROUP: +if thread_type == fbchat.ThreadType.GROUP: # Will remove the user with ID `` from the thread client.remove_user_from_group("", thread_id=thread_id) @@ -78,14 +84,14 @@ client.change_thread_title("", thread_id=thread_id, thread_type=thread_ty # Will set the typing status of the thread to `TYPING` client.set_typing_status( - TypingStatus.TYPING, thread_id=thread_id, thread_type=thread_type + fbchat.TypingStatus.TYPING, thread_id=thread_id, thread_type=thread_type ) # Will change the thread color to `MESSENGER_BLUE` -client.change_thread_color(ThreadColor.MESSENGER_BLUE, thread_id=thread_id) +client.change_thread_color(fbchat.ThreadColor.MESSENGER_BLUE, thread_id=thread_id) # Will change the thread emoji to `👍` client.change_thread_emoji("👍", thread_id=thread_id) # Will react to a message with a 😍 emoji -client.react_to_message("<message id>", MessageReaction.LOVE) +client.react_to_message("<message id>", fbchat.MessageReaction.LOVE) diff --git a/examples/keepbot.py b/examples/keepbot.py index e4a701b..414048c 100644 --- a/examples/keepbot.py +++ b/examples/keepbot.py @@ -1,11 +1,10 @@ -from fbchat import Client -from fbchat.models import * +import fbchat # Change this to your group id old_thread_id = "1234567890" # Change these to match your liking -old_color = ThreadColor.MESSENGER_BLUE +old_color = fbchat.ThreadColor.MESSENGER_BLUE old_emoji = "👍" old_title = "Old group chat name" old_nicknames = { @@ -16,7 +15,7 @@ old_nicknames = { } -class KeepBot(Client): +class KeepBot(fbchat.Client): def on_color_change(self, author_id, new_color, thread_id, thread_type, **kwargs): if old_thread_id == thread_id and old_color != new_color: print( @@ -77,5 +76,7 @@ class KeepBot(Client): ) -client = KeepBot("<email>", "<password>") -client.listen() +session = fbchat.Session.login("<email>", "<password>") + +keep_bot = KeepBot(session) +keep_bot.listen() diff --git a/examples/removebot.py b/examples/removebot.py index 90f63c1..4bb6d57 100644 --- a/examples/removebot.py +++ b/examples/removebot.py @@ -1,11 +1,13 @@ -from fbchat import Client -from fbchat.models import * +import fbchat -class RemoveBot(Client): +class RemoveBot(fbchat.Client): def on_message(self, author_id, message_object, 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_object.text == "Remove me!" and thread_type == ThreadType.GROUP: + if ( + message_object.text == "Remove me!" + and thread_type == fbchat.ThreadType.GROUP + ): print("{} will be removed from {}".format(author_id, thread_id)) self.remove_user_from_group(author_id, thread_id=thread_id) else: @@ -19,5 +21,7 @@ class RemoveBot(Client): ) -client = RemoveBot("<email>", "<password>") -client.listen() +session = fbchat.Session.login("<email>", "<password>") + +remove_bot = RemoveBot(session) +remove_bot.listen()