Merge pull request #499 from carpedm20/session-in-models

Add ThreadABC helper, and move a bunch of methods out of Client
This commit is contained in:
Mads Marquart
2020-01-09 11:33:45 +01:00
26 changed files with 1011 additions and 1642 deletions

View File

@@ -5,15 +5,11 @@ session = fbchat.Session.login("<email>", "<password>")
print("Own id: {}".format(sesion.user_id))
# Create helper client class
client = fbchat.Client(session)
# Create helper User class
user = fbchat.Thread(session=session, id=session.user_id)
# Send a message to yourself
client.send(
fbchat.Message(text="Hi me!"),
thread_id=session.user_id,
thread_type=fbchat.ThreadType.USER,
)
user.send(fbchat.Message(text="Hi me!"))
# Log the user out
session.logout()

View File

@@ -2,15 +2,15 @@ import fbchat
# Subclass fbchat.Client and override required methods
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.id)
self.mark_as_read(thread_id)
def on_message(self, author_id, message_object, thread, **kwargs):
self.mark_as_delivered(thread.id, message_object.id)
self.mark_as_read(thread.id)
print("{} from {} in {}".format(message_object, thread_id, thread_type.name))
print("{} from {} in {}".format(message_object, thread))
# If you're not the author, echo
if author_id != self.session.user_id:
self.send(message_object, thread_id=thread_id, thread_type=thread_type)
thread.send(message_object)
session = fbchat.Session.login("<email>", "<password>")

View File

@@ -39,8 +39,13 @@ threads += client.fetch_thread_list(offset=20, limit=10)
print("Threads: {}".format(threads))
# If we have a thread id, we can use `fetch_thread_info` to fetch a `Thread` object
thread = client.fetch_thread_info("<thread id>")["<thread id>"]
print("thread's name: {}".format(thread.name))
# Gets the last 10 messages sent to the thread
messages = client.fetch_thread_messages(thread_id="<thread id>", limit=10)
messages = thread.fetch_messages(limit=10)
# Since the message come in reversed order, reverse them
messages.reverse()
@@ -49,22 +54,15 @@ for message in messages:
print(message.text)
# If we have a thread id, we can use `fetch_thread_info` to fetch a `Thread` object
thread = client.fetch_thread_info("<thread id>")["<thread id>"]
print("thread's name: {}".format(thread.name))
print("thread's type: {}".format(thread.type))
# `search_for_threads` searches works like `search_for_users`, but gives us a list of threads instead
thread = client.search_for_threads("<name of thread>")[0]
print("thread's name: {}".format(thread.name))
print("thread's type: {}".format(thread.type))
# Here should be an example of `getUnread`
# Print image url for 20 last images from thread.
images = client.fetch_thread_images("<thread id>")
images = thread.fetch_images()
for image in itertools.islice(image, 20):
print(image.large_preview_url)

View File

