Exemplo n.º 1
0
        // Issues a request to my private webhook server to queue and handle it.
        public async Task <string> ExecuteWebhook(string content, string username, bool wait)
        {
            Log.Debug("Issuing webhook request...");
            var payload = new WebhookRequest
            {
                Content         = content,
                Username        = username,
                WaitForResponse = wait
            };

            Log.Debug(payload.ToString());

            var stringPayload = JsonConvert.SerializeObject(payload);
            var httpContent   = new StringContent(stringPayload, Encoding.UTF8, "application/json");

            _client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue(BitPlugin.Instance.Config.WebhookAuth);
            var response = await _client.PostAsync($"{BitPlugin.Instance.Config.WebhookServer}/{_token}", httpContent);

            Log.Debug($"Response Status: {response.StatusCode}");
            string responseContent = null;

            if (!response.IsSuccessStatusCode)
            {
                responseContent = await response.Content.ReadAsStringAsync();

                Log.Error($"Request to webhook failed: {responseContent}");
            }

            if (!payload.WaitForResponse)
            {
                return(null);
            }

            // imagine not being able to do `responseContent || await response.Content...`
            // quality lang.
            if (responseContent != null)
            {
                return(responseContent);
            }
            else
            {
                return(await response.Content.ReadAsStringAsync());
            }
        }
        private WebhookResponse ProcessWebhookRequests(WebhookRequest value)
        {
            var         intentName     = value.QueryResult.Intent.DisplayName;
            IAppRequest iRequest       = null;
            string      controllerName = "";

            _commonMethods.ProcessIntends(value, intentName, ref iRequest, ref controllerName);
            _commonMethods.SetupAPICall(iRequest, controllerName, out RestClient clinet, out RestRequest request, Request);
            var responseResult = clinet.Execute <AppResponse>(request);

            _logger.LogInformation("Completed service request");
            var response = responseResult.Data;

            if (response != null && response.IsResponseSuccess)
            {
                var returnMsg = response.ResponseData.ConvertAllToASCII();
                returnMsg = CheckAndAddEndOfMessage(returnMsg);
                returnMsg = returnMsg.ConvertAllToASCII();
                var simpleResponses = new Message
                {
                    SimpleResponses = new SimpleResponses(),
                    Platform        = Platform.ActionsOnGoogle
                };
                simpleResponses.SimpleResponses.SimpleResponses_.Add(
                    new SimpleResponse
                {
                    Ssml = returnMsg.ConvertToSSML()
                });
                var returnValue = new WebhookResponse();
                returnValue.FulfillmentMessages.Add(simpleResponses);
                return(returnValue);
            }
            _logger.LogError($"{controllerName} could not process request.\n\tDetails:");
            _logger.LogError($"Input value{value.ToString()}");
            _logger.LogError($"Value sent as parameter to {controllerName}:\n{iRequest}");
            _logger.LogError($"Return value if any:{response}");
            return(new WebhookResponse
            {
                FulfillmentText = Utility.ErrorReturnMsg() + Utility.EndOfCurrentRequest()
            });
        }