How to Make Facebook Messenger Bot using Python
Being able to automate tasks and create helpful bots in Facebook Messenger bot looks to be intriguing and cool, so in this tutorial, we’ll look at how to connect in Python to Facebook Messenger and perform a variety of great things!
What is a Facebook Messenger bot?
Chatbots allow businesses to automate a variety of low-level customer support activities. Customers may interact with a bot to answer basic pre-purchase inquiries or complete the early phases of an RMA returns request instead of waiting on hold to speak with a support representative.
When it comes to engaging with customers, a variety of platforms allow chatbots, but Facebook Messenger is arguably the most significant.
Messenger bot in python
We’ll use the fbchat library, which works by simulating a browser. This entails making identical GET/POST requests to fool Facebook into believing it is accessing the website regularly. As a result, this API isn’t official, and it doesn’t require an API key; instead, it requires your Facebook account credentials.
You’ll need to install the fbchat library first:
pip3 install fbchat
Now, let’s import fbchat: Create an empty python file or open an interactive shell or jupyter notebook and follow along.
#1001 programming , Facebook Messenger bot from fbchat import Client from fbchat.models import Message
Let’s first log in to our Facebook account:
Note : You must input valid Facebook credentials; else, continuing this instruction will be pointless.
#1001 programming , Facebook Messenger bot # facebook username & password username = "username.or.email" password = "password" # login client = Client(username, password)
We now have the client object, which contains a number of helpful methods.
Let’s take a look at the users you’ve lately spoken with:
#1001 programming , Facebook Messenger bot # get users users = client.fetchThreadList() print(users)
This will produce a list of threads, each of which can be either a user or a group.
Let’s look for our closest buddy and get as much information as we can on these users:
#1001 programming , Facebook Messenger bot # get the detailed informations about users detailed_users = [ list(client.fetchThreadInfo(user.uid).values())[0] for user in users ]
For example , the message count field of a thread object counts the amount of messages sent between you and that thread; we may order by this attribute:
#1001 programming , Facebook Messenger bot # sort by number of messages sorted_detailed_users = sorted(detailed_users, key=lambda u: u.message_count, reverse=True)
Now that we have a list of 30-40 people ordered by message count, we can quickly find the best buddy by:
#1001 programming , Facebook Messenger bot # print the best friend! best_friend = sorted_detailed_users[0] print("Best friend:", best_friend.name, "with a message count of", best_friend.message_count)
sending a message :
#1001 programming , Facebook Messenger bot # message the best friend! client.send(Message(text=f"Congratulations {best_friend.name}, you are my best friend with {best_friend.message_count} messages!"), thread_id=best_friend.uid)
You may receive a list of all the users you’ve chatted to in Messenger by:
#1001 programming , Facebook Messenger bot # 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!")
Finally, make sure you logout when you’re done:
#1001 programming , Facebook Messenger bot # logout client.logout()
As you can see, there are a plethora of things you can do with this library, like creating automatic reply messages, a chatbot, an echobot, and a variety of other fascinating features. Please look through their official documents.
Full code
#1001 programming , python project #import library from fbchat import Client from fbchat.models import Message, MessageReaction # facebook username & password username = "username.or.email" password = "password" # login client = Client(username, password) # get list users you most recently talked to users = client.fetchThreadList() print(users) # get the detailed informations about 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:", best_friend.name, "with a message count of", best_friend.message_count) # message the best friend! client.send(Message( text=f"Congratulations {best_friend.name}, you are my best friend with {best_friend.message_count} messages!" ), thread_id=best_friend.uid) # 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!") # logout client.logout()