Sunday, October 20, 2013

Send Facebook Message

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 send 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 send.
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 sending.

Once the connection to Facebook is established, messages can be send with one line of code, we encapsulate it in a function:

public void SendMessage(string msg, string receiverName)
{
    xmpp.Send(new agsXMPP.protocol.client.Message(new Jid(receiverName), agsXMPP.protocol.client.MessageType.chat, msg));
}

Note, that the Jid of the receiver starts with a "-". The following line for example sends a message to me:

SendMessage("Hello Oliver.", "-100006476847029@chat.facebook.com");

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)
        {
            SendMessage("Hello Oliver.""-100006476847029@chat.facebook.com");
        }

        public void SendMessage(string msg, string receiverName)
        {
            xmpp.Send(new agsXMPP.protocol.client.Message(new Jid(receiverName), agsXMPP.protocol.client.MessageType.chat, msg));
        }
    }
}

9 comments:

  1. Hey there,

    how do you get one's Jid though?

    ReplyDelete
  2. Hey! I described that in the previous post (http://csharp-tricks-en.blogspot.de/2013/10/read-out-facebook-friend-list.html). The code listed there reads out all your contacts from your friends list and also saves the Jid. I think that's what you want.
    From general people you should be able to read out the Jid using the Graph Explorer (http://csharp-tricks-en.blogspot.de/2013/09/read-data-from-facebook.html).

    ReplyDelete
  3. Hii Oliver..
    Is there any way to login using JID?
    Thanks :)

    ReplyDelete
  4. Hi Oliver,
    For long time run this facebook application is fine. But now it cannot connect to facebook and the application hang. Even your sample doesn't run as well. Can you help what happen with it? Thanks

    ReplyDelete
  5. Hello,
    like I wrote, Facebook unfortunately disabled this method, only official Messengers work now :(

    ReplyDelete
  6. This comment has been removed by the author.

    ReplyDelete
    Replies
    1. This comment has been removed by the author.

      Delete