0

How to connect to XML-RPC Api from c# ,

A client can interact with a Pandorabot by POST'ing to:

http://www.pandorabots.com/pandora/talk-xml The form variables the client needs to POST are:

botid - see H.1 above. input - what you want said to the bot. custid - an ID to track the conversation with a particular customer. This variable is optional. If you don't send a value Pandorabots will return a custid attribute value in the element of the returned XML. Use this in subsequent POST's to continue a conversation.

How to call?

Philip Kirkbride
  • 21,381
  • 38
  • 125
  • 225
  • 1
    What research have you done? What have you tried? Are you experiencing a specific error that we can help you work around? – M.Babcock Jan 22 '12 at 03:02
  • Possible duplicate of: http://stackoverflow.com/questions/1348503/how-to-use-xmlrpc-in-c-sharp. Though they at least showed a little effort. – M.Babcock Jan 22 '12 at 03:09
  • It's not a duplicate of that question, What i've trying to do is send http request to that api and request the output. Im new to XML-RPC, i have no idea how it works. This is the documentation they provided http://pastebin.com/7qAX2t8m, I have no idea how to input the variables and send request and get respone, Can u please help – Zend Mastiff Jan 22 '12 at 06:56

1 Answers1

1

This should get you going:

   public void Talk()
   {
        string xmlResult = null;
        Result result = null;  // Result declared at the end 
        string botId = "c49b63239e34d1"; // enter your botid
        string talk = "Am I a human?"; 
        string custId = null; // (or a value )
        using (var wc = new WebClient())
        {
            var col = new NameValueCollection();

            col.Add("botid", botId);
            col.Add("input", talk);
            if (!String.IsNullOrEmpty(custId))
            {
                col.Add("custid", custId);
            }

            byte[] xmlResultBytes = wc.UploadValues(
                @"http://www.pandorabots.com/pandora/talk-xml", 
                "POST", 
                col);
            xmlResult = UTF8Encoding.UTF8.GetString(xmlResultBytes);
            result = Result.GetInstance(xmlResultBytes);
        }

        //raw result
        Console.WriteLine(xmlResult);

        // use the Result class
        if (result.status == 0)  // no error
        {
            Console.WriteLine("{0} -> {1}", 
                result.input, result.that);
        }
        else  // error
        {
            Console.WriteLine("Error: {0} : {1}", 
                result.input, result.message);
        }
    }


[XmlRoot(ElementName="result")]
public class Result
{
    static XmlSerializer ser = new XmlSerializer(typeof(Result) , "");

    public Result()
    {
    }

    public static Result GetInstance(byte[] bytes)
    {
        return (Result)ser.Deserialize(new MemoryStream(bytes));
    }

    [XmlAttribute]
    public int status { get; set; }
    [XmlAttribute]
    public string botid { get; set; }
    [XmlAttribute]
    public string custid { get; set; }
    [XmlElement]
    public string input { get; set; }
    [XmlElement]
    public string that { get; set; }
    [XmlElement]
    public string message { get; set; }
}
rene
  • 41,474
  • 78
  • 114
  • 152
  • What is "Result' in Result result = null;, It gives me an error, is it a datatype? – Zend Mastiff Jan 23 '12 at 12:49
  • YAH, my bad i saw the public declaration below, Okay. Thanks it worked perfect, Can u provide C++ version of it, i tried to but the functions give me error – Zend Mastiff Jan 23 '12 at 16:40
  • No, I cann't. I'm not a C++ dev. You tagged your question with C# so that is what you get :-). You can mark my answer as correct, try for your self to get the implementation for C++ going and when you're stuck ask a new question, showing your code and referencing this question and answer. Stackoverflow is not C++ implementation bot :-) – rene Jan 23 '12 at 19:17