Skip to content

Commit bcc9619

Browse files
committed
added fbchat tutorial
1 parent ed70d15 commit bcc9619

File tree

4 files changed

+49
-0
lines changed

4 files changed

+49
-0
lines changed

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,3 +19,6 @@ This is a repository of all the tutorials of [The Python Code](https://www.thepy
1919
- [Building a Speech Emotion Recognizer using Scikit-learn](https://www.thepythoncode.com/article/building-a-speech-emotion-recognizer-using-sklearn). ([code](machine-learning/speech-emotion-recognition))
2020
- [Top 8 Python Libraries For Data Scientists and Machine Learning Engineers](https://www.thepythoncode.com/article/top-python-libraries-for-data-scientists).
2121

22+
- ### [General Python Topics](https://www.thepythoncode.com/topic/general-python-topics)
23+
- [How to Make Facebook Messenger bot in Python](https://www.thepythoncode.com/article/make-bot-fbchat-python). ([code](general/messenger-bot))
24+

general/messenger-bot/README.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# [How to Make Facebook Messenger bot in Python](https://www.thepythoncode.com/article/make-bot-fbchat-python)
2+
To run this:
3+
- `pip install -r requirements.txt`
4+
-
5+
```
6+
python messenger_bot.py
7+
```
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
from fbchat import Client
2+
from fbchat.models import Message, MessageReaction
3+
4+
# facebook user credentials
5+
username = "username.or.email"
6+
password = "password"
7+
8+
# login
9+
client = Client(username, password)
10+
11+
# get 20 users you most recently talked to
12+
users = client.fetchThreadList()
13+
print(users)
14+
15+
# get the detailed informations about these users
16+
detailed_users = [ list(client.fetchThreadInfo(user.uid).values())[0] for user in users ]
17+
18+
# sort by number of messages
19+
sorted_detailed_users = sorted(detailed_users, key=lambda u: u.message_count, reverse=True)
20+
21+
# print the best friend!
22+
best_friend = sorted_detailed_users[0]
23+
24+
print("Best friend:", best_friend.name, "with a message count of", best_friend.message_count)
25+
26+
# message the best friend!
27+
client.send(Message(
28+
text=f"Congratulations {best_friend.name}, you are my best friend with {best_friend.message_count} messages!"
29+
),
30+
thread_id=best_friend.uid)
31+
32+
# get all users you talked to in messenger in your account
33+
all_users = client.fetchAllUsers()
34+
35+
print("You talked with a total of", len(all_users), "users!")
36+
37+
# let's logout
38+
client.logout()
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
fbchat

0 commit comments

Comments
 (0)