直线

时间:2018-05-09 14:27:31

标签: c# botframework bots ansi iso

最后

TLDR

目前我需要向Activity发送DirectLine发送带有特殊字符的邮件,字符串为Menú principal,请注意 ú < / strong>即可。其他一切都很好,但发送特殊字符让我哭泣。

但是当我发送序列化活动时,它会显示?,因此您可以看到Menú principal != Men? principal哪个会让机器人回答不同。

我尝试过一些东西,但似乎没有用,这是我目前的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);
    }

参数serializedJson是使用Json.NET序列化的Activity对象,当我尝试发送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}"

正如您所看到的,text"text\":\"Menú principal\"

在对话中看到活动日志后,它会将ú显示为?

https://gyazo.com/ff1fa7045de11ab0a5d721bc198363c0

您可以在this存储库中找到整个代码,uploadString函数位于this类,测试用例位于this文件中。

TLDR:我需要将Menú principal发送到DirectLine,当它到达僵尸程序时,它会显示如下:Men? principal

1 个答案:

答案 0 :(得分:0)

该字符不是有效的ASCII字符。尝试对字符串使用Unicode编码:

{{1}}