diff --git a/docs/api.rst b/docs/api.rst index f1d8b35..942371d 100644 --- a/docs/api.rst +++ b/docs/api.rst @@ -28,8 +28,6 @@ Messages .. autoclass:: Mention .. autoclass:: EmojiSize(Enum) :undoc-members: -.. autoclass:: MessageReaction(Enum) - :undoc-members: Exceptions ---------- diff --git a/docs/intro.rst b/docs/intro.rst index 6f8f849..5dc9f90 100644 --- a/docs/intro.rst +++ b/docs/intro.rst @@ -82,12 +82,10 @@ Message IDs Every message you send on Facebook has a unique ID, and every action you do in a thread, like changing a nickname or adding a person, has a unique ID too. -Some of ``fbchat``'s functions require these ID's, like `Client.react_to_message`, -and some of then provide this ID, like `Client.send`. This snippet shows how to send a message, and then use the returned ID to react to that message with a 😍 emoji:: - message_id = thread.send(Message(text='message')) - client.react_to_message(message_id, MessageReaction.LOVE) + message = thread.send_text("A message!") + message.react("😍") .. _intro_interacting: diff --git a/examples/interract.py b/examples/interract.py index 16c6f7a..279c0cb 100644 --- a/examples/interract.py +++ b/examples/interract.py @@ -63,4 +63,4 @@ thread.set_emoji("👍") message = fbchat.Message(session=session, id="") # Will react to a message with a 😍 emoji -message.react(fbchat.MessageReaction.LOVE) +message.react("😍") diff --git a/fbchat/__init__.py b/fbchat/__init__.py index ef39f5c..afbc45d 100644 --- a/fbchat/__init__.py +++ b/fbchat/__init__.py @@ -18,7 +18,7 @@ from ._thread import ThreadLocation, ThreadColor, ThreadABC, Thread from ._user import TypingStatus, User, UserData, ActiveStatus from ._group import Group, GroupData from ._page import Page, PageData -from ._message import EmojiSize, MessageReaction, Mention, Message +from ._message import EmojiSize, Mention, Message from ._attachment import Attachment, UnsentMessage, ShareAttachment from ._sticker import Sticker from ._location import LocationAttachment, LiveLocationAttachment diff --git a/fbchat/_client.py b/fbchat/_client.py index 9568ba5..0240638 100644 --- a/fbchat/_client.py +++ b/fbchat/_client.py @@ -10,7 +10,7 @@ from ._thread import ThreadLocation, ThreadColor from ._user import TypingStatus, User, UserData, ActiveStatus from ._group import Group, GroupData from ._page import Page, PageData -from ._message import EmojiSize, MessageReaction, Mention, Message +from ._message import EmojiSize, Mention, Message from ._attachment import Attachment from ._sticker import Sticker from ._location import LocationAttachment, LiveLocationAttachment @@ -1259,14 +1259,11 @@ class Client: i = d["deltaMessageReaction"] mid = i["messageId"] author_id = str(i["userId"]) - reaction = ( - MessageReaction(i["reaction"]) if i.get("reaction") else None - ) add_reaction = not bool(i["action"]) if add_reaction: self.on_reaction_added( mid=mid, - reaction=reaction, + reaction=i.get("reaction"), author_id=author_id, thread=get_thread(metadata), at=at, @@ -1855,7 +1852,7 @@ class Client: Args: mid: Message ID, that user reacted to - reaction (MessageReaction): Reaction + reaction: The added reaction. Not limited to the ones in `Message.react` add_reaction: Whether user added or removed reaction author_id: The ID of the person who reacted to the message thread: Thread that the action was sent to. See :ref:`intro_threads` @@ -1863,7 +1860,7 @@ class Client: """ log.info( "{} reacted to message {} with {} in {}".format( - author_id, mid, reaction.name, thread + author_id, mid, reaction, thread ) ) diff --git a/fbchat/_message.py b/fbchat/_message.py index f7e273d..f9807c7 100644 --- a/fbchat/_message.py +++ b/fbchat/_message.py @@ -29,19 +29,6 @@ class EmojiSize(Enum): return None -class MessageReaction(Enum): - """Used to specify a message reaction.""" - - HEART = "❤" - LOVE = "😍" - SMILE = "😆" - WOW = "😮" - SAD = "😢" - ANGRY = "😠" - YES = "👍" - NO = "👎" - - @attrs_default class Mention: """Represents a ``@mention``.""" @@ -74,6 +61,9 @@ class Mention: } +SENDABLE_REACTIONS = ("❤", "😍", "😆", "😮", "😢", "😠", "👍", "👎") + + @attrs_default class Message: """Represents a Facebook message.""" @@ -93,18 +83,26 @@ class Message: data = {"message_id": self.id} j = self.session._payload_post("/messaging/unsend_message/?dpr=1", data) - def react(self, reaction: Optional[MessageReaction]): + def react(self, reaction: Optional[str]): """React to the message, or removes reaction. + Currently, you can use "❤", "😍", "😆", "😮", "😢", "😠", "👍" or "👎". It + should be possible to add support for more, but we haven't figured that out yet. + Args: - reaction: Reaction emoji to use, if ``None`` removes reaction + reaction: Reaction emoji to use, or if ``None``, removes reaction. """ + if reaction and reaction not in SENDABLE_REACTIONS: + raise ValueError( + "Invalid reaction! Please use one of: {}".format(SENDABLE_REACTIONS) + ) + data = { "action": "ADD_REACTION" if reaction else "REMOVE_REACTION", "client_mutation_id": "1", "actor_id": self.session.user_id, "message_id": self.id, - "reaction": reaction.value if reaction else None, + "reaction": reaction, } data = { "doc_id": 1491398900900362, @@ -190,7 +188,7 @@ class MessageData(Message): is_read = attr.ib(None) #: A list of people IDs who read the message, works only with `Client.fetch_thread_messages` read_by = attr.ib(factory=list) - #: A dictionary with user's IDs as keys, and their `MessageReaction` as values + #: A dictionary with user's IDs as keys, and their reaction as values reactions = attr.ib(factory=dict) #: A `Sticker` sticker = attr.ib(None) @@ -266,8 +264,7 @@ class MessageData(Message): if _util.millis_to_datetime(int(receipt["watermark"])) >= created_at ], reactions={ - str(r["user"]["id"]): MessageReaction._extend_if_invalid(r["reaction"]) - for r in data["message_reactions"] + str(r["user"]["id"]): r["reaction"] for r in data["message_reactions"] }, sticker=_sticker.Sticker._from_graphql(data.get("sticker")), attachments=attachments, diff --git a/tests/test_message_management.py b/tests/test_message_management.py index 9db09a4..66440b4 100644 --- a/tests/test_message_management.py +++ b/tests/test_message_management.py @@ -1,16 +1,11 @@ import pytest -from fbchat import Message, MessageReaction +from fbchat import Message from utils import subset pytestmark = pytest.mark.online -def test_set_reaction(client): - mid = client.send(Message(text="This message will be reacted to")) - client.react_to_message(mid, MessageReaction.LOVE) - - def test_delete_messages(client): text1 = "This message will stay" text2 = "This message will be removed"