def message(): speak("Checking for messages....") userID = "your email" psd = 'your password' useragent = "you user agent" cli = Client(userID, psd, user_agent=useragent, max_tries=1) if cli.isLoggedIn(): threads = cli.fetchUnread() if len(threads) == 1: speak(f"Sir, You have {len(threads)} message.") info = cli.fetchThreadInfo(threads[0])[threads[0]] speak("You have message from {}".format(info.name)) msg = cli.fetchThreadMessages(threads[0], 1) for message in msg: speak("Sir, the message is {}".format(message.text)) elif len(threads) >= 2: speak(f"Sir, You have {len(threads)} messages.") for thread in threads: initial_number = 0 info = cli.fetchUserInfo(thread[initial_number])[thread[initial_number]] initial_number += 1 speak("Sir, you have message from {}".format(info.name)) msg = cli.fetchThreadMessages(thread[initial_number], 1) msg.reverse() for message in msg: speak(f"The message is {message.text}.") else: speak("Sir, You have no messages.") else: print("Not logged in")
def download_pictures(user, password, thread_id, output_folder, msg_limit=100): if not isdir(output_folder): mkdir(output_folder) client = Client(user, password) if thread_id is None: thread = client.fetchThreadList()[0] msg_list = client.fetchThreadMessages(thread.uid, msg_limit) else: msg_list = client.fetchThreadMessages(thread_id=thread_id, limit=msg_limit) for msg in msg_list: for att in msg.attachments: att_valid = att.__class__.__name__ in "ImageAttachment, VideoAttachment".split( ", ") if not att_valid: continue url = client.fetchImageUrl(att.uid) data = http.request("GET", url=url, preload_content=False) data = data.read() try: ext = att.original_extension except AttributeError: ext = re.match(".+\.([a-z0-9]{3,4})\?.+", url) if ext is None: ext = "unkown" else: ext = ext.groups()[0] output_filename = join(output_folder, att.uid + "." + ext) print("Downloading %s" % output_filename) with open(output_filename, "wb") as f: f.write(data)
class Messenger(object): def __init__(self, email, pwd=None): self.email = email self.pwd = pwd self.client = None if pwd is not None: self.client = Client(email, pwd) def isOnline(self): return self.client.isLoggedIn() def login(self, pwd=None): if pwd is None: self.client.login(self.email, self.pwd) else: self.pwd = pwd self.client = Client(self.email, self.pwd) def searchUsers(self, search_query): return self.client.searchForUsers(search_query, limit=5) def send(self, messenger_id, message): return self.client.send(Message(text=message), thread_id=messenger_id, thread_type=ThreadType.USER) def getLast(self, th): return self.client.fetchThreadMessages(thread_id=th, limit=1)[0] def logout(self): self.client.logout() def __str__(self): return "Messenger<uid:" + str(self.client.uid) + ">"
class MessengerHandler(): def __init__(self): self.client = Client("*****@*****.**", "bl!dohuset") print("Own id: {}".format(self.client.uid)) def sendMessage(self, userID, message): self.client.send(Message(text=message), thread_id=userID, thread_type=ThreadType.USER) def logout(self): self.client.logout() def getUsers(self): self.users = self.client.fetchAllUsers() # print("users' IDs: {}".format([user.uid for user in self.users])) userIDs = [] for user in self.users: userIDs.append(user.uid) return userIDs def fetchMessage(self, sensorTemp, sensorHumid, timestamp, userIDs): for userID in userIDs: messages = self.client.fetchThreadMessages(thread_id=userID, limit=1) for message in messages: message.text = message.text.lower() if (message.text == 'info'): self.sendMessage( userID, f"Hej!\nTemperaturen i huset är {sensorTemp} och luftfuktigheten är {sensorHumid}\n Senast updaterad {timestamp}.\nMvh\nHuset" )
def name(self, uid): threads = Client.fetchThreadList() mes_thread = Client.fetchThreadMessages(uid, limit=1) for user in threads: if uid == user.uid: readable = time.ctime(int(user.last_message_timestamp[:10])) print(user.name, 'messaged at {}'.format(readable)) for mes in mes_thread: print(user.name, 'said {}'.format(mes.text))
class Messenger: def __init__(self, thread_type=ThreadType.USER): self.user = input("Enter username: "******"Enter password: "******"{random.choice(self.greetings)} {random.choice(self.names)}, I {random.choice(self.actions)} you !! <3 " print(f"Own ID: {self.client.uid}") print(f"Sending Message to {thread.uid}") self.client.send(Message(text=message), thread_id=thread.uid, thread_type=self.thread_type) print(f"Message: {message} sent from {self.client.uid} to {thread.uid}") self.client.logout() def GetMessages(self, thread, limit): return self.client.fetchThreadMessages(thread) def GetMessages(self, thread, limit, filename): file = open(filename, "w", encoding="utf-8") messages = self.client.fetchThreadMessages(thread, limit=limit) messages.reverse() for message in messages: file.write(f"Time: {message.timestamp} User:{message.author} Message: {message.text}") file.write("\n") file.close() def PromptLogin(self): self.user = input("Enter username: "******"Enter password: ") def Logout(self): self.client.logout() def CustomURLToUID(self, custom): return self.client.searchForUsers(custom, limit=1)[0]
def print_all_threads(client: Client): threads = client.fetchThreadList() for thread in threads: messages = client.fetchThreadMessages(thread.uid, limit=30) if thread.type == ThreadType.USER: thread_info = client.fetchThreadInfo(thread.uid) interlocutor = thread_info[thread.uid] _print_thread_messages(client, messages, interlocutor) else: _print_thread_messages(client, messages, None)
class FbBot(): def __init__(self): config = configparser.ConfigParser() config.read('config.ini') config = config['FACEBOOK'] self.client = Client(config['email'], config['password']) self.thread_id = config['thread_id'] self.group = self.client.fetchThreadInfo( self.thread_id)[self.thread_id] self.names = self.getListOfNames() def getListOfNames(self): # Returns a list of all names in chat. self.users = [ self.client.fetchUserInfo(uid) for uid in tqdm(self.group.participants) ] return [self._formatName(str(user)) for user in self.users] def _formatName(self, user): name, uid = tuple(user.split('USER ')[1].split(')')[0].split('(')) return (name[:-1], uid) def getLunches(self): cnt = self.group.message_count + 1 flach_id = '100002684466673' frokost_besked = 'Hvem spiser med i dag?' messages = self.client.fetchThreadMessages(self.thread_id, cnt) messages = messages[::-1] ppl = [] for message in messages: if (message.author == flach_id and (message.text is not None and frokost_besked in message.text)): date = int(message.timestamp) // 1000 # F*****g miliseconds ppl.append({ 'date': dt.fromtimestamp(date), 'msgid': message.uid, 'reactions': [ self._idToName(user) for user, react in message.reactions.items() if react == MessageReaction.YES ] }) return ppl def _idToName(self, target_id): for name, uid in self.names: if uid == target_id: return name
def main(): client = Client('*****@*****.**', '2xkzWW+%nKND', max_tries=100) #while True: unread_thread_ids = client.fetchUnread() for unread_thread in unread_thread_ids: msg = client.fetchThreadMessages(unread_thread) msg.reverse() for m in msg: if not m.is_read: print(unread_thread, m) database_holding('fb', unread_thread, m.text, 'Client') # Работа с БД send_to_operator(unread_thread, m.text) client.markAsRead(unread_thread) client.logout()
def getVideoURLs(client :Client): #Gets a list of users with the given name and uses the first user in the list users = client.searchForUsers('Testo Accounto') user = users[0] #Grabs the latest 10 messages in the thread with te given user and reverse the thread messages = client.fetchThreadMessages(thread_id=user.uid, limit=10) messages.reverse() #Loops through all the messages and their attachments and tries to fetch the video's URL #Also prints out the messages in the thread for message in messages: for attach in message.attachments: print(client.fetchVideoUrl(attach.uid)) print(message.text)
class fb_client: def __init__(self, thread, login, password): self.client = Client(login, password) self.thread = thread # return the last message of a FB thread def get_last_message(self): return self.client.fetchThreadMessages(thread_id = self.thread, limit = 1)[0].text # Send a message to a FB thread def send_message(self, message): self.client.send(Message(text = message), thread_id = self.thread, thread_type = ThreadType.GROUP) def __del__(self): self.client.logout()
def _get_messages(client: Client, sent: bool): threads = client.fetchThreadList() sent_messages = [] received_messages = [] for thread in threads: messages = client.fetchThreadMessages(thread.uid, limit=30) name = client.fetchUserInfo(client.uid)[client.uid].name user = client.searchForUsers(name)[0] messages.reverse() for message in messages: if user.uid == message.author: sent_messages.append(message) elif user.uid != message.author: received_messages.append(message) if sent: return sent_messages else: return received_messages
class FaceBookMessageBot: def __init__(self, thread_id, thread_type=ThreadType.GROUP): self.thread_id = thread_id self.thread_type = thread_type def login(self, email, password): self.email = email self.passwd = password self.client = Client(email, password) def send_message(self, message, repeat_on_fail=7): repeat = repeat_on_fail try: self.client.send(Message(text=message), thread_id=self.thread_id, thread_type=self.thread_type) except Exception as ex: print(str(ex)) if repeat > 0: sleep(10) self.login(self.email, self.passwd) self.send_message(message, repeat - 1) else: raise ex def send_message_my(self, message): thread_type = ThreadType.USER self.client.sendMessage(message, thread_id=self.client.client_id, thread_type=thread_type) def get_messages(self, limit=30, before=None): return self.client.fetchThreadMessages(self.thread_id, limit, before) def logout(self): self.client.logout() @staticmethod def get_aut_token(app_id, app_secred): # not log user token return utils.get_application_access_token(app_id, app_secred) def get_user_id(self): return self.client.client_id
class Messenger(object): def __init__(self): username, login = get_login('login') self.client = Client(username, login) self.user_map = {} self._initialize_contacts() self._initialize_messages() def _initialize_contacts(self): self.user_map[self.client.uid] = self.client.fetchUserInfo(self.client.uid)[self.client.uid].name for user in self.client.fetchAllUsers(): self.user_map[user.uid] = user.name def _initialize_messages(self, limit=1000): try: print("Input the user you want to message:") to_search = input() if to_search == "exit": print("Exiting...") return users = self.client.searchForUsers(to_search) + self.client.searchForGroups(to_search) users = users for i in range(0, len(users)): print(i, ":", users[i].name) user = users[int(input("Please specify which chat you'd like to participate in: "))] self.messages = self.client.fetchThreadMessages(thread_id=user.uid, limit=limit)[::-1] thread = self.client.fetchThreadInfo(user.uid) self.chat = Chat(user.name, user.uid, self.messages, thread, self.client.uid, self.user_map[self.client.uid], self.user_map, find_answers=True) except IndexError : traceback.print_exc() except ValueError : traceback.print_exc() def run_loop(self, limit=150): print("Wrong literal, try again.") while True: self._initialize_messages(limit=limit) def get_messages(self): return self.chat.get_messages()
def main (): if len(sys.argv) < 4: print('Usage: python messenger_scraper.py facebook_username facebook_password conversation_id') return False facebook_username = sys.argv[1] facebook_password = sys.argv[2] conversation_id = sys.argv[3] client = Client(facebook_username, facebook_password) print('Attempting {}...'.format(conversation_id)) # what the f**k fbchat, where is function to retrieve all images? # ehh I have to do everything by myself lastMessageTimestamp = None try: os.mkdir(conversation_id) except OSError: print('Dir already exists!') try: while True: messages = client.fetchThreadMessages(conversation_id, 100, lastMessageTimestamp) lastMessageTimestamp = messages[len(messages)-1].timestamp endOfMessages = False for key,message in enumerate(messages): if message.attachments: for attachment in message.attachments: try: url = client.fetchImageUrl(attachment.uid) except: continue downloadImage(message.author, url, conversation_id, client) if key == len(messages)-1 and key < 99: endOfMessages = True if endOfMessages == True: break except KeyboardInterrupt: print('Downloading interrupted!')
def onMessage(self, author_id, message_object, thread_id, thread_type, **kwargs): self.markAsDelivered(thread_id, message_object.uid) self.markAsRead(thread_id) mes_thread2 = Client.fetchThreadMessages(author_id, limit=20) mes_thread2.reverse() mes_list = [] pre_mes = 'Good {}..i\'m Claudia a bot created by seyi to control his social media accounts..it would be nice chatting with you'.format( part_of_day(h)) if author_id != self.uid: self.name(author_id) for mes in mes_thread2: mes_list.append(mes.text) if pre_mes in mes_list: pass # self.send(message_object, thread_id=thread_id, thread_type=thread_type) else: time.sleep(10) self.send(Message(text=pre_mes), thread_id=author_id, thread_type=thread_type) else: print('your message has been sent')
class Hub: def __init__(self): self.Client = Client(USER, PASS) def logout(self): self.Client.logout() def list(self, size=20): users = self.Client.fetchThreadList(limit=size) convos = {} for u in users[:100]: if u.name != None: convos[u.name] = u.uid print("fetched conversations") return convos def fetchUsers(self): self.users = self.Client.fetchAllUsers() return self.users def fetchUserInfo(self, ui): return self.Client.fetchUserInfo(ui) def get_conversations(self): return self.conversations def fetch_messages(self, conv_id): messages = self.Client.fetchThreadMessages(thread_id=conv_id, limit=10) return messages def search_conversations(self): convos = self.list() print("Convos is " + str(convos)) selection = iterfzf(iter_conversations(convos.keys()), multi=False) # Fetch thread messages print(self.fetch_messages(convos[selection]))
def fb_messages(): logger.debug('fb_messages:Init function') msg = {} msg[0] = '' msg[1] = '' msg[2] = '' try: logger.debug('fb_messages:Init channel') fb_thread = int(app_conf['fb_thread']) fb_client = Client(app_conf['fb_login'], app_conf['fb_password']) logger.debug('fb_messages:Fetch last 3 messages') last_messages = fb_client.fetchThreadMessages(thread_id=fb_thread, limit=3) msg[0] = last_messages[2].text msg[1] = last_messages[1].text msg[2] = last_messages[0].text logger.info( 'fb:messages:Messages fetched from thread {}'.format(fb_thread)) except ValueError: logger.error('fb_messages:Fail to reach fb messages') logger.debug('fb_messages:Exit function') return json.dumps(msg, ensure_ascii=False)
def print_single_thread(client: Client, thread_id): messages = client.fetchThreadMessages(thread_id, limit=30) thread_info = client.fetchThreadInfo(thread_id) interlocutor = thread_info[thread_id] _print_thread_messages(client, messages, interlocutor)
class fbMessenger: def __init__(self, username, password): self.client = Client(username, password) # facebook login def print(self, thread, index): print("{}.".format(index)) print("thread name: {}".format(thread.name)) print("type: {}".format("Group" if thread.is_group else "Single user")) print("msg count: {}".format(thread.msg_count)) print("day count: {}".format(thread.day_count)) print("file count: {}".format(thread.file_count)) print("image count: {}".format(thread.image_count)) print("first talk: {}".format(thread.first_talk)) print("last talk: {}".format(thread.last_talk)) print("last msg :'{}'\n".format(thread.last_msg)) def get_messages(self): from_time = datetime.now() - timedelta( days=30) # msg records in previous 30 days threads = self.client.fetchThreadList( limit=20) # find 30 candidate contacts contact_list = [] index = 1 print("second try") for thread in threads: msgs = self.client.fetchThreadMessages(thread_id=str(thread.uid), limit=500) if thread.type == ThreadType.USER: thread_info = ThreadInfo() thread_info.name = thread.name thread_info.is_group = False if thread.type == ThreadType.USER else True thread_info.last_talk = datetime.fromtimestamp( int(msgs[0].timestamp) / 1000) thread_info.last_msg = msgs[0].text tem_day = None for msg in msgs: msg_time = datetime.fromtimestamp( int(msg.timestamp) / 1000) if from_time < msg_time: thread_info.msg_count += 1 if msg.text is not None else 0 # msg count thread_info.first_talk = datetime.fromtimestamp( int(msg.timestamp) / 1000) # first talk for atch in msg.attachments: # file count & image count if isinstance(atch, FileAttachment): thread_info.file_count += 1 elif isinstance(atch, ImageAttachment): thread_info.image_count += 1 if msg_time.day != tem_day: # dat count thread_info.day_count += 1 tem_day = msg_time.day if thread_info.msg_count == 0: continue else: contact_list.append(thread_info) self.print(thread=thread_info, index=index) if index == 15: #################### number of candidate contacts ##################### break else: index += 1 return contact_list
print("Fetching %d messages" % messages_number) #-----fetch and set number of attachments and messages----- last_timestamp = None messages_count = 0 active = True messages_list = [] try: while messages_count <= messages_number and active == True: #nonlocal active, attach_count, last_timestamp, messages_count messages = client.fetchThreadMessages( thread_id=uid_c, limit=min(STEPS, messages_number + 1), before=last_timestamp ) #get specific number of messages starting at last timestamp if last_timestamp != None: #remove first item because it's message with passed timestamp and we don't want it messages.pop(0) messages_count += len(messages) print("Number of fetched messages %d" % messages_count) if len(messages) == 0: print("End of messages") active = False break last_timestamp = messages[-1].timestamp #save new timestamp
print("user's name: {}".format(user.name)) print("users' names: {}".format(users[k].name for k in users)) # `searchForUsers` searches for the user and gives us a list of the results, # and then we just take the first one, aka. the most likely one: user = client.searchForUsers('<name of user>')[0] print('user ID: {}'.format(user.uid)) print("user's name: {}".format(user.name)) print("user's photo: {}".format(user.photo)) print("Is user client's friend: {}".format(user.is_friend)) # Fetches a list of the 20 top threads you're currently chatting with threads = client.fetchThreadList() # Fetches the next 10 threads threads += client.fetchThreadList(offset=20, limit=10) print("Threads: {}".format(threads)) # Gets the last 10 messages sent to the thread messages = client.fetchThreadMessages(thread_id='<thread id>', limit=10) # Since the message come in reversed order, reverse them messages.reverse() # Prints the content of all the messages for message in messages: print(message.text) # Here should be an example of `getUnread`
from fbchat import Client from fbchat.models import * import time from fbchat import Credentials client = Client(Credentials.username, Credentials.password) client.wave(thread_id='1275720524', thread_type=ThreadType.USER) client.send(client, Message("This is a test message for my COMP 490 project."), '1275720524') client.send(client, Message("This message will be removed."), '1275720524') end_time = time.time() + 10 # 10 seconds until message is removed while time.time() <= end_time: x = 1 # wait message = client.fetchThreadMessages('1275720524', limit=1) client.unsend(message[0].uid) client.logout()
from fbchat import Client from Auth.credentials import user, password, person client = Client(user, password) if not client.isLoggedIn(): client.login(user, password) print("Login successful...") user = client.searchForUsers(person)[0] userid = user.uid thread_id = userid messages = [] me = client.fetchThreadMessages(thread_id=thread_id, limit=500) me.reverse() print(len(me)) for m in me: u = client.fetchUserInfo(m.author)[m.author] print(u.name, m.text) client.logout()
print('Found {count} messages in thread with {friend_name}'.format( count=thread_message_count, friend_name=thread_message_name)) message_before_date = config.get('Messages', 'before_date') message_search_limit = int(config.get('Messages', 'search_limit')) message_search_before = convert_date_to_epoch(message_before_date) if message_search_limit > thread_message_count: message_search_limit = thread_message_count print( '\tWarning: Message search limit was greater than the total number of messages in thread.\n' ) if message_search_before is not None: messages = fb_client.fetchThreadMessages( my_thread.uid, limit=message_search_limit, before=message_search_before) print( 'Searching for images in the {message_limit} messages sent before {before_date}...' .format(message_limit=message_search_limit, before_date=message_before_date)) else: messages = fb_client.fetchThreadMessages( my_thread.uid, limit=message_search_limit) print( 'Searching for images in the last {message_limit} messages...'. format(message_limit=message_search_limit)) sender_id = None if config.getboolean('Media', 'sender_only'): sender_id = my_thread.uid
def fetchFBInformation(app, userid, fbemailid, fbpassword): #### # 0. Set base variables ############ messagefile_complete_name = "all_messages_" messagefile_complete_filetype = ".txt" ############ # 1. Login into the FB account ############################### try: # define a FB connector client = Client(fbemailid, fbpassword) # Update FBemailid for the <userid> updateFBEmailid(app, int(userid), fbemailid) # Update the process status to '2' for successful login updateProcessStatus(app, int(userid), '2') except Exception as wrongLoginCredentials: print("Wrong User name or password") return (-1) ######### # 2. Fetch chat-interlocutors of <userid> ############################################## interlocutors = client.fetchAllUsers() print("Number of Users: " + str(len(users))) # Update the process status to '3' for having fetched FB chat data updateProcessStatus(app, int(userid), '3') ######### # 3. Fetch chatHistories for all interlocutors of <userid> that happen after <maxTimeStamp>; if <maxTimeStamp> is "1" : fetch all ################################################################################################################################## # initialize a list for all conversation threads of <userid> conversations = [] # initialize a list of interlocutor uids with whom <userid> has more than 1 message exchanged interlocutor_uids = [] # fetch the timestamp when the last FB fetch happened and cast it to <int>-type maxTimestampDB = fetchTimestamp(app, int(userid)) maxTimestampDB = int(maxTimestampDB) # Fetch messages <userid> chats with and append it to <conversations> for interlocutor in interlocutors: try: # try to fetch the last 1000 messages from the conversation between <userid> and <interlocutor> threadMessages = client.fetchThreadMessages( thread_id=interlocutor.uid, limit=10000) messageCount = len(threadMessages) # if more than one message has been exchanged ... if (len(threadMessages) > 1): # remember thread messages in the <conversations>-list conversations.append( client.fetchThreadMessages(thread_id=interlocutor.uid)) # remember the interlocutor_uid interlocutor_uids.append(interlocutor.uid) except fetchFBchatException: #print("## Error ## ","UID: ",user.uid, " Name: ", user.first_name, " ", user.last_name, "# of msgs: ") pass # Error conversations that contain no messages (i.e. conversations started automatically by messenger after becoming friends on FB print("Fetched ", "conversations: " + str(len(conversations)), " userlist length: " + str(len(userlist))) ########## # 4. Extract all non-empty conversations which have not been fetched before ########################################################################## write_log("Threads Fetched. Start language classfication.") # set paths ## path that will contain all messages as a single file in JSON format #messagefolder_json = "./Messages_json/" # ## path that will contain all messages as a single file in plain text format #messagefolder_plain = "./Messages_plain/" # path that will contain all messages the user has ever sent in one single file per language used messagefolder_complete = "./ServerFeatures/Userdata/Messages_complete/" + str( userid) + "/" # file-name to store a message temporarily for language classification message_tempfile_path = "./ServerFeatures/Userdata/Messages_complete/" + str( uderid) + "/tempfile.txt" # initialize a list for the languages used in <userid>'s conversations languages = [] # initialize (WRITE WHAT) language_count = [] # initialize a counting variable for the considered conversations interlocutor_number = 0 # return value of the function (all English messages in one string) # VERIFY - probably the timestamps for the conversations max_message_timestamps = [] # create message folders if not existing yet #if not os.path.exists(messagefolder_json): # os.makedirs(messagefolder_json) #if not os.path.exists(messagefolder_plain): # os.makedirs(messagefolder_plain) if not os.path.exists(messagefolder_complete): os.makedirs(messagefolder_complete) # clear all message files if existent # TODO: this will have to be removed # otherwise, running the script multiple times will cause messages to appear multiple times in the files # TODO: change later for updating #for root, dirs, files in os.walk(messagefolder_json): # for file_ in files: # if file_.endswith(".txt"): # os.remove(os.path.join(root, file_)) #for root, dirs, files in os.walk(messagefolder_plain): # for file_ in files: # if file_.endswith(".txt"): # os.remove(os.path.join(root, file_)) # collect all *.txt files in <messagefolder_complete> # (only relevant if the user has stored FB messages in the past for root, dirs, files in os.walk(messagefolder_complete): for file_ in files: if file_.endswith(".txt"): os.remove(os.path.join(root, file_)) # for all conversations (with more than one message) for conversation in conversations: # set sub directory paths for each chat partner in the #userdir_json = messagefolder_json + "/" + str(userlist[user_number]) #userdir_plain = messagefolder_plain + "/" + str(userlist[user_number]) # ## make directories if not yet existent #if not os.path.exists(userdir_json): # os.makedirs(userdir_json) #if not os.path.exists(userdir_plain): # os.makedirs(userdir_plain)## # remember all thread messages, their predicted language, and the number of considered messages of the conversation conversation_messages = [] conversation_message_languages = [] conversation_message_counter = 0 text_returned_en = "" conversation_empty = True # for all messages in the conversation for message in conversation: # get message text, author, timestamp message_text = message.text message_author = message.author message_timestamp = message.timestamp # encode <message_text> to utf-8 if type(message_text) == 'unicode': message_text = message_text.encode('utf-8') elif type(message_text) == str: message_text.decode('utf-8').encode('utf-8') # remember the timestamp of the message max_message_timestamps.append(message_timestamp) # (e.g. exclude automatically generated call messages) # if <message_text> is not empty # AND # the message has not been fetched in the past (maxTimestampDB indicates the most recent point in time <userid> fetched FB messages # AND # if the message was sent by <userid> and not by <interlocutor> message_not_empty = (message_text != None) message_sent_by_userid = (message_author == client.uid) message_not_fetched_before = (int(message_timestamp) > int(maxTimestampDB)) if (message_not_empty & message_sent_by_userid & message_not_fetched_before): conversation_empty = False # set message file paths for the json and text message files #messagedir_plain = userdir_plain + "/" + message_timestamp + ".txt" #messagedir_json = userdir_json + "/" + message_timestamp + ".txt" # remove newlines (\n) and carriage returns (\r) (for language classification - otherwise this would produce multiple outputs) message_text = message_text.replace('\n', ' ').replace('\r', '') ## write message json file #message_dictionary = {'timestamp': message_timestamp, 'author_id': message_author, 'text': message_text} #with open(messagedir_json, "w") as outfile: # json.dump(message_dictionary, outfile) # outfile.close() # write message text file #with open(messagedir_plain, "w") as outfile: # outfile.write(message_text) # outfile.close() # put <message_text> into a temporary file for languge prediction with open(message_tempfile_path, "w") as tempfile: tempfile.write(message_text) # try to predict the message's language with the original fasttext algorithm try: message_language = check_output([ './ServerFeatures/Wordembedding/fastText-original/fasttext', 'predict', './ServerFeatures/Preprocessing/Languageprediction/pretrainedmodel.bin', message_tempfile_path ]) # remove newlines (\n) from <message_language> message_language = str(message_language).replace('\n', '') except CalledProcessError as e: raise RuntimeError( "command '{}' returned with error (code {}): {}". format(e.cmd, e.returncode, e.output)) # keep track of every language prediction for the conversation and count the messages in the conversation # e.g. # conversation_messages = [msg1, msg2, msg3] # conversation_message_languages = ['__label__en', '__label__de', '__label__en'] conversation_messages.append(message_text) conversation_message_languages.append(message_language) conversation_message_counter += 1 #### # 5. Filter out empty conversations ########################################3 if conversation_empty: continue # with the next iteration of the loop """ # this code only focuses on English conversations # extract the languages used in the conversation and their respective counts conversation_message_languages_and_count = [ ( conversation_messages_languages.count(language), language) for language in set(conversation_message_languages) ] # pick the majority language, max finds the maximum in the 0-th elements of the duples majority_language = max(conversation_message_languages_and_count)[1] """ #### # 6. Build separate chat histories for <userid> for each conversation language ############################################################################ conversation_languages = [] conversation_language_count = [] # for all recorded conversation languages for message_language in conversation_message_languages: message_language_known = False conversation_language_index = 0 # for for conversation_language in conversation_languages: if conversation_language == message_language: conversation_language_count[ conversation_language_index] = conversation_language_count[ conversation_language_index] + 1 message_language_known = True break conversation_language_index = conversation_language_index + 1 if (message_language_known == False): conversation_languages.append(message_language) conversation_language_count.append(1) # retrieve final conversation language for the whole conversation max_language_count = 0 conversation_language = '' conversation_language_index = 0 for x in conversation_languages: if (conversation_language_count[conversation_language_index] > max_language_count): max_language_count = conversation_language_count[ conversation_language_index] conversation_language = conversation_languages[ conversation_language_index] conversation_language_index = conversation_language_index + 1 #print("Final conversation language: ", conversation_language) # add conversation language use to the global language use of the user language_index = 0 language_known = False for language in languages: if language == conversation_language: language_count[language_index] = language_count[ language_index] + len(conversation_messages) language_known = True break if (language_known == False): languages.append(conversation_language) language_count.append(len(conversation_messages)) # append all conversation messages to the respective complete history with regard to the conversation language # format e.g.: "./ServerFeatures/Userdata/Messages_complete/testid/all_messages___label__en.txt" complete_history_path = messagefolder_complete + messagefile_complete_name + str( userid) + conversation_language + messagefile_complete_filetype # the conversation messages are encoded in 'utf-8', see above with open(complete_history_path, "a+") as complete_file: for conversation_message in conversation_messages: append_message = (conversation_message.decode('utf-8') + " ").encode('utf-8') complete_file.write(append_message) # append message to the return string if it is english // TODO: potentially change later if (conversation_language == '__label__en'): text_returned_en = text_returned_en + conversation_message + " " interlocutor_number += 1 # print("Conversation languages: ",conversation_languages) # print("Conversation language count: ",conversation_language_count) print("Overall languages used: ", languages) print("Overall languages used count:", language_count) # delete tempfile if (os.path.isfile(message_tempfile_path)): os.remove(message_tempfile_path) # Update the process status to '4' updateProcessStatus(app, int(userid), '4') ###### # 7. Extract language usage statistics of <userid> ################################## # for testing only # get language statistics msg_count_en = 0 msg_count_total = 0 index = 0 for language in languages: if (language == '__label__en'): msg_count_en = msg_count_en + language_count[index] msg_count_total = msg_count_total + language_count[index] index = index + 1 write_log("Finished language classification") ###### # 8. Update <maxTimeStamp> in the userinfo TABLE if necessary #################################################### complete_EN_history_path = messagefolder_complete + messagefile_complete_name + str( userid) + '__label__en' + messagefile_complete_filetype # if an English chat history path exists EN_chatHistory_exists = os.path.isfile(complete_EN_history_path) if EN_chatHistory_exists: new_non_empty_conversations_fetched = len(max_message_timestamps) > 0 if new_non_empty_conversations_fetched: print("Update maxTimeStamp in the userinfo TABLE") word_count_en = len(text_returned_en.split()) write_log( "Fetched {} Facebook messages. Number of english classified messages: {}. Number of english words: {}" .format(msg_count_total, msg_count_en, word_count_en)) # Update maxTimeStamp in the userinfo TABLE status = updateMessageTimestamp(app, int(userid), max(max_message_timestamps)) # return the chat history path for English conversations return complete_EN_history_path # return "-1" if no changes were made return "-1"
client.isLoggedIn() print(format(client.uid)) user = client.searchForUsers(input("withwhom to chat:"))[0] print("user's name: {}".format(user.name)) #messages = client.fetchThreadMessages(thread_id= 'user.uid', limit=1) #for message in messages: temp = 'it' bot='it' try: cb.browser.get(cb.url) except: cb.browser.close() sys.exit() while True: cb.get_form() messages = client.fetchThreadMessages(thread_id= user.uid, limit=1) for message in messages: p=message.text if temp==p or bot==p: time.sleep(3) print("no reply") else: temp=message.text cb.send_input(temp) bot = cb.get_response() print("she says: ",p) print("bot says: ",bot) client.send(Message(text=bot), thread_id=user.uid,thread_type=ThreadType.USER) time.sleep(5) '''
#print (data_list[1]["GroupChatID"]) while True: #try: for item in data_list: sleep(5) thread_id = item["GroupChatID"] thread_type = ThreadType.GROUP print("Listening for messages from: " + item["Name"] + " on group chat: " + item["Subject"]) message = client.fetchThreadMessages(thread_id, limit=1) message_1 = message[0] message = message[0] accepted = True if message_1.author != item["UID"]: accepted = False print(item["Name"]) print("Does not match user ID") break if messages[item["Subject"]] != message.text: messages[item["Subject"]] = message.text #message = message[0] print(message.text + '\n' + ' -' + item["Name"])
class UnreadMessageFetcher(): def __init__(self, username, password): self.__newUnreadMessageNb = 0 # Number of unread messages before the next graphic user interface actualusation. self.__unreadMessageDictionnaryList = [] self.__threadList = [] self.__unRodeThread = [] self.__client = Client(username, password) self.__setThreadList() self.__setUnreadMessagesList() ##Public method to empty the list of notifications. # # arg: none # # return: none def EmptyLists(self): """ To avoid to refill the list with already fetched notification, each conversation with at least one unread message is marked as read. To avoid to empty an already empty list, the size of the notification list must be superior to zero to empty the list. """ if (len(self.__unreadMessageDictionnaryList) > 0): self.__unreadMessageDictionnaryList = [] for i in range(len(self.__unRodeThread)): self.__client.markAsRead(self.__unRodeThread[i].uid) self.__threadList = [] ##Private method to affect the list of the ten most recent messenger conversations of the user into the __threadlist attribute. # # arg: none # # return: none def __setThreadList(self): self.__threadList = self.__client.fetchThreadList(limit=10) ##Private method for setting the __unreadMessageDictionnaryList attribute by storing the unread messages dictionnaries' into the list. # # arg: none # # return: none def __setUnreadMessagesList(self): i = 0 isDictEmpty = False while ((i < 10) and (isDictEmpty == False)): dict = self.__getUnReadMessageDict(i) if ((dict != {}) and (not (dict in self.__unreadMessageDictionnaryList))): self.__unreadMessageDictionnaryList.append(dict) self.__newUnreadMessageNb += 1 # ToDo check if the thread is already into the list before adding the thread into the list. For avoiding to mark as read a thread twice. self.__unRodeThread.append(self.__threadList[i]) notifSoundThread = Thread( target=PlayNotificationSound ) # For running the sound notification into a second thread who will run in a parallel of the main one. notifSoundThread.start() # Starting the thread. else: isDictEmpty = True i += 1 ##Private method to get the unread message dictionnary of the selected conversation. # # arg: threadIndex: the index of the conversation. # # return: unReadMessageDict: the unread message dictionnary of the selected conversation. def __getUnReadMessageDict(self, threadIndex): unreadMessageDict = {} fetchedMessage = self.__client.fetchThreadMessages( thread_id=self.__threadList[threadIndex].uid, limit=1) if (fetchedMessage[0].is_read == False): if (self.__threadList[threadIndex].type.name == "USER"): name = self.__threadList[threadIndex].name else: authorname = self.__getaUserName( fetchedMessage[0].author ) # Get the name of the author of the last message in a group conversation with his user ID name = self.__threadList[ threadIndex].name + " (" + authorname + ")" # Concatenate the author name in parenthesis with the name of the conversation message = fetchedMessage[0].text photo = self.__threadList[threadIndex].photo unreadMessageDict = { "name": name, "photo": photo, "message": message } return (unreadMessageDict) ##Public method to reset the unread message counter when emptying the notification list. # # arg: none # # return: none def resetNewUnreadMessageNb(self): self.__newUnreadMessageNb = 0 ##Public method to update the __threadList and __unreadMessageLis attributes # # arg: none # # return: none def setLists(self): self.__setThreadList() self.__setUnreadMessagesList() ## Private method to get the name of the user who sent the message in a group conversation. # Messenger uses the real name of the other user in the name attribut of the dictionnary for # user to user conversation and group's name for group conversation. To specify who sent the # message, this method fetches the profile's informations of the user who sends the message # by using is facebook id (each message dictionnary returned by the client object by the # fbchat module contains the id of the user who sent the message) and extract his name to # return it to the __getUnReadMessageDict method. # # arg: userId: The Facebook user's id of the user who sent the message. # # return: name: The name of the user who sent the message. def __getaUserName(self, userId): userInfo = self.__client.fetchUserInfo(userId) name = userInfo.get(userId).name return (name) ##Public method to optain the size of the list # Usefull? # arg: none # # return: d: The size of the __unreadMessageDictionnaryList attribute. def getTotalUnreadMessageNumber(self): d = len(self.__unreadMessageDictionnaryList) return (d) ##Public method to get the number of new unread messages fetched during an iteration of # the __setUnReadMessageList method. #Usefull? Must see that at the last refactorings. # # arg: none # # return: __newUnReadMessageNb: The number of new unread messages fetched during an iteration of the # __setUnReadMessageList method. def getNewUnreadMessageNumber(self): return (self.__newUnreadMessageNb) ##Public method to return the message author's name into the interface. # # arg: dictIndex: the index of the selected message dict into the __unreadMessageDictionnaryList attribute. # # return: self.__unreadMessageDictionnaryList[dictIndex].get('name'): The message author's name. def getAuthorName(self, dictIndex): return (self.__unreadMessageDictionnaryList[dictIndex].get('name')) ##Public method to return the url of the message author's profile picture for loading it into the interface. # # arg: dictIndex: the index of the selected message dict into the __unreadMessageDictionnaryList attribute. # # return: self.__unreadMessageDictionnaryList[dictIndex].get('photo'): The url of the message author's profile picture. def getConversationPic(self, dictIndex): return (self.__unreadMessageDictionnaryList[dictIndex].get('photo')) ##Public method to return the message into the interface. # # arg: dictIndex: the index of the selected message dict into the __unreadMessageDictionnaryList attribute. # # return: self.__unreadMessageDictionnaryList[dictIndex].get('message'): The unread message. def getMessage(self, dictIndex): return (self.__unreadMessageDictionnaryList[dictIndex].get('message'))
import pygsheets import fb_link_settings as settings client = Client(settings.FACEBOOK_USERNAME, settings.FACEBOOK_PASSWORD) localpath = settings.LOCAL_PATH namedict={} names = settings.NAMES for name in names: namedict[client.searchForUsers(name)[0].uid] = name ## insert thread_id here. Fetches last 1000 messages — adjust as necessary messages=client.fetchThreadMessages(thread_id=settings.THREAD_ID,limit=1000) lines=[] cols=['Name','Title','Link','Source'] d=dict.fromkeys(cols) df1=pd.DataFrame(columns=cols) for message in messages: try: m=message.attachments[0] info=[namedict[message.author],m.title,m.original_url,m.source] for i in range(len(cols)): d[cols[i]]=info[i] df1=df1.append(d,ignore_index=True)