Tuesday, October 22, 2013

Rudimentary Facebook Chat Client

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.

Today I want to combine the previous posts and present you a basic client for the Facebook chat.
When loading the form the connection is established and all friends from the contact list are saved in the dictionary Friends. Additionally, for each one a MessageCallback is created, so that any incoming message is received.
If this process is done, the form is shown.
It contains 2 text boxes and 1 button. In textBox1 the receiver can be specified, in textBox2 the message text. By clicking the button the message is send. Incoming messages are displayed in message boxes.
As receiver the complete Facebook name has to be specified, which means the name what is shown for example in the chat bar.
The program then converts this name to the corresponding Jid using the dictionary Friends, for incoming messages it converts the Jid to a name.

Since all code parts are known from previous posts, here directly the 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;

using System.Net;
using System.IO;
using Newtonsoft.Json.Linq;

namespace WindowsFormsApplication2
{
    public partial class Form1 : Form
    {

        public Form1()
        {
            InitializeComponent();
        }

        XmppClientConnection xmpp = new XmppClientConnection("chat.facebook.com");
        Dictionary<string, string> Friends = new Dictionary<string, string>();
        bool StartCollectingContacts = false;
        bool CollectingContacts = true;

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

            while (CollectingContacts)
            {
                if (StartCollectingContacts)
                    CollectingContacts = false;
                System.Threading.Thread.Sleep(1000);
            }
        }

        private void OnLogin(object sender)
        {
            Presence p = new Presence(ShowType.chat, "Online");
            p.Type = PresenceType.available;
            xmpp.Send(p);
            xmpp.OnRosterItem += new XmppClientConnection.RosterHandler(xmpp_OnRosterItem);
        }

        private void xmpp_OnRosterItem(object sender, agsXMPP.protocol.iq.roster.RosterItem item)
        {
            try
            {
                StartCollectingContacts = true;
                CollectingContacts = true;
                Friends.Add(item.GetAttribute("jid").ToString(), item.GetAttribute("name").ToString());
                xmpp.MessageGrabber.Add(new Jid(item.GetAttribute("jid").ToString()), new BareJidComparer(), new MessageCB(MessageCallBack), null);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }

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

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

        private void button1_Click(object sender, EventArgs e)
        {
            string ReceiverId = Friends.First(x => x.Value == textBox1.Text).Key;
            SendMessage(textBox2.Text, ReceiverId);
        }

    }
}

1 comment:

  1. hi! Good work!
    I really appreciate the work you done!
    i have tried to download the given link but it wont redirect to the file will you please mail me the Facebook Chat related Samples..!
    Email: shekharshete1987@gmail.com

    One more thing can we use same for ASP.NET Web Applications?
    Please write some blogs on FacebookChat in Asp.net and let me know..!

    Thanks,
    www.shekharshetemcts.wordpress.com

    ReplyDelete