Click here to Skip to main content
Click here to Skip to main content

Tagged as

Using CleverBOT as Your Secretary on Facebook

, , 17 Jan 2014 CPOL
Rate this:
Please Sign up or sign in to vote.
How to use agsXMPP and ChatterBotAPI for managing communication between users and cleverbot

Introduction

How many of you have never run into a talkative friend? Smile | :)

Recently, I have been swamped with too many useless messages from some of my Facebook contacts, and I've looked for a funny method to make them tired! I thought of using cleverbot as my secretary, in order to answer talkative humans with strange phrases (and make them tired). Obviously, behind this funny description, there are some lines of code that can be used in any environment. In particular, you can extract from prototype the code for login in facebook with C#, send message to user and handle received messages.

The goal of this tip is the description of following things:

  1. How to use the agsXMPP SDK to interact with Facebook platform. (Login, send and receive message)
  2. How to use ChatterBotAPI in order to interact with CleverBot.

I hope it will be helpful for you.

Background

In order to extend the prototype attached to this tip, you only need to know what is XMPP protocol (and how to use C#). The structure of our prototype will be as shown in the following figure.

Using the Code

Since we use a third-party API, the code is really simple (and is self-explanatory).

//

 
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 ChatterBotAPI;
 
namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        } 

        XmppClientConnection xmpp = new XmppClientConnection("chat.facebook.com");
 
        ChatterBotFactory factory = new ChatterBotFactory();
        ChatterBot bot1;
        ChatterBotSession bot1session;
 
        private void Form1_Load(object sender, EventArgs e)
        { 
            bot1 = factory.Create(ChatterBotType.CLEVERBOT);
            bot1session = bot1.CreateSession();            
 
            xmpp.Username = "your_user_name"; // the username not the email 
            xmpp.Password = "your_password";
            
            xmpp.OnLogin += new ObjectHandler(OnLogin);
            xmpp.OnError += new ErrorHandler(xmpp_OnError);
            xmpp.OnAuthError += new XmppElementHandler(xmpp_OnAuthError);
            xmpp.OnMessage += new MessageHandler(xmpp_OnMessage);
            xmpp.AutoResolveConnectServer = false;
            xmpp.Open();
        }
 
        void xmpp_OnMessage(object sender, agsXMPP.protocol.client.Message msg)
        {
            if (msg.Chatstate.ToString()=="composing")
                return;
 
            String a = msg.FirstChild.InnerXml;
            
            if (a == "")
                return;
            // from
            Console.WriteLine("Facebook: " + a);
            Jid to = msg.From;
 
            string s = bot1session.Think(a);                        
            agsXMPP.protocol.client.Message nmsg = new agsXMPP.protocol.client.Message();
            nmsg.Type = agsXMPP.protocol.client.MessageType.chat;
            nmsg.To = to;
            nmsg.Body = s;
            xmpp.Send(nmsg);
            Console.WriteLine("\tClever: " + s);
            
        }
 
        void xmpp_OnAuthError(object sender, agsXMPP.Xml.Dom.Element e)
        {
            
            throw new NotImplementedException();
        }
 
        void xmpp_OnError(object sender, Exception ex)
        {
            throw new NotImplementedException();
        }
 
        private void OnLogin(object sender)
        {
            
            Presence p = new Presence(ShowType.chat, "Online");
            p.Type = PresenceType.available;
            xmpp.Send(p);
        }    
    }
} 

// 

Points of Interest

Here, we have described the guidelines and the code to build a prototype of bot-secretary. Smile | :)

You need to keep in mind that, in order to have the best performance from CleverBot, you need to open one ChatterBotSession for each 'friend' you want to handle: The prototype attached handles all messages you receive with one single BotSession. So, CleverBot can think that it's talking with the same person though it is talking with all your friends.

History

  • 17/01/2014: First release
  • 17/01/2014: English revision made by Bruno Interlandi

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Authors

luigidibiasi
Software Developer F.lli Di Biasi SNC
Italy Italy
No Biography provided
Follow on   Twitter   LinkedIn

binterlandi
Student
Italy Italy
No Biography provided
Follow on   LinkedIn

Comments and Discussions

 
QuestionCould you update to a newer version of SDK PinmemberMember 1039185915-May-14 14:10 
AnswerRe: Could you update to a newer version of SDK Pinmemberluigidibiasi23-May-14 22:57 
Questionhelp please! PinmemberMember 107307407-Apr-14 8:14 
AnswerRe: help please! Pinmemberluigidibiasi30-Apr-14 4:56 
QuestionMac Support? PinmemberMember 106492246-Mar-14 8:38 
AnswerRe: Mac Support? Pinmemberluigidibiasi11-Mar-14 23:07 
GeneralRe: Mac Support? PinmemberMember 1064922412-Mar-14 3:08 
GeneralRe: Mac Support? Pinmemberluigidibiasi12-Mar-14 22:43 
Questionhelp PinmemberMember 105717724-Feb-14 14:24 
AnswerRe: help Pinmemberluigidibiasi5-Feb-14 6:21 
GeneralCleverbot to Cleverbot PinprotectorDaveAuld17-Jan-14 7:59 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Mobile
Web02 | 2.8.141015.1 | Last Updated 17 Jan 2014
Article Copyright 2014 by luigidibiasi, binterlandi
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid