Exemplo n.º 1
0
        public Task <HttpResponseMessage> SetWebhook(Webhook webhook)
        {
            string jsonMsg = webhook.ToJson();
            Task <HttpResponseMessage> postAsync = _httpClient.PostAsync(URlWebhook, EncodeMsg(jsonMsg));

            return(postAsync);
        }
        private static void SetChat2DeskWebhook()
        {
            var chat2deskToken = WebConfigurationManager.AppSettings["chat2deskToken"];

            var serviceUrl = WebConfigurationManager.AppSettings["serviceUrl"];

            using (WebClient client = new WebClient())
            {
                var isWebhookFound = false;

                client.Encoding = Encoding.UTF8;
                client.Headers["Content-type"]  = "application/json";
                client.Headers["Authorization"] = chat2deskToken;
                var urlString = "https://api.chat2desk.com/v1/webhooks";

                var responseJson = client.DownloadString(urlString);

                var response = GetWebhooks.FromJson(responseJson);

                if (response != null && response.Data != null)
                {
                    foreach (var webhook in response.Data)
                    {
                        if (webhook.Url.AbsoluteUri == serviceUrl + "/api/webhook")
                        {
                            isWebhookFound = true;
                        }
                    }
                }

                if (!isWebhookFound)
                {
                    var webhook = new Webhook()
                    {
                        Url    = new Uri(serviceUrl + "/api/webhook"),
                        Name   = "webhook1",
                        Events = new string[] {
                            "inbox",
                            "outbox",
                            "new_client",
                            "add_tag_to_client",
                            "add_tag_to_request",
                            "delete_tag_from_client",
                            "close_dialog",
                            "close_request",
                            "new_qr_code"
                        }
                    };

                    client.Encoding = Encoding.UTF8;
                    client.Headers["Content-type"]  = "application/json";
                    client.Headers["Authorization"] = chat2deskToken;

                    var postResponseJson = client.UploadString(urlString, "POST", webhook.ToJson());

                    var responsePost = GetWebhooksResponse.FromJson(responseJson);

                    if (responsePost.Status == "error")
                    {
                        throw new Exception("При настройке взаимодействия с Chat2Desk возникла ошибка: " + responsePost.Message);
                    }
                }
            }
        }