@@ -4,94 +4,64 @@ session = fbchat.Session.login("<email>", "<password>")
client = fbchat.Client(session)
thread_id = "1234567890"
thread_type = fbchat.ThreadType.GROUP
thread = User(session=session, id=session.user_id)
# thread = User(session=session, id="0987654321")
# thread = Group(session=session, id="1234567890")
# Will send a message to the thread
client.send(
fbchat.Message(text="<message>"), thread_id=thread_id, thread_type=thread_type
)
thread.send(fbchat.Message(text="<message>"))
# Will send the default `like` emoji
client.send(
fbchat.Message(emoji_size=fbchat.EmojiSize.LARGE),
thread_id=thread_id,
thread_type=thread_type,
)
thread.send(fbchat.Message(emoji_size=fbchat.EmojiSize.LARGE))
# Will send the emoji `👍`
client.send(
fbchat.Message(text="👍", emoji_size=fbchat.EmojiSize.LARGE),
thread_id=thread_id,
thread_type=thread_type,
)
thread.send(fbchat.Message(text="👍", emoji_size=fbchat.EmojiSize.LARGE))
# Will send the sticker with ID `767334476626295`
client.send(
fbchat.Message(sticker=fbchat.Sticker("767334476626295")),
thread_id=thread_id,
thread_type=thread_type,
)
thread.send(fbchat.Message(sticker=fbchat.Sticker("767334476626295")))
# Will send a message with a mention
client.send(
thread.send(
fbchat.Message(
text="This is a @mention",
mentions=[fbchat.Mention(thread_id, offset=10, length=8)],
),
thread_id=thread_id,
thread_type=thread_type,
)
)
# Will send the image located at `<image path>`
client.send_local_image(
"<image path>",
message=fbchat.Message(text="This is a local image"),
thread_id=thread_id,
thread_type=thread_type,
thread.send_local_image(
"<image path>", message=fbchat.Message(text="This is a local image")
)
# Will download the image at the URL `<image url>`, and then send it
client.send_remote_image(
"<image url>",
message=fbchat.Message(text="This is a remote image"),
thread_id=thread_id,
thread_type=thread_type,
thread.send_remote_image(
"<image url>", message=fbchat.Message(text="This is a remote image")
)
# Only do these actions if the thread is a 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)
# Will add the user with ID `<user id>` to the thread
client.add_users_to_group("<user id>", thread_id=thread_id)
# Will add the users with IDs `<1st user id>`, `<2nd user id>` and `<3th user id>` to the thread
client.add_users_to_group(
["<1st user id>", "<2nd user id>", "<3rd user id>"], thread_id=thread_id
)
if isinstance(thread, fbchat.Group):
# Will remove the user with ID `<user id>` from the group
thread.remove_participant("<user id>")
# Will add the users with IDs `<1st user id>`, `<2nd user id>` and `<3th user id>` to the group
thread.add_participants(["<1st user id>", "<2nd user id>", "<3rd user id>"])
# Will change the title of the group to `<title>`
thread.change_title("<title>")
# Will change the nickname of the user `<user_id>` to `<new nickname>`
client.change_nickname(
"<new nickname>", "<user id>", thread_id=thread_id, thread_type=thread_type
)
thread.set_nickname(fbchat.User(session=session, id="<user id>"), "<new nickname>")
# Will change the title of the thread to `<title>`
client.change_thread_title("<title>", thread_id=thread_id, thread_type=thread_type)
# Will set the typing status of the thread to `TYPING`
client.set_typing_status(
fbchat.TypingStatus.TYPING, thread_id=thread_id, thread_type=thread_type
)
# Will set the typing status of the thread
thread.start_typing()
# Will change the thread color to `MESSENGER_BLUE`
client.change_thread_color(fbchat.ThreadColor.MESSENGER_BLUE, thread_id=thread_id)
thread.set_color(fbchat.ThreadColor.MESSENGER_BLUE)
# Will change the thread emoji to `👍`
client.change_thread_emoji("👍", thread_id=thread_id)
thread.set_emoji("👍")
# Will react to a message with a 😍 emoji
client.react_to_message("<message id>", fbchat.MessageReaction.LOVE)
# message = fbchat.Message(session=session, id="<message id>")
#
# # Will react to a message with a 😍 emoji
# message.react(fbchat.MessageReaction.LOVE)

View File

@@ -16,50 +16,48 @@ old_nicknames = {
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:
def on_color_change(self, author_id, new_color, thread, **kwargs):
if old_thread_id == thread.id and old_color != new_color:
print(
"{} changed the thread color. It will be changed back".format(author_id)
)
self.change_thread_color(old_color, thread_id=thread_id)
thread.set_color(old_color)
def on_emoji_change(self, author_id, new_emoji, thread_id, thread_type, **kwargs):
if old_thread_id == thread_id and new_emoji != old_emoji:
def on_emoji_change(self, author_id, new_emoji, thread, **kwargs):
if old_thread_id == thread.id and new_emoji != old_emoji:
print(
"{} changed the thread emoji. It will be changed back".format(author_id)
)
self.change_thread_emoji(old_emoji, thread_id=thread_id)
thread.set_emoji(old_emoji)
def on_people_added(self, added_ids, author_id, thread_id, **kwargs):
if old_thread_id == thread_id and author_id != self.session.user_id:
def on_people_added(self, added_ids, author_id, thread, **kwargs):
if old_thread_id == thread.id and author_id != self.session.user_id:
print("{} got added. They will be removed".format(added_ids))
for added_id in added_ids:
self.remove_user_from_group(added_id, thread_id=thread_id)
thread.remove_participant(added_id)
def on_person_removed(self, removed_id, author_id, thread_id, **kwargs):
def on_person_removed(self, removed_id, author_id, thread, **kwargs):
# No point in trying to add ourself
if (
old_thread_id == thread_id
old_thread_id == thread.id
and removed_id != self.session.user_id
and author_id != self.session.user_id
):
print("{} got removed. They will be re-added".format(removed_id))
self.add_users_to_group(removed_id, thread_id=thread_id)
thread.add_participants(removed_id)
def on_title_change(self, author_id, new_title, thread_id, thread_type, **kwargs):
if old_thread_id == thread_id and old_title != new_title:
def on_title_change(self, author_id, new_title, thread, **kwargs):
if old_thread_id == thread.id and old_title != new_title:
print(
"{} changed the thread title. It will be changed back".format(author_id)
)
self.change_thread_title(
old_title, thread_id=thread_id, thread_type=thread_type
)
thread.set_title(old_title)
def on_nickname_change(
self, author_id, changed_for, new_nickname, thread_id, thread_type, **kwargs
self, author_id, changed_for, new_nickname, thread, **kwargs
):
if (
old_thread_id == thread_id
old_thread_id == thread.id
and changed_for in old_nicknames
and old_nicknames[changed_for] != new_nickname
):
@@ -68,11 +66,8 @@ class KeepBot(fbchat.Client):
author_id, changed_for
)
)
self.change_nickname(
old_nicknames[changed_for],
changed_for,
thread_id=thread_id,
thread_type=thread_type,
thread.set_nickname(
changed_for, old_nicknames[changed_for],
)

View File

@@ -2,21 +2,17 @@ import fbchat
class RemoveBot(fbchat.Client):
def on_message(self, author_id, message_object, thread_id, thread_type, **kwargs):
def on_message(self, author_id, message_object, thread, **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 == fbchat.ThreadType.GROUP
):
print("{} will be removed from {}".format(author_id, thread_id))
self.remove_user_from_group(author_id, thread_id=thread_id)
if message_object.text == "Remove me!" and isinstance(thread, fbchat.Group):
print("{} will be removed from {}".format(author_id, thread))
thread.remove_participant(author_id)
else:
# Sends the data to the inherited on_message, so that we can still see when a message is recieved
super(RemoveBot, self).on_message(
author_id=author_id,
message_object=message_object,
thread_id=thread_id,
thread_type=thread_type,
thread=thread,
**kwargs,
)