Click here to Skip to main content
15,868,349 members
Articles / Programming Languages / C#
Tip/Trick

Using CleverBOT as Your Secretary on Facebook

,
Rate me:
Please Sign up or sign in to vote.
4.92/5 (4 votes)
17 Jan 2014CPOL2 min read 32.5K   829   2   29
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? :-)

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.

Image 1

Using the Code

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

C#
//

 
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. :-)

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)


Written By
Software Developer BiTS - Softmining
Italy Italy
Luigi Di Biasi is a PhD student in a computer science @ University of Salerno.

He is sole director of Arrowsoft.IT SRLs and CIO of Softmining SRL. He is founder of dibiasi.it materiale elettrico.

He studied Computer Science at the University of Salerno in 2013. From 2014 to 2016 he is [assegnista di ricerca] for PRIN 2010-2011 Data-Centric Genomic Computing (GenData 2020)).

He collaborate to the GRIMD developing (grid for molecular dynamics), which permits the parallel and distributed computation of many kinds of scientific software, to the YADA developing (Yet Another Docking Approach), a tool that improve and refine the predictions made by VINA Autodock and to the implementation of ProtComp alignment free tools, a new algorithm for sequences analysis that outperforms existing methods. He collaborate on 7 scientific paper.

In 2017 he is responsible developer for the RIRIBOX Project (in collaboration with Acea Pinerolese and Ecofficina SRL) and for energii.dk (in collaboration with Peme IVS). All these projects has involved collaborations between Italian, Danish and American developers.

In 2016 he is responsible developer for the “Una Buona Occasione” Project (in collaboration with Ecofficina SRL). He developed all the edutainment games related to this project (16 WebGL games).

Actually, his research activities range on the AI (algorithms developing, applications) and on the game developing.

Written By
Student
Italy Italy
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
SuggestionFix clverbot Pin
Member 1157892913-May-16 13:29
Member 1157892913-May-16 13:29 
PraiseRe: Fix clverbot Pin
Zenrac14-May-16 0:56
Zenrac14-May-16 0:56 
QuestionHelp me! Pin
Ulyses Neira25-Oct-15 3:59
Ulyses Neira25-Oct-15 3:59 
AnswerRe: Help me! Pin
luigidibiasi26-Oct-15 4:48
luigidibiasi26-Oct-15 4:48 
QuestionHelp :) Pin
Zenrac5-Sep-15 5:35
Zenrac5-Sep-15 5:35 
AnswerRe: Help :) Pin
luigidibiasi5-Sep-15 5:41
luigidibiasi5-Sep-15 5:41 
You should open the Project with c# ide
QuestionRe: Help :) Pin
Zenrac5-Sep-15 6:09
Zenrac5-Sep-15 6:09 
AnswerRe: Help :) Pin
luigidibiasi5-Sep-15 9:31
luigidibiasi5-Sep-15 9:31 
QuestionRe: Help :) Pin
Zenrac5-Sep-15 11:44
Zenrac5-Sep-15 11:44 
QuestionRe: Help :) Pin
Zenrac9-Sep-15 7:26
Zenrac9-Sep-15 7:26 
AnswerRe: Help :) Pin
luigidibiasi9-Sep-15 8:40
luigidibiasi9-Sep-15 8:40 
QuestionMessage Closed Pin
10-Sep-15 7:33
Zenrac10-Sep-15 7:33 
QuestionRe: Help :) Pin
Zenrac11-Sep-15 6:46
Zenrac11-Sep-15 6:46 
AnswerRe: Help :) Pin
luigidibiasi11-Sep-15 9:13
luigidibiasi11-Sep-15 9:13 
QuestionRe: Help :) Pin
Zenrac15-Sep-15 4:49
Zenrac15-Sep-15 4:49 
QuestionRe: Help :) Pin
Zenrac11-May-16 7:45
Zenrac11-May-16 7:45 
QuestionCould you update to a newer version of SDK Pin
Member 1039185915-May-14 14:10
Member 1039185915-May-14 14:10 
AnswerRe: Could you update to a newer version of SDK Pin
luigidibiasi23-May-14 22:57
luigidibiasi23-May-14 22:57 
Questionhelp please! Pin
Member 107307407-Apr-14 8:14
Member 107307407-Apr-14 8:14 
AnswerRe: help please! Pin
luigidibiasi30-Apr-14 4:56
luigidibiasi30-Apr-14 4:56 
QuestionMac Support? Pin
Member 106492246-Mar-14 8:38
Member 106492246-Mar-14 8:38 
AnswerRe: Mac Support? Pin
luigidibiasi11-Mar-14 23:07
luigidibiasi11-Mar-14 23:07 
GeneralRe: Mac Support? Pin
Member 1064922412-Mar-14 3:08
Member 1064922412-Mar-14 3:08 
GeneralRe: Mac Support? Pin
luigidibiasi12-Mar-14 22:43
luigidibiasi12-Mar-14 22:43 
Questionhelp Pin
Member 105717724-Feb-14 14:24
Member 105717724-Feb-14 14:24 

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

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