Wednesday, October 23, 2013

Determine Whether Chat Partner is Typing in Facebook Chat

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 just want to show briefly, how to determine with C#, when a chat partner in the Facebook chat is typing - as it is for example also shown on Facebook.
This post is based upon the previous one about receiving messages. From that only a tiny bit has to be changed, since the "typing" event is send like a chat message: The message then has no Body, but the property Chatstate is then equal to Composing. If the user stops typing, a message with Chatstate active is send.
An exemplary MessageCallback function could look as follows:

private void MessageCallBack(object sender, agsXMPP.protocol.client.Message msg, object data)
{
    if (msg.Body != null)
    {
        MessageBox.Show(msg.From + ": " + msg.Body);
    }
    else
    {
        if (msg.Chatstate == agsXMPP.protocol.extensions.chatstates.Chatstate.composing)
            MessageBox.Show(msg.From + ": Started Typing");
        if (msg.Chatstate == agsXMPP.protocol.extensions.chatstates.Chatstate.active)
            MessageBox.Show(msg.From + ": Stopped Typing");
    }
}
I should mention though, that this function unfortunately does not work very well. The start of the composition of a message is send without problems, but the end of it often not.
Therefore, I thought about using a timer to reset the "composing" status. If someone has a better advice, I would be happy about a comment.

No comments:

Post a Comment