-
Notifications
You must be signed in to change notification settings - Fork 411
Create multi-threading.py (example) #401
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,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) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We should clean the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. And then there'd be no need to make |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd argue there's really not a big need to log out (it's not shown in the other examples, either)