-
Notifications
You must be signed in to change notification settings - Fork 403
Expand file tree
/
Copy pathmulti-threading.py
More file actions
50 lines (37 loc) · 1.37 KB
/
multi-threading.py
File metadata and controls
50 lines (37 loc) · 1.37 KB
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
39
40
41
42
43
44
45
46
47
48
49
50
# -*- coding: UTF-8 -*-
from fbchat import Client, logging
from fbchat.models import *
import threading
import sys
user = "<email>"
password = "<password>"
# Subclass fbchat.Client and override required methods
class MessagePrinter(Client):
def onMessage(self, author_id, message_object, thread_id, thread_type, **kwargs):
self.markAsDelivered(thread_id, message_object.uid)
self.markAsRead(thread_id)
print("\nIncoming message: {}".format(message_object.text))
# Creating some feedback on login since we will disable INFO logging
def onLoggedIn(self, email=None):
print("Login of {} successful.".format(email))
# Login and set logging level to WARNING to avoid some unessential output
client1 = Client(user, password, logging_level=logging.WARNING)
client2 = MessagePrinter(user, password, logging_level=logging.WARNING)
# Creating and starting a separate thread for receiving messages
t1 = threading.Thread(target=client2.listen, daemon=True)
t1.start()
# Loop checking for, and sending messages
try:
while True:
payload = input("Message: ")
if payload:
client1.send(
Message(text=payload),
thread_id=client1.uid,
thread_type=ThreadType.USER,
)
# Clean-up on exit
except KeyboardInterrupt:
client1.logout()
client2.logout()
sys.exit(0)