Search code examples
c#botframeworkbotsansiiso

POST activity with special characters in Direct Line


TLDR at the end

Currently I need to POST an Activity to the DirectLine sending a message with a special character, the string is Menú principal, please notice the ú. Everything else works fine but sending special characters makes me cry.

But when I am sending the serialized activity, it displays a ?, so as you can see Menú principal != Men? principal which wil make the bot to answer different.

I've tried a few things but it doesn't seem to work, this is my current function to POST.

    /// <summary>
    /// Uploads to an URL and gets result
    /// </summary>
    /// <typeparam name="T">Type of object you are receiving</typeparam>
    /// <param name="bearer">Token</param>
    /// <param name="url">Url</param>
    /// <param name="serializedJson">Serialized JSON to send</param>
    /// <returns></returns>
    public static T uploadString<T>(string bearer, string url, string serializedJson)
    {
        string serializedResult = "";

        /// Webclient
        using (var client = new WebClient())
        {
            /// Looks like it goes wrong when uplading UTF8 words
            string ansistring = Encoding.ASCII.GetString(Encoding.ASCII.GetBytes(serializedJson));

            try
            {
                /// Add headers
                client.Headers.Add("Content-Type", "application/json");
                client.Headers.Add("Authorization", $"Bearer {bearer}");

                /// Upload string
                serializedResult = client.UploadString(url, ansistring);
            }
            catch (Exception e)
            {
                string a = e.Message;
            }
        }

        /// Get result and return it as an object
        return JsonConvert.DeserializeObject<T>(serializedResult);
    }

The parameter serializedJson is an Activity object serialized using Json.NET and it looks like this when I am trying to send Menú principal.

serializedJson = "{\"type\":\"message\",\"id\":\"c5908d50-5389-11e8-bd5c-45bb8f0ca339\",\"timestamp\":\"2018-05-09T13:06:22.49+00:00\",\"localTimestamp\":null,\"serviceUrl\":null,\"channelId\":null,\"from\":{\"id\":\"default-user\",\"name\":\"User\"},\"conversation\":null,\"recipient\":null,\"textFormat\":\"plain\",\"attachmentLayout\":null,\"membersAdded\":null,\"membersRemoved\":null,\"reactionsAdded\":null,\"reactionsRemoved\":null,\"topicName\":null,\"historyDisclosed\":null,\"locale\":\"es\",\"text\":\"Menú principal\",\"speak\":null,\"inputHint\":null,\"summary\":null,\"suggestedActions\":null,\"attachments\":[],\"entities\":[],\"channelData\":{\"clientActivityId\":\"1525870846394.1792692356311325.10\"},\"action\":null,\"replyToId\":null,\"value\":null,\"name\":null,\"relatesTo\":null,\"code\":null,\"expiration\":null,\"importance\":null,\"deliveryMode\":null,\"textHighlights\":null}"

As you can see the text is "text\":\"Menú principal\".

After seeing the Activity log in the conversation, it displays the ú as a ?.

https://gyazo.com/ff1fa7045de11ab0a5d721bc198363c0

You can find the entire code in this repository, the uploadString function is in this class and the test cases are in this file.

TLDR: I need to send Menú principal to DirectLine and when it reachs the bot it displays like this: Men? principal


Solution

  • That character is not a valid ASCII character. Try using Unicode encoding for your string:

    var stringToSend = Encoding.Unicode.GetString(Encoding.Unicode.GetBytes("Menú principal"));