Skip to content

Commit

Permalink
Fix names in group chat
Browse files Browse the repository at this point in the history
  • Loading branch information
blcc committed Mar 23, 2016
1 parent b9ceff2 commit 512784a
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 20 deletions.
13 changes: 6 additions & 7 deletions cliFBChat.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ def on_message(self,mid, imessage, author_id, thread_id, timestamp, stickurl):
sender_name = colored(self._roster(author_id),self.name_color)
thread_name = colored(self._roster(thread_id),self.thread_color)
msgtime = colored('['+timestamp+']',self.time_color)
if author_id != self.uid or thread_id:
if thread_id or author_id != self.uid :
self.last_tid = thread_id or author_id
self.last_tname = self._roster(self.last_tid)
self.last_isgroup = False
Expand All @@ -61,6 +61,7 @@ def on_notify(self,text,metadata):
print(colored(re.sub("\n",'',text),"cyan"))

def do_cmd(a,fbid,fbname,c):
if fbname.isdigit(): fbname = c._roster(fbid)
if re.match("^\/whois ",a):
users = c.getUsers(a[7:])
for u in users: u.isgroup = False
Expand All @@ -71,7 +72,6 @@ def do_cmd(a,fbid,fbname,c):
print(" %s : %s %s"%
(i,colored(users[i].name,'cyan')
,colored(re.sub("www\.","m.",users[i].url),'blue')))
c.roster[users[i].uid] = users[i].name
print(colored("/talkto [number]",'red'))
else:
print(colored("Find no user",'red'))
Expand Down Expand Up @@ -119,17 +119,16 @@ def do_cmd(a,fbid,fbname,c):
for i in range(nthreads):
print(" %s : %s "%
(i,colored(threads[i].name,'yellow')))
c.roster[threads[i].uid] = threads[i].name
#print("use /talkto [number] ")
print(colored("use /talkto [number] ",'red'))
else:
print(colored("Find no chat",'red'))
return
if re.match("^\/roster",a):
for i in c.roster.keys():
print("%s : %s"%
print("%s : %s %s"%
(colored(c.roster[i],'yellow')
,colored(i,'cyan') ))
,colored(i,'cyan'),type(i) ))
return
if re.match("^\/clear",a):
print("")
Expand All @@ -154,8 +153,8 @@ def do_cmd(a,fbid,fbname,c):
return

if not a:
print("say something to %s ? %s %s"%(colored(fbname,"yellow"),
colored(fbid,"blue"),colored(str(c.last_isgroup),"blue")))
print("say something to %s ?" %(colored(fbname,"yellow")))
#,colored(fbid,"blue"))),colored(str(c.last_isgroup),"blue")))
return

if __name__ == '__main__':
Expand Down
30 changes: 17 additions & 13 deletions fbchat/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -120,10 +120,13 @@ def _cleanPost(self, url, query=None, timeout=30):
self.req_counter+=1
return self._session.post(url, headers=self._header, data=query, timeout=timeout)

def _roster(self,fbid):
def _roster(self,fbid,name=''):
try:
name = self.roster[fbid]
return name
if name and fbid:
self.roster[int(fbid)] = name
elif fbid:
name = self.roster[int(fbid)]
return name
except:
return fbid
def login(self):
Expand All @@ -142,7 +145,7 @@ def login(self):
self.client_id = hex(int(random()*2147483648))[2:]
self.start_time = now()
self.uid = int(self._session.cookies['c_user'])
self.roster[str(self.uid)] = 'me'
self._roster(self.uid,'me')
self.user_channel = "p_" + str(self.uid)
self.ttstamp = ''

Expand Down Expand Up @@ -201,7 +204,7 @@ def getUsers(self, name):
for entry in j['payload']['entries']:
if entry['type'] == 'user':
users.append(User(entry))
self.roster[entry['uid']] = entry['text']
self._roster(entry['uid'],entry['text'])

return users # have bug TypeError: __repr__ returned non-string (type bytes)

Expand Down Expand Up @@ -311,6 +314,8 @@ def getThreadList(self, start, end=None):
try:
for participant in j['payload']['participants']:
participants[participant["fbid"]] = participant["name"]
## include other_user_name in self.roster
self._roster(participant["fbid"],participant["name"])
except Exception as e:
print(j)
return None
Expand All @@ -325,8 +330,7 @@ def getThreadList(self, start, end=None):
thread["other_user_name"] = ""
if not thread['name'] and thread["thread_fbid"] in participants.keys(): thread['name'] = participants[thread["thread_fbid"]]
t = Thread(**thread)
self.roster[thread["thread_fbid"]] = thread['name']
## not done: include other_user_name in self.roster
self._roster(thread["thread_fbid"],thread['name'])
self.threads.append(t)

return self.threads
Expand Down Expand Up @@ -435,17 +439,17 @@ def _parseGroupMessage(self,metadata):
sender_fbid = metadata['delta']['messageMetadata']['actorFbId']
thread_name = thread_fbid
sender_name = sender_fbid
if fbid in self.roster.keys(): self.last_tname = self.roster[fbid]
if sender_fbid in self.roster.keys(): sender_name = self.roster[fbid]
if thread_fbid in self.roster.keys(): self.last_tname = self._roster(thread_fbid)
if sender_fbid in self.roster.keys(): sender_name = self._roster(fbid)
elif 'messaging' == metadata['type']:
thread_fbid = metadata['message']['thread_fbid']
message=metadata['message']['body']
mid = m['message']['mid']
mid = metadata['message']['mid']
thread_name = metadata['message']['group_thread_info']['name']
sender_fbid = metadata['message']['sender_fbid']
sender_name = metadata['message']['sender_name']
self.roster[thread_fbid] = thread_name
self.roster[sender_fbid] = sender_name
self._roster(thread_fbid,thread_name)
self._roster(sender_fbid,sender_name)
#print("_parsePersonalMessage(): get "+message)
#print(mid)
return message,mid,thread_fbid,sender_fbid
Expand All @@ -461,7 +465,7 @@ def _parsePersonalMessage(self,metadata):
message=metadata['message']['body']
fbid = metadata['message']['sender_fbid']
name = metadata['message']['sender_name']
self.roster[fbid] = name
self._roster(fbid,name)
#print("_parsePersonalMessage(): get "+message)
#print(mid)
return message,mid,fbid
Expand Down

0 comments on commit 512784a

Please sign in to comment.