Update examples

Only `import fbchat`, and update to initialize Client using Session
This commit is contained in:
Mads Marquart
2020-01-08 10:45:41 +01:00
parent 06b7e14c31
commit 78949e8ad5
6 changed files with 66 additions and 43 deletions

View File

@@ -1,10 +1,19 @@
from fbchat import Client
from fbchat.models import *
import fbchat
client = Client("<email>", "<password>")
# Log the user in
session = fbchat.Session.login("<email>", "<password>")
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()

View File

@@ -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("<email>", "<password>")
client.listen()
session = fbchat.Session.login("<email>", "<password>")
echo_bot = EchoBot(session)
echo_bot.listen()

View File

@@ -1,8 +1,9 @@
from itertools import islice
from fbchat import Client
from fbchat.models import *
import itertools
import fbchat
client = Client("<email>", "<password>")
session = fbchat.Session.login("<email>", "<password>")
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("<thread id>")
for image in islice(image, 20):
for image in itertools.islice(image, 20):
print(image.large_preview_url)

View File

@@ -1,37 +1,43 @@
from fbchat import Client
from fbchat.models import *
import fbchat
client = Client("<email>", "<password>")
session = fbchat.Session.login("<email>", "<password>")
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="<message>"), thread_id=thread_id, thread_type=thread_type)
client.send(
fbchat.Message(text="<message>"), 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 `<image path>`
client.send_local_image(
"<image path>",
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 `<image url>`, and then send it
client.send_remote_image(
"<image url>",
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 `<user id>` from the thread
client.remove_user_from_group("<user id>", thread_id=thread_id)
@@ -78,14 +84,14 @@ client.change_thread_title("<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)

View File

@@ -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()

View File

@@ -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()