Monday, October 21, 2013

Receive Facebook Messages

Notice: Since the release of the Facebook API 2.0 in April 2014 the chat function is disabled, which means that this code unfortunately is no longer runnable.

In this post I want to show, how to receive messages over the Facebook chat using C#. Since "normal" messages and chat messages are sent using the same principle, with this not only chat messages can be received.
Requirement is a connection to the Facebook chat with XMPP, furthermore the previous post could be useful, in which is shown, how to read the Jids of the friends, which are needed for receiving.

To receive messages from contacts, for each a so called MessageCallback has to be created:

xmpp.MessageGrabber.Add(new Jid("-100006476847029@chat.facebook.com"), new BareJidComparer(), new MessageCB(MessageCallBack), null);

If now a message is received from the corresponding contact, the function MessageCallBack() is called.
This could look for example as follows:

private void MessageCallBack(object sender, agsXMPP.protocol.client.Message msg, object data)
{
    if (msg.Body != null)
    {
        MessageBox.Show(msg.From + ": " + msg.Body);
    }
}

The complete code:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using agsXMPP;
using agsXMPP.protocol.client;
using agsXMPP.protocol.iq.roster;
using agsXMPP.Collections;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        XmppClientConnection xmpp = new XmppClientConnection("chat.facebook.com");

        private void Form1_Load(object sender, EventArgs e)
        {
            xmpp.OnLogin += new ObjectHandler(OnLogin);
            xmpp.Open("name""password");
        }

        private void OnLogin(object sender)
        {
            xmpp.MessageGrabber.Add(new Jid("-100006476847029@chat.facebook.com"), new BareJidComparer(), new MessageCB(MessageCallBack), null);
        }

        private void MessageCallBack(object sender, agsXMPP.protocol.client.Message msg, object data)
        {
            if (msg.Body != null)
            {
                MessageBox.Show(msg.From + ": " + msg.Body);
            }
        }
    }
}

4 comments:

  1. hello,

    I have a question , is it possible to get the messages you receive in a listview? with in one column the name and in the other the message? I tried it but i always get an error and i don't know how i can fix it. this is the error: Cross-thread operation not valid: Control 'lvBerichten2' accessed from a thread other than the thread it was created on.

    thanks

    ReplyDelete
    Replies
    1. Hey, yes of course that's possible!
      The problem you are facing does not have anything to do with a listview, but with the property of .Net of now alowing access to controls by different threads than by which they were created.
      Therefore, you need to "invoke" the access. Just look in the code of the advanced chat client I posted here, there I used the same principle:

      if (listBox1.InvokeRequired)
      {
      listBox1.Invoke(new Action(() =>
      {
      listBox1.Items.Add(...) etc.
      }));
      }

      Delete
    2. it worked, thank you very much!

      Delete
  2. what is "-100006476847029@chat.facebook.com"

    ReplyDelete