Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from fbchat import Client
- from fbchat.models import *
- from getpass import *
- import datetime
- from textwrap import wrap
- def printw(list):
- for text in list:
- print(text)
- def printd(dict):
- for key in dict:
- print(str(key) + " - " + dict[key])
- def read_thread(client, thread, num_of_messages):
- messages = client.fetchThreadMessages(thread_id=thread.uid, limit=num_of_messages)
- print("\nShowing messages from thread: " + thread.name)
- print("----------------------------------------------------------------------")
- for message in reversed(messages):
- author_dic = client.fetchUserInfo(message.author)
- author_name = author_dic[next(iter(author_dic))].name
- print("Author: " + author_name)
- print(datetime.datetime.fromtimestamp(round(int(message.timestamp) / 1000)))
- if message.attachments:
- print("Message has %d attachments!" % len(message.attachments))
- if message.text:
- printw(wrap(message.text))
- print("----------------------------------------------------------------------")
- print("\n")
- return True
- def read_unread(client):
- # TO BE DONE
- # Choose, which messages to show, according to the sender or the time
- # If message is too long, modify it to the size of the window or predefined length
- # Enable possibility to respond
- # Modify formatting the text to display message
- # Catch case, that there are more than 20 unread messages
- unread_threads = client.fetchUnread()
- for thread in reversed(unread_threads):
- thread_info = client.fetchThreadInfo(thread)
- print("Showing messages from thread: " + thread_info[next(iter(thread_info))].name)
- print("----------------------------------------------------------------------")
- messages = client.fetchThreadMessages(thread_id=thread)
- for message in reversed(messages):
- if not message.is_read:
- author_dic = client.fetchUserInfo(message.author)
- author_name = author_dic[next(iter(author_dic))].name
- print("Author: " + author_name)
- print(datetime.datetime.fromtimestamp(round(int(message.timestamp) / 1000)))
- if message.attachments:
- print("Message has %d attachments!" % len(message.attachments))
- printw(wrap(message.text))
- print("----------------------------------------------------------------------")
- print("\n")
- return True
- def read_latest(client):
- ret_value = True
- answer_valid = True
- answer = input("How many threads do you want to see? ")
- if answer.isnumeric():
- threads = client.fetchThreadList(limit=int(answer))
- threads_name_dict = {}
- threads_dict = {}
- idx = 1
- for thread in threads:
- threads_dict[thread.name] = thread
- threads_name_dict[idx] = thread.name
- idx += 1
- print("What thread do you want to open?")
- printd(threads_name_dict)
- answer = input("Your answer: ")
- if answer.isnumeric():
- thread_name = threads_name_dict[int(answer)]
- answer = input("How many messages do you wan to read? ")
- if answer.isnumeric():
- thread = threads_dict[thread_name]
- ret_value = read_thread(client, thread, int(answer))
- answer = input("0 - Continue\n1 - send an answer\n")
- if answer.isnumeric():
- if int(answer) == 1:
- text = input("Type in your message: ")
- sent = client.send(Message(text=text), thread_id=thread.uid, thread_type=thread.type)
- if sent:
- print("Message sent successfully!")
- else:
- answer_valid = False
- if not answer_valid:
- print("\nYour answer was invalid, choose again: ")
- ret_value = read_latest(client)
- return ret_value
- def read_any(client):
- return True
- def send(client):
- name = input("Whom do you want to send the message? ")
- users = client.searchForUsers(name)
- user = users[0]
- print("Is this the user?")
- print(user)
- answer = input("0 - No, 1 - Yes")
- if answer.isnumeric():
- if int(answer) == 1:
- text = input("Type in your message: ")
- sent = client.send(Message(text=text), thread_id=user.uid, thread_type=ThreadType.USER)
- if sent:
- print("Message sent successfully!")
- return True
- def logout(client):
- if client.logout():
- print("You have been successfully logged out.")
- return False
- def ask(question, dict1, dict2, client):
- answer_valid = True
- ret_value = False
- print(question)
- printd(dict1)
- answer = input("Your answer: ")
- if answer.isnumeric():
- if int(answer) <= len(dict1):
- ret_value = dict2[dict1[int(answer)]](client)
- else:
- answer_valid = False
- else:
- answer_valid = False
- if not answer_valid:
- print("\nYour answer was invalid, choose again: ")
- ret_value = ask(question, dict1, dict2, client)
- return ret_value
- Choices_num2str = {
- 1: "Read unread messages",
- 2: "Read latest messages",
- 3: "Read messages from any user or group",
- 4: "Send message",
- 5: "Logout"
- }
- Choices_str2func = {
- "Read unread messages": read_unread,
- "Read latest messages": read_latest,
- "Read messages from any user or group": read_any,
- "Send message": send,
- "Logout": logout
- }
- ThreadTypes = {
- 1: ThreadType.USER,
- 2: ThreadType.GROUP
- }
- def main():
- username = input("Email address: ")
- password = getpass()
- me = Client(username, password)
- test = True
- try:
- while test:
- test = ask("What do you want to do?", Choices_num2str, Choices_str2func, me)
- except:
- logout(me)
- return 0
- if __name__ == "__main__":
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement