Code for How to Make Facebook Messenger Bot in Python Tutorial

View on Github

from fbchat import Client
from fbchat.models import Message, MessageReaction

# facebook user credentials
username = ""
password = "password"

# login
client = Client(username, password)

# get 20 users you most recently talked to
users = client.fetchThreadList()

# get the detailed informations about these users
detailed_users = [ list(client.fetchThreadInfo(user.uid).values())[0] for user in users ]

# sort by number of messages
sorted_detailed_users = sorted(detailed_users, key=lambda u: u.message_count, reverse=True)

# print the best friend!
best_friend = sorted_detailed_users[0]

print("Best friend:",, "with a message count of", best_friend.message_count)

# message the best friend!
                    text=f"Congratulations {}, you are my best friend with {best_friend.message_count} messages!"

# get all users you talked to in messenger in your account
all_users = client.fetchAllUsers()

print("You talked with a total of", len(all_users), "users!")

# let's logout