Messages

class fbchat.Message(*, thread, id)[source]

Represents a Facebook message.

Example

>>> thread = fbchat.User(session=session, id="1234")
>>> message = fbchat.Message(thread=thread, id="mid.$XYZ")
thread

The thread that this message belongs to.

id

The message ID.

property session

The session to use when making requests.

delete()[source]

Delete the message (removes it only for the user).

If you want to delete multiple messages, please use Client.delete_messages.

Example

>>> message.delete()
unsend()[source]

Unsend the message (removes it for everyone).

The message must to be sent by you, and less than 10 minutes ago.

Example

>>> message.unsend()
react(reaction)[source]

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.

Parameters

reaction (Optional[str]) – Reaction emoji to use, or if None, removes reaction.

Example

>>> message.react("😍")
fetch()[source]

Fetch fresh MessageData object.

Example

>>> message = message.fetch()
>>> message.text
"The message text"
Return type

MessageData

static format_mentions(text, *args, **kwargs)[source]

Like str.format, but takes tuples with a thread id and text instead.

Return a tuple, with the formatted string and relevant mentions.

>>> Message.format_mentions("Hey {!r}! My name is {}", ("1234", "Peter"), ("4321", "Michael"))
("Hey 'Peter'! My name is Michael", [Mention(thread_id=1234, offset=4, length=7), Mention(thread_id=4321, offset=24, length=7)])
>>> Message.format_mentions("Hey {p}! My name is {}", ("1234", "Michael"), p=("4321", "Peter"))
('Hey Peter! My name is Michael', [Mention(thread_id=4321, offset=4, length=5), Mention(thread_id=1234, offset=22, length=7)])
class fbchat.Mention(*, thread_id, offset, length)[source]

Represents a @mention.

>>> fbchat.Mention(thread_id="1234", offset=5, length=2)
Mention(thread_id="1234", offset=5, length=2)
thread_id

The thread ID the mention is pointing at

offset

The character where the mention starts

length

The length of the mention

class fbchat.EmojiSize(Enum)[source]

Used to specify the size of a sent emoji.

LARGE = '369239383222810'
MEDIUM = '369239343222814'
SMALL = '369239263222822'
class fbchat.MessageData(*, thread, id, author, created_at, text=None, mentions=NOTHING, emoji_size=None, is_read=None, read_by=NOTHING, reactions=NOTHING, sticker=None, attachments=NOTHING, quick_replies=NOTHING, unsent=False, reply_to_id=None, replied_to=None, forwarded=False)[source]

Represents data in a Facebook message.

Inherits Message.

author

ID of the sender

created_at

When the message was sent

text

The actual message

mentions

A list of Mention objects

emoji_size

Size of a sent emoji

is_read

Whether the message is read

read_by

People IDs who read the message, only works with ThreadABC.fetch_messages

reactions

A dictionary with user’s IDs as keys, and their reaction as values

sticker

A Sticker

attachments

A list of attachments

quick_replies

A list of QuickReply

unsent

Whether the message is unsent (deleted for everyone)

reply_to_id

Message ID you want to reply to

replied_to

Replied message

forwarded

Whether the message was forwarded