fbchat-dev / fbchat

Facebook Chat (Messenger) for Python
https://fbchat.readthedocs.io
BSD 3-Clause "New" or "Revised" License
1.19k stars 412 forks source link

how to store/write the text messages using client.listen() #244

Closed sumit6192 closed 6 years ago

sumit6192 commented 6 years ago

i find this python package awesome but have some doubts:

from fbchat import Client from fbchat.models import * client = Client('email', 'passw') client.listen() gives me all the incoming messages but how do i store or write them in a file so that i can respond them later using client.send(Message(text='message'), thread_id=id, thread_type=ThreadType.USER/GROUP)

madsmtm commented 6 years ago

RTFD

Subclass the Client, and use onMessage to save the information you need to a file.

sumit6192 commented 6 years ago

sorry to bother you again i am a newbee in python could you just give an example how to i tried: with open("C:/Users/sumit.k.das/Desktop/output.txt", "wb") as text_file: text_file.write(client.listen())

but nothing happened

madsmtm commented 6 years ago

You could do something like this, then:

with open(file) as text_file:
    class CustomClient(Client):
        def onMessage(self, message_object=None, **kwargs):
            text_file.write(message_object.text)

    client = CustomClient(email, password)
    client.listen()

Though there are many other approaches to this, and the described method isn't the best.

sumit6192 commented 6 years ago

Tried this one but no able to write the incoming messages.are you sure we can write the incoming messages using client.listen() to a file..

sumit6192 commented 6 years ago

i get output - Logging in sumit280@gmail.com... Login of sumit280@gmail.com successful. Listening... Inbox event: 1, 3, 3

but nothing gets written on my text file.. tried different options also but not able to write incoming messages using client.listen()

madsmtm commented 6 years ago

Client.listen won't have a return value, so you can't do something like print(client.listen()). This function calls different methods when the client e.g. when it recieves a message, or when someone kicks a person from a group chat. So if you overwrite it like I showed you, you should be able to do what you want with the message_object parameter, and write the .text part of the message to a file. Though you should probably open the file with append, so that you can actually write to the file, like this:

with open(file, 'a') as text_file:
    pass # Do stuff here
madsmtm commented 6 years ago

But it's beyond me to properly explain how subclassing and method overwriting works, try find a tutorial on this first.

sumit6192 commented 6 years ago

i triend to write in append mode too but not able to write the incoming messages-

with open('file path','a') as text_file:
    class CustomClient(Client):
        def onMessage(self, message_object=None, **kwargs):
            text_file.write(message_object.text)

    client = CustomClient(email, password)
    client.listen()

it is just returning Listening... Inbox event: 1, 3, 3

i just need to write the incoming messages to a file using the client.listen(). if you could just guide me through that it ould be awesum

sumit6192 commented 6 years ago

or if you could just tell me how can i get the incoming Thread_id (user_id) of the incoming messages of any user in my contact.. in a generic way

madsmtm commented 6 years ago

The method I've described IS a way to write incoming messages to a file. I don't know why you it doesn't work for you, though, are you sure the messages haven't been written to the file?

If you need to get the thread id when recieving messages, you could write def onMessage(self, message_object=None, thread_id=None, **kwargs) instead, and then the thread_id parameter will be the ID of the thread that sent the message

sumit6192 commented 6 years ago

Got it now its working ..Thanks a lot Bro you are a saviour