Google Talk (GTalk) Autoreply using .NET

Abstract: In this article, we will see how you can develop a simple Gtalk client using .NET using which you can notify your users that you are away from your desk.

Google Talk (GTalk) Autoreply using .NET

In this article we will see how you can develop a simple GTalk client using .NET using which you can notify your users that you are away from your desk. This article will also discuss how to use the Extensible Messaging and Presence Protocol (XMPP) protocol libraries. GTalk services are built on the XMPP protocol. The article is primarily in VB.NET, but C# code has also been mentioned in the article. Below is the screenshot of how our application will look like.

Below is the screenshot of our application in action.

Before getting started with the application, it is important for you to learn a little about Extensible Messaging and Presence Protocol (XMPP). According to www.xmpp.org

The Extensible Messaging and Presence Protocol (XMPP) is an open technology for real-time communication, which powers a wide range of applications including instant messaging, presence, multi-party chat, voice and video calls, collaboration, lightweight middleware, content syndication, and generalized routing of XML data.

You can learn more about XMPP at www.xmpp.org. Google Talk services is built on XMPP protocol. This allows us to build clients which can connect to Google Talk.

In a short while, you will see that we will be saving user details in the application variables. In the above code we are just loading them to the appropriate controls.

Note: It is not advisable to set ‘CheckForIllegalCrossThreadCalls’ to False. This is not a good solution to be used in a production application.

Step 7:Add the following code to ChkStartAutomatic’sCheckedChanged Status. This code will add the required values to the registry there by allowing the user to start the application automatically on Windows Startup.

Step 8:The following code in btnSave’s Click Event will do the job of doing basic validation, Login to Gmail and wait for a message. You will see that we have added 3 handlers for onMessage, onAuthError and onLogin. These handlers will take care of Sending the Autoreply, handling Authentication error if the Username/Password is incorrect and the necessary settings when the login is successful.

C#

if (txtUserName.Text.Trim() == "")

{

lblStatus.Text = "Please enter Username.";

txtUserName.Focus();

return;

}

if (txtPassword.Text.Trim() == "")

{

lblStatus.Text = "Please enter Password.";

txtPassword.Focus();

return;

}

if (txtMessage.Text.Trim() == "")

{

lblStatus.Text = "Please enter Message.";

txtMessage.Focus();

return;

}

lblStatus.Text = "Logging in. Please wait...";

objXmpp = new agsXMPP.XmppClientConnection();

agsXMPP.Jid jid = null;

jid = new agsXMPP.Jid(txtUserName.Text.Trim() + "@gmail.com");

objXmpp.Password = txtPassword.Text.Trim();

objXmpp.Username = jid.User;

objXmpp.Server = jid.Server;

objXmpp.AutoResolveConnectServer = true;

try

{

objXmpp.OnMessage += messageReceived;

objXmpp.OnAuthError += loginFailed;

objXmpp.OnLogin += loggedIn;

objXmpp.Open();

}

catch (Exception ex)

{

MessageBox.Show(ex.Message);

}

VB.NET

If txtUserName.Text.Trim = ""Then

lblStatus.Text = "Please enter Username."

txtUserName.Focus()

ExitSub

EndIf

If txtPassword.Text.Trim = ""Then

lblStatus.Text = "Please enter Password."

txtPassword.Focus()

ExitSub

EndIf

If txtMessage.Text.Trim = ""Then

lblStatus.Text = "Please enter Message."

txtMessage.Focus()

ExitSub

EndIf

lblStatus.Text = "Logging in. Please wait..."

objXmpp = New agsXMPP.XmppClientConnection

Dim jid As agsXMPP.Jid

jid = New agsXMPP.Jid(txtUserName.Text.Trim + "@gmail.com")

objXmpp.Password = txtPassword.Text.Trim

objXmpp.Username = jid.User

objXmpp.Server = jid.Server

objXmpp.AutoResolveConnectServer = True

Try

AddHandler objXmpp.OnMessage, AddressOf messageReceived

AddHandler objXmpp.OnAuthError, AddressOf loginFailed

AddHandler objXmpp.OnLogin, AddressOf loggedIn

objXmpp.Open()

Catch ex As Exception

MsgBox(ex.Message)

EndTry

Step 9:Below is the code for the three functions messageReceived, loginFailed and loggedIn

ShobanKumar, is a 23 year old Client App Dev MVP from Trivandrum mainly working on ASP, ASP.NET, VB, VB.NET and PHP. You can also find him blogging at www.crankup.net and www.codegeeks.net. Follow him on twitter @shobankr

Was this article worth reading? Share it with fellow developers too. Thanks!

ShobanKumar is an ex-Microsoft MVP in SharePoint who currently works as a SharePoint Consultant. You can read more about his projects at http://shobankumar.com. You can also follow him on twitter @shobankr

Feedback - Leave us some adulation, criticism and everything in between!

it is not working.... it just diaplays the message logging in and then nothing happens

Comment posted by
Shoban Kumar
on Sunday, January 31, 2010 1:08 AM

@Amit
Dd you try running the attached source code download? Are you behind any firewall?

Comment posted by
Luis
on Friday, June 25, 2010 3:54 AM

Thanks for sharing this with us. Can post a simple code for sending a message? (not a reply just a message). I wanted to use this when I need to override the bot.

Keep up the good work!

Comment posted by
Luis
on Friday, June 25, 2010 6:05 AM

Thanks for sharing this with us. Can post a simple code for sending a message? (not a reply just a message). I wanted to use this when I need to override the bot.

Keep up the good work!

Comment posted by
mohammed
on Friday, September 10, 2010 7:16 PM

hi

Comment posted by
gene
on Friday, December 3, 2010 7:52 AM

thank you for the effort, but this is really sloppy job. in just a few lines of code there are so many errors, omissions... the download is not worth anything. if you're doing something for others, do it well, otherwise there's more grief than good.

HiShobanKumar,
I am using your gtalk autoreply code its awesome working superb,i want to implement it in yahoo messenger i tried using many yaho messenger server name n port,can u help to solve this issue that how to connect yahoo through agsxmpp dll,Please mail me code if possible