我已经设法使用C#连接到我的帐户和访问密钥的LiveChat API。我在此https://github.com/livechat/api-client-csharp的帮助下将LiveChat API包含在c#控制台项目中。我真正想做的是访问其他代理的所有聊天消息,这样我就可以将它们存储在数据结构中并推送到我的sql server。我从LIVECHAT API文档中尝试了这个代码,并在名为getChatMessages的菜单列表中添加了一个选项,它返回一个存储在字符串变量中的JSON FORMAT字符串。现在我想访问JSON格式的每个元素,这样我就可以存储在数据结构中了吗?
public async Task getChatMessages()
{
Dictionary<string, string> parameters = new Dictionary<string, string>();
parameters.Add("date_from", "2017-10-27");
parameters.Add("query", "test");
string result = await Api.Archives.Get(parameters);
return await Api.Get(result);
}
以下代码是调用API的实际函数
public async Task<string> Get(Dictionary<string, string> parameters)
{
string uri = "";
if (parameters != null && parameters.Count > 0)
{
foreach (var keyValuePair in parameters)
{
if (uri.Length == 0)
{
uri = "chats?";
}
else
{
uri += "&";
}
uri += string.Format("{0}={1}", keyValuePair.Key, HttpUtility.UrlEncode(keyValuePair.Value));
}
}
return await Api.Get(uri);
}
该值以非结构化JSON格式返回到字符串变量中。这是响应格式;我使用逗号(,)作为分隔符将每个单独的元素作为新行写入文本文件,因此它是可读的。我真正想要的是访问聊天消息的每个元素,以便我可以推送到sql表格式。以下是API给出的JSON格式。
{"chats":[{"type":"chat"
"id":"OYTHUUBLB2"
"tickets":[]
"visitor_name":"val"
"visitor_id":"S1509127853.f7c1666ec1"
"visitor_ip":"avl"
"visitor":{"id":"S1509127853.f7c1666ec1"
"name":"val"
"email":"val"
"ip":"76.71.60.221"
"city":"Brampton"
"region":"Ontario"
"country":"Canada"
"country_code":"CA"
"timezone":"America/Rainy_River"}
"agents":[{"display_name":"val"
"email":"val"
"ip":"val"}]
"supervisors":[]
"rate":"not_rated"
"duration":213
"chat_start_url":"val"
"group":[3]
"started":"val"
"pending":false
"tags":[]
"timezone":"America/Bogota"
"messages":[{"author_name":"val"
"text":"val"
"date":"val"
"timestamp":1509127958
"agent_id":"val"
"user_type":"agent"
"type":"message"
"welcome_message":true}
{"author_name":"val"
"text":"val"
"date":"val"
"timestamp":1509127976
"user_type":"visitor"
"type":"message"}
{"author_name":"val"
"text":"val"
"date":"val"
"timestamp":1509128147
"user_type":"visitor"
"type":"message"}]
"prechat_survey":[{"key":"Name:"
"value":"val"
"id":"148156886386907509"}
{"key":"E-mail:"
"value":"val"
"id":"148156886386907954"}
{"key":"Choose a department:"
"value":"val"
"id":"148156886386909015"}
{"key":"Question:"
"value":"I have another question"
"id":"val"}]
"events":[{"author_name":"val"
"text":"val"
"date":"val"
"timestamp":1509127958
"agent_id":"val"
"user_type":"agent"
"type":"message"
"welcome_message":true}
{"author_name":"val"
"text":"val"
"date":"val"
"timestamp":val
"user_type":"visitor"
"type":"message"}
{"author_name":"val"
"text":"val"
"date":"val"
"timestamp":val
"user_type":"val"
"type":"message"}
{"text":"val"
"date":"val"
"timestamp":val
"type":"event"
"user_type":"visitor"}]
"engagement":"immediate_start"
"started_timestamp":1509127958
"ended_timestamp":1509128171
"ended":"val"}
]
"total":1
"pages":1}