Write first facebook chat bot in PHP using Jaxl library

Write first facebook chat bot in PHP using Jaxl library

Today facebook officially announced availability of it’s chat through jabber/xmpp clients. This is a big win for XMPP, with almost 400 million new probable users adding into XMPP club. In this post, I will demonstrate how to connect to facebook chat servers using Jaxl client library in PHP. It can further be used to make custom chat bots for facebook.

Creating your first facebook chat bot:
Follow the steps to successfully run a facebook chat bot:

  1. Download Jaxl or checkout latest from trunk
    svn checkout http://jaxl.googlecode.com/svn/trunk/ jaxl-read-only
  2. Edit the configuration file config.ini.php as follows:
      // Set an enviornment
      $env = "prod";
      $key = array("prod"=>array("user"=>"facebook_username",
  3. Run from command line:
    [email protected]:/jaxl$ sudo php index.php
    OSType: Linux, Registering shutdown for SIGINT and SIGTERM
    OpenSSL: Enabled for CLI
    Attempting DIGEST-MD5 Authentication...
    Starting Session...
    Requesting Feature List...
    Requesting Roster List...
    Setting Status...

Try to send a message to your running chat bot and you shall receive a default message back from the bot saying “Hi, Thanks for your message”.

See further sample codes and explaination on how to build a full fledged gaming chat bots under xmpp category.


  1. Pingback: Abhinav Singh’s Blog: Writing your first facebook chat bot in PHP using Jaxl library | Development Blog With Code Updates : Developercast.com

  2. Pingback: Abhinav Singh’s Blog: Writing your first facebook chat bot in PHP using Jaxl library | Webs Developer

  3. Phil

    Doesn’t work for me – I fill out the email and password I login to facebook with, and the bot hangs on attempting the digest-md5 auth – I can see when I sniff the traffic that I see a challenge come from facebook and that the bot sends a response, but then after that – nothing.

    Any ideas?

  4. Phil

    Oh. I’ve read the developer information for Facebook chat and DIGEST-MD5 authentication requires that you have created a facebook ‘username’. You cannot login with your email address.

    1. Phil

      Abhinav: yeah, but as far as I am concerned, an email address often is considered to be a ‘username’… I’ll confess to being annoyed by facebook’s ‘username’ function in the first place, but I suppose in this instance you don’t have a lot of choice in the matter (I wonder if you could logon using your FB u ID if you were able to otherwise work it out)

    2. Right I agree, there is and will be a lot of confusion on this for many developers. And yeah there is not a lot of error checks in Jaxl, else library should have told you about the invalid username.

      Nevertheless, glad that you brought this up, will also update the blog post.

    3. Phil

      Well, the thing is – if you try to authenticate with your email address, you don’t get an error back from facebook! It just does not respond at all, so it’s a timeout that has to be handled with a “(Bad username?)” subtext.

      Have you written anything that extends the roster list to track presence updates so that I can do actions on all users in the roster that are not offline, for example?

    4. There are a lot of methods available. You can find them listed at top of jaxl.class.php file.

      * eventMessage(), eventPresence()
      * sendMessage($jid,$message), sendStatus($status)
      * subscribe($jid)
      * roster(‘get’)
      * roster(‘add’,$jid)
      * roster(‘remove’,$jid)
      * roster(‘update’,$jid,$name,$groups)

      Also check this post of mine “How to broadcast a message to your Gtalk friends using JAXL?” which was intended for gtalk but the methodology will be same for facebook. http://bit.ly/Aecv3

  5. Phil

    Hi Abhinav,

    Yeah – I saw the functions in the class, but the roster in of itself only tracks what contacts you have when you sign in, it does not process the Presence status of the contacts.

    The reason I’m looking at it is because of the broadcast script, and I don’t want it to message people who are currently offline.

    The thing that’s confusing me is that I have updated eventPresence in the broadcast class to print out $fromJid.” is “.$status every time it receives presence updates and it’s not printing anything when it receives the burst of presence events at sign-on…

    1. Phil

      Still nothing, sadly.. I can see in the traffic sniffing that after logging on and sending the message it is coded to send that it receives a full roster and nothing is printed.

      Subsequent presence updates (such as <presence from="@chat.facebook.com” to=”@chat.facebook.com/jaxl_5e991852_47F7F32FA6A83″ type=”unavailable”/> ) also result in no output? :/

    2. I don’t think eventPresence() is currently called when someone goes offline. This was done intentionally while building gtalkbots and thats why it is like this.

      Good thing is, as I plan to make progress on Jaxl I am also changing the architecture a bit so that users can bind event handlers for various kind of events. It will be more like wordpress plugin framework. Also there will be some documentation done.

      I am currently not at my workstation, will checkin the required changes if required. Otherwise will update you with the code required to get this info.

  6. Pingback: uberVU - social comments

  7. Pingback: Writing your first facebook chat bot in PHP using Jaxl library | Dev Loom

  8. jpod

    D:htmljaxl2>php index.php
    OSType: Windows, Unable to register shutdown functions, try moving to a linux bo
    OpenSSL: Enabled for CLI
    Attempting DIGEST-MD5 Authentication…

    ^^ prosess just stop there.. can u explain more? is problem cause by my OSType?

  9. Pankaj

    Hi Abhi,

    Thanks for sharing your work.
    I have tried this example.
    I have uploaded the example on the server & the script is running fine.
    I want to know after running the script, what are the steps to follow?

    Should i send an invite to [email protected] because right now there is no reply from the user if i send an invitation.

    Please guide me.


    1. Hi Paul,

      Jaxl doesn’t handle all error cases yet and seems like it missed here.

      However from the error i can predict you have a wrong username and password set in the config.ini.php file.

      Kindly check and lemme know. Also see above example of what actual facebook username looks like. It’s not your gmail or yahoo mail id, but your facebook username.

    2. Yup ‘not-authorized’ means u have wrong username/password combination in your config.ini.php file. Try to connect to that account using any of the IM’s and see if you can connect through. If you are able to connect via IM with same user/pass, do let me know.

  10. hello sir.. IM King im planning to do this but i dont know… it didnt work for me im still 14 years old and i want to konw how… all i know is AIML but im not so good on it.


    1) can i use notepad to edit an any configuration file?

    2) (im not demanding) but do you have video for this? if so… can you give me a lin please thanks…

    3)thats all hope youll answer my questions.. thanks

  11. Lo

    i don’t get it…

    where should i place these?

    view plaincopy to clipboardprint?

    1. svn checkout http://jaxl.googlecode.com/svn/trunk/ jaxl-read-only

    view plaincopy to clipboardprint?

    1. [email protected]:/jaxl$ sudo php index.php
    2. OSType: Linux, Registering shutdown for SIGINT and SIGTERM
    3. OpenSSL: Enabled for CLI
    4. Attempting DIGEST-MD5 Authentication…
    5. Starting Session…
    6. Requesting Feature List…
    7. Requesting Roster List…
    8. Setting Status…
    9. Done

  12. Tom

    I found the Authentication hangs when you have the domain set incorrectly in the config.ini.php.

    You have it correctly in your example, but I think the jaxl defaults are misdirecting people. The default is server: talk.google.com domain: gmail.com. This makes it looks like one needs to omit the subdomain which is incorrect.

    Ok so my problem now is I’m not receive any roster response. It just hangs on Sending XML>>

    No response. Any ideas? Cheers.

  13. Nitish

    When i type php inex.php in cmd:
    it displays an error: ‘php’ is not recognised as an internal or external command,operable program or batch file

    pls pls help ne

  14. met

    it is stange but, svn checkout http://jaxl.googlecode.com/svn/trunk/ jaxl-read-only

    those files are missing from the general trunk of your files. anyway, i have tried the code above but i wasnt able to make it work. it is get stucked in the “Attempting DIGEST-MD5 Authentication…” step. I am sure that the userid is correct, and the password also. I have also tried [email protected], 100012*** and my username “mydemo.lastname” but that all didnt work. Would you please upload a demo “php file” that is working with the library here: http://jaxl.googlecode.com/svn/trunk/ jaxl-read-only
    which i can work with facebook?


  15. met

    I have seen that you have answered in stackoverflow about jabber, thanks for it. But i really couldnt find a proper way to understand if a user is online or not (or list all users online).

    What i got is i can only list online users for a channel (which not all jabber clients doesnt support, like in facebook;i am not an admin of any channel)

    secondly, what i see that someone recommend to subscribe to someone and i can learn if he/she is online or not. But thisi s again limited with facebook servers.

    1. Hi Met,

      Your connected Jaxl application can only know about presence status of users to which it has previously subscribed successfully. In short, you can only get info about your roster items. Jaxl internally cache this for you, try print_r($jaxl->roster) in your code.

      I didn’t quite understand what you meant by channel? Also what restriction do u face with fb servers?

  16. met

    Met , i have fixed the problems i had. Two problems i have is (which you have explained in stackoverflow too but incomplete)
    1) the photos are binval. how i can convert them to image ?

    2) the vcard doesnt get response from fbook. i want to export the username to names from the vcard info. however when i try to get a info i get that error.

  17. Vikas

    Hi Abhinav,

    I am trying to update the name of a buddy of the bot. Its updating also but while getting the roster list, its not shown there. What could be wrong there?


  18. Andrew

    Hi, Abhinav and All!
    Did anybody faced error like this?

    PHP Warning: fwrite(): SSL operation failed with code 1. OpenSSL Error messages:
    error:140D00CF:SSL routines:SSL_write:protocol is shutdown in /home/andrew/Dropbox/Sites/tests/jaxl-read-only/xmpp.class.php on line 219
    PHP Stack trace:
    PHP 1. {main}() /home/andrew/Dropbox/Sites/tests/jaxl-read-only/index.php:0
    PHP 2. XMPP->getXML() /home/andrew/Dropbox/Sites/tests/jaxl-read-only/index.php:18
    PHP 3. XMPP->ping() /home/andrew/Dropbox/Sites/tests/jaxl-read-only/xmpp.class.php:258
    PHP 4. XMPP->sendXML() /home/andrew/Dropbox/Sites/tests/jaxl-read-only/xmpp.class.php:869
    PHP 5. fwrite() /home/andrew/Dropbox/Sites/tests/jaxl-read-only/xmpp.class.php:219

    Any ideas are welcome 🙂 Help me please

Comments are closed.