Skip to content

Commit

Permalink
2nd commit
Browse files Browse the repository at this point in the history
  • Loading branch information
blcc committed Feb 15, 2016
1 parent fbe392a commit 7c10193
Show file tree
Hide file tree
Showing 9 changed files with 772 additions and 3 deletions.
Binary file added .cliFBChat.py.swp
Binary file not shown.
7 changes: 6 additions & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
Copyright (c) 2016, blcc
New BSD License

Copyright (c) 2015, Taehoon Kim
All rights reserved.

Redistribution and use in source and binary forms, with or without
Expand All @@ -11,6 +13,9 @@ modification, are permitted provided that the following conditions are met:
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.

* The names of its contributors may not be used to endorse or promote products
derived from this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
Expand Down
28 changes: 26 additions & 2 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,41 @@ Installation

.. code-block:: console
git clone it.
$ git clone https://github.com/blcc/cliFBChat.git
Usage
=======

.. code-block:: console
python cliFBChat.py
$ python cliFBChat.py
And input FB username(email) and password.

If someone send you a message, you can just type to reply.

Or send message to someone with following command.

Commands
=======

.. code-block:: console
/whois [username]
Find user

.. code-block:: console
/talkto [number]
Set message send destination. User only.

.. code-block:: console
[enter]
Show who talk to now.


.. code-block:: console
/set [FB id with long number] [is group chat(1) or no(0)]
Same as /talkto, but use user id if already know.

Author
=======

Expand Down
130 changes: 130 additions & 0 deletions cliFBChat.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
# -*- coding: utf-8 -*-
import fbchat
from termcolor import colored
from threading import Thread
from sys import exit,exc_info,stdout
from getpass import getpass
import time
import re

USERNAME = ""
PASSWD = ""

def try_bad_encode_to_unicode(text):
encs = ['iso-8859-1','iso-8859-2']
#print("try: "+repr(text))
for i in encs:
try:
t = text.encode(i).decode('utf-8')
except UnicodeEncodeError:
continue
#print(" "+i+" is right code")
return t
return text

class myfb(fbchat.Client):
last_tid = ''
last_tname = ''
last_isgroup = False
def on_message(self,mid, author_id, author_name, imessage, metadata):
self.markAsDelivered(author_id, mid)
self.markAsRead(author_id)
open("msg.txt","a").write(str(metadata)+"\n")
#print("set last_tid = "+mid)
## sometimes message will return with wrong code...
## in each character......
message = try_bad_encode_to_unicode(imessage)

## group chat use another kind of json.
try: ## if group chat
self.last_tid = metadata['message']['thread_fbid']
self.last_tname = metadata['message']['group_thread_info']['name']
self.last_isgroup = True
except: ## if personal chat
#print(exc_info())
if not author_id == self.uid:
self.last_tid = author_id
self.last_tname = author_name
self.last_isgroup = False
try: ## add time msg time
msgtime = metadata['message']['timestamp']
msgtime = int(msgtime)/1000
timestamp = time.strftime("%H:%M",time.localtime(msgtime))
timestamp = "%s"%(colored('['+timestamp+']','blue'))
except:
print("add timestamp failed")
timestamp = u'' ## avoid error...
print(exc_info())
if not message: ## for 貼圖
try:
url = metadata['message']['attachments'][0]['url']
message = u":"
print("%s%s%s %s"%(colored(timestamp,'blue'),colored(author_name,'green'),message,colored(url,"cyan")))
except:
print(exc_info())
pass
else:
tt = "%s%s: %s"%(colored(timestamp,'blue'),colored(author_name,'green'),message)
print(tt)

def do_cmd(a,fbid,fbname,c):
if re.match("^\/whois ",a):
users = c.getUsers(a[7:])
c.last_users = users
nuser = len(users)
if users:
for i in range(nuser):
print(" %s : %s %s"%
(i,colored(users[i].name,'cyan')
,colored(re.sub("www\.","m.",users[i].url),'blue')))
print("use /talkto [number] ")
else:
print(colored("Find no user",'red'))
return
if re.match("^\/talkto ",a):
i = int(a[8:])
if c.last_users:
print("talk to "+c.last_users[i].name)
c.last_tid = c.last_users[i].uid
c.last_tname = c.last_users[i].name
c.last_isgroup = False
else:
print(colored("Find no user",'red'))
return
if re.match("^\/set ",a):
indata = a[5:].split(" ")
if len(indata) < 2: print('\/set [fbid] [isgroup(1 or 0)]')
c.last_tid = indata[0]
c.last_tname = indata[0]
c.last_isgroup = indata[1]
print(colored("set id to %s"%(str(indata)),'red'))
return

if fbid and a:
print(colored("[%s] send %s to %s "%(('ok' if c.send(fbid,a) else 'failed'),a,fbname),"blue"))
return

if not fbid:
print("no last_tid")
return

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

try:
if not USERNAME:
USERNAME = raw_input("FB username: ")
PASSWD = getpass("FB password: ")
c = myfb(USERNAME, PASSWD)
th = Thread(target=c.listen)
th.start()
while 1 :
aa = raw_input("")
a = aa.decode("utf-8")
do_cmd(a,c.last_tid,c.last_tname,c)
except (KeyboardInterrupt,EOFError):
print("exiting")
c.stop_listen()
exit()
26 changes: 26 additions & 0 deletions fbchat/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# -*- coding: UTF-8 -*-

"""
fbchat
~~~~~~
Facebook Chat (Messenger) for Python
:copyright: (c) 2015 by Taehoon Kim.
:license: BSD, see LICENSE for more details.
"""


from .client import *


__copyright__ = 'Copyright 2015 by Taehoon Kim'
__version__ = '0.3.0'
__license__ = 'BSD'
__author__ = 'Taehoon Kim; Moreels Pieter-Jan'
__email__ = '[email protected]'
__source__ = 'https://github.com/carpedm20/fbchat/'

__all__ = [
'Client',
]
Loading

0 comments on commit 7c10193

Please sign in to comment.