Skip to content

Repository files navigation

fbchat

Facebook Chat (Messenger) for Python. This project was inspired by facebook-chat-api.

No XMPP or API key is needed. Just use your ID and PASSWORD.

Installation

Simple:

$ pip install fbchat

Example

importfbchatclient=fbchat.Client("YOUR_ID", "YOUR_PASSWORD")

Sending a Message

friends=client.getUsers("FRIEND'S NAME") # return a list of namesfriend=friends[0]
sent=client.send(friend.uid, "Your Message")
ifsent:
print("Message sent successfully!")
# IMAGESclient.sendLocalImage(friend.uid,message='<message text>',image='<path/to/image/file>') # send local imageimgurl="http://i.imgur.com/LDQ2ITV.jpg"client.sendRemoteImage(friend.uid,message='<message text>', image=imgurl) # send image from image url

Getting user info from user id

friend1=client.getUsers('<friend name 1>')[0]
friend2=client.getUsers('<friend name 2>')[0]
friend1_info=client.getUserInfo(friend1.uid) # returns dict with detailsboth_info=client.getUserInfo(friend1.uid,friend2.uid) # query both together, returns list of dictsfriend1_name=friend1_info['name']

Getting last messages sent

last_messages=client.getThreadInfo(friend.uid, last_n=20)
last_messages.reverse() # messages come in reversed orderformessageinlast_messages:
print(message.body)

Example Echobot

importfbchat#subclass fbchat.Client and override required methodsclassEchoBot(fbchat.Client):
def__init__(self,email, password, debug=True, user_agent=None):
fbchat.Client.__init__(self,email, password, debug, user_agent)
defon_message(self, mid, author_id, author_name, message, metadata):
self.markAsDelivered(author_id, mid) #mark deliveredself.markAsRead(author_id) #mark readprint("%s said: %s"%(author_id, message))
#if you are not the author, echoifstr(author_id) !=str(self.uid):
self.send(author_id,message)
bot=EchoBot("<email>", "<password>")
bot.listen()

Saving session

session_cookies=client.setSession()
# save session_cookies

Loading session

client=fbchat.Client(None, None, session_cookies=session_cookies)
# ORclient.setSession(session_cookies)

Authors

Taehoon Kim / @carpedm20

About

Facebook Chat (Messenger) for Python

Resources

Stars

0 stars

Watchers

2 watching

Forks

Releases

Packages

Contributors

Languages