forked from x4nth055/pythoncode-tutorials
-
Notifications
You must be signed in to change notification settings - Fork 1
/
messenger_bot.py
38 lines (27 loc) · 1.12 KB
/
messenger_bot.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
from fbchat import Client
from fbchat.models import Message, MessageReaction
# facebook user credentials
username = "username.or.email"
password = "password"
# login
client = Client(username, password)
# get 20 users you most recently talked to
users = client.fetchThreadList()
print(users)
# 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:", 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!")
# let's logout
client.logout()