3
votes

I'm trying to create a very simple script that uses python's xmpppy to send a message over facebook chat.

import xmpp
FACEBOOK_ID = "[email protected]"
PASS = "password"
SERVER = "chat.facebook.com"
jid=xmpp.protocol.JID(FACEBOOK_ID)
C=xmpp.Client(jid.getDomain(),debug=[])
if not C.connect((SERVER,5222)):
    raise IOError('Can not connect to server.')
if not C.auth(jid.getNode(),PASS):
    raise IOError('Can not auth with server.')
C.send(xmpp.protocol.Message("[email protected]","Hello world",))

This code works to send a message via gchat, however when I try with facebook I recieve this error:

An error occurred while looking up _xmpp-client._tcp.chat.facebook.com

When I remove @chat.facebook.com from the FACEBOOK_ID I get this instead:

File "gtalktest.py", line 11, in 
    if not C.connect((SERVER,5222)):
  File "/home/john/xmpppy-0.3.1/xmpp/client.py", line 195, in connect
    if not CommonClient.connect(self,server,proxy,secure,use_srv) or secureNone and not secure: return self.connected
  File "/home/john/xmpppy-0.3.1/xmpp/client.py", line 179, in connect
    if not self.Process(1): return
  File "/home/john/xmpppy-0.3.1/xmpp/dispatcher.py", line 302, in dispatch
    handler['func'](session,stanza)
  File "/home/john/xmpppy-0.3.1/xmpp/dispatcher.py", line 214, in streamErrorHandler
    raise exc((name,text))
xmpp.protocol.HostUnknown: (u'host-unknown', '')

I also notice any time I import xmpp I get the following two messages when running:

/home/john/xmpppy-0.3.1/xmpp/auth.py:24: DeprecationWarning: the sha module is deprecated; use the hashlib module instead
  import sha,base64,random,dispatcher
/home/john/xmpppy-0.3.1/xmpp/auth.py:26: DeprecationWarning: the md5 module is deprecated; use hashlib instead
  import md5

I'm fairly new to solving these kinds of problems, and advise, or links to resources that could help me move forward in solve these issues would be greatly appreciated. Thanks for reading!

2
Looks like you are doing all in a right way. Try adding always to debug parameter of Client constructor: C=xmpp.Client(jid.getDomain(),debug=['always']). Maybe these logs will say a little more. :)eigenein

2 Answers

2
votes

I also started the same project, and was trapped into same problem. I found the solution too. You have to write the UserName of facebook (Hence You must opt one Username) and that too in small Caps. This is the most important part. Most probably you too like me would not be writing it in small Caps.

1
votes
import xmpp

FACEBOOK_ID = "[email protected]"
PASS = "password"
SERVER = "chat.facebook.com"

jid=xmpp.protocol.JID(FACEBOOK_ID)

client=xmpp.Client(jid.getDomain(),debug=['always'])

if not client.connect((SERVER,5222)):
    raise IOError('Can not connect to server.')
if not client.auth(jid.getNode(),PASS):
    raise IOError('Can not auth with server.')


message = xmpp.protocol.Message(frm=client.Bind.bound[0], to="-<#_ID_OF_FRIEND>@chat.facebook.com", typ="chat", body="Hello world",)

client.SendAndWaitForResponse(message)

This worked for me. Anyway, if you want to know the server response to your query, use Client.SendAndWaitForResponse instead of Client.send ;)