Quick Start Guide¶
This guide will help you get started with fbchat-muqit.
Prerequisites¶
Python 3.9 or higher
A Facebook account to use as Client. (Safer to use old unused account or new)
Basic knowledge of async/await in Python
Installation¶
pip install fbchat-muqit
Account Login¶
fbchat-muqit library uses cookies for authentication. Since due to recent changes in Facebook login via email and password has become very complex.
We use the already logged in browser cookies and trick the Facebook server thinking we are accessing the account normally from our browser. Learn more about how Facebook cookies work.
To get your Facebook account cookies. First login in your Facebook account and then add c3c fbstate extension in your browser. Open a your Facebook account in a browser tab and use this extension to get your account cookies. Copy the cookies and save them in a json file. We will use the cookies for authentication instead of email and password. We will call this account Client account.
Your First Bot¶
Let’s create a simple echo bot that replies to messages or send image files.:
from fbchat_muqit import Client, Message, EventType
client = Client(cookies_file_path="cookies.json")
# @client.event(EventType.LISTENING) you can also do this if you want a different name for the function instead of `on_listening`
@client.event
async def on_listening():
print(f"I'm online. Account name: {client.name} and uid: {client.uid}")
# fetch all users clinet is chatting with.
all_users = await client.fetch_all_users()
for user_id, user_data in all_users.items():
print(f"User Id: {user_id} and user name {user_data.name}")
@client.event
async def on_message(message: Message):
# To avoid spam check if sender_id is client's id or not
if message.sender_id != client.uid:
if message.text.startswith("/send image"):
await client.send_message("sending please wait...", message.thread_id)
await client.send_files_from_path(
file_paths=["/sdcard/my_picture.png", "/Download/hello.jpg"],
thread_id=message.thread_id
)
else:
# otherwise echo the message
await client.send_message(message.text, message.thread_id)
client.run()
Next Steps¶
Learn about authentication
Explore guides/sending-messages
Check out examples/basic-bot