From e581fb1590869066387b0044a253b67fbc48f0cd Mon Sep 17 00:00:00 2001 From: joakimed Date: Wed, 27 Feb 2019 17:34:47 +0100 Subject: [PATCH 1/3] Create multi-threading.py --- examples/multi-threading.py | 42 +++++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) create mode 100644 examples/multi-threading.py diff --git a/examples/multi-threading.py b/examples/multi-threading.py new file mode 100644 index 00000000..64b3d9d7 --- /dev/null +++ b/examples/multi-threading.py @@ -0,0 +1,42 @@ +# -*- coding: UTF-8 -*- + +from fbchat import Client +from fbchat.models import * +import threading + +# Subclass fbchat.Client and override required methods +class PrintMessage(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)) + + +# Logging in and setting logging level to WARNING to avoid some unessential output +client = PrintMessage("", "", logging_level=30) + + +def send(): + while True: + payload = input("Message: ") + if payload: + client.send( + Message(text=payload), thread_id=client.uid, thread_type=ThreadType.USER + ) + + +def receive(): + while True: + client.doOneListen() + + +# Creating and starting separate threads for handling the receiving and sending of messages +t1 = threading.Thread(target=receive) +t2 = threading.Thread(target=send) +t1.start() +t2.start() From e7e83c097048416b049ee9b3be0690f13a329ec9 Mon Sep 17 00:00:00 2001 From: joakimed Date: Fri, 1 Mar 2019 17:06:45 +0100 Subject: [PATCH 2/3] Update multi-threading.py --- examples/multi-threading.py | 43 ++++++++++++++++++++++--------------- 1 file changed, 26 insertions(+), 17 deletions(-) diff --git a/examples/multi-threading.py b/examples/multi-threading.py index 64b3d9d7..6402c787 100644 --- a/examples/multi-threading.py +++ b/examples/multi-threading.py @@ -1,11 +1,16 @@ # -*- coding: UTF-8 -*- -from fbchat import Client +from fbchat import Client, logging from fbchat.models import * +import sys import threading +user = "" +password = "" + + # Subclass fbchat.Client and override required methods -class PrintMessage(Client): +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) @@ -17,26 +22,30 @@ def onLoggedIn(self, email=None): print("Login of {} successful.".format(email)) -# Logging in and setting logging level to WARNING to avoid some unessential output -client = PrintMessage("", "", logging_level=30) +# 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() -def send(): +# Loop checking for, and sending messages +try: while True: payload = input("Message: ") if payload: - client.send( - Message(text=payload), thread_id=client.uid, thread_type=ThreadType.USER + 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) -def receive(): - while True: - client.doOneListen() - - -# Creating and starting separate threads for handling the receiving and sending of messages -t1 = threading.Thread(target=receive) -t2 = threading.Thread(target=send) -t1.start() -t2.start() From 535c9b1e0549121222cd483c2b7ccf9f20ada24b Mon Sep 17 00:00:00 2001 From: joakimed Date: Fri, 1 Mar 2019 17:13:50 +0100 Subject: [PATCH 3/3] Update multi-threading.py --- examples/multi-threading.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/examples/multi-threading.py b/examples/multi-threading.py index 6402c787..76471547 100644 --- a/examples/multi-threading.py +++ b/examples/multi-threading.py @@ -2,8 +2,8 @@ from fbchat import Client, logging from fbchat.models import * -import sys import threading +import sys user = "" password = "" @@ -48,4 +48,3 @@ def onLoggedIn(self, email=None): client1.logout() client2.logout() sys.exit(0) -