Exemple #1
0
        /// <summary>
        /// Process a calling request within the calling conversaion.
        /// </summary>
        /// <param name="toBot"> The calling request sent to the bot.</param>
        /// <param name="callRequestType"> The type of calling request.</param>
        /// <returns> The response from the bot.</returns>
        public static async Task <HttpResponseMessage> SendAsync(HttpRequestMessage toBot, CallRequestType callRequestType)
        {
            using (var scope = CallingModule.BeginLifetimeScope(Container, toBot))
            {
                var bot           = scope.Resolve <ICallingBot>();
                var context       = scope.Resolve <CallingContext>();
                var parsedRequest = await context.ProcessRequest(callRequestType);

                Trace.TraceInformation($"Processing X-Microsoft-Skype-Chain-ID: {parsedRequest.SkypeChainId}");
                if (parsedRequest.Faulted())
                {
                    return(context.Request.CreateResponse(parsedRequest.ParseStatusCode));
                }
                else
                {
                    try
                    {
                        string res = string.Empty;
                        switch (callRequestType)
                        {
                        case CallRequestType.IncomingCall:
                            res = await bot.CallingBotService.ProcessIncomingCallAsync(parsedRequest.Content);

                            break;

                        case CallRequestType.CallingEvent:
                            res = await bot.CallingBotService.ProcessCallbackAsync(parsedRequest.Content, parsedRequest.AdditionalData);

                            break;

                        default:
                            throw new Exception($"Unsupported call request type: {callRequestType}");
                        }

                        return(new HttpResponseMessage(HttpStatusCode.OK)
                        {
                            Content = new StringContent(res, Encoding.UTF8, "application/json")
                        });
                    }
                    catch (Exception e)
                    {
                        return(context.Request.CreateErrorResponse(HttpStatusCode.InternalServerError, e));
                    }
                }
            }
        }
Exemple #2
0
        public static async Task <IActionResult> SendAsync(HttpRequest toBot, CallRequestType callRequestType)
#endif
        {
            using (var scope = CallingModule.BeginLifetimeScope(Container, toBot))
            {
                var bot           = scope.Resolve <ICallingBot>();
                var context       = scope.Resolve <CallingContext>();
                var parsedRequest = await context.ProcessRequest(callRequestType);

                Trace.TraceInformation($"Processing X-Microsoft-Skype-Chain-ID: {parsedRequest.SkypeChainId}");
                if (parsedRequest.Faulted())
                {
#if NET46
                    return(context.Request.CreateResponse(parsedRequest.ParseStatusCode));
#else
                    return(new StatusCodeResult((int)parsedRequest.ParseStatusCode));
#endif
                }
                else
                {
                    try
                    {
                        string res = string.Empty;
                        switch (callRequestType)
                        {
                        case CallRequestType.IncomingCall:
                            res = await bot.CallingBotService.ProcessIncomingCallAsync(parsedRequest.Content);

                            break;

                        case CallRequestType.CallingEvent:
                            res = await bot.CallingBotService.ProcessCallbackAsync(parsedRequest.Content, parsedRequest.AdditionalData);

                            break;

                        default:
                            throw new Exception($"Unsupported call request type: {callRequestType}");
                        }
#if NET46
                        return(new HttpResponseMessage(HttpStatusCode.OK)
                        {
                            Content = new StringContent(res, Encoding.UTF8, "application/json")
                        });
#else
                        return(new ContentResult {
                            Content = res, ContentType = "application/json", StatusCode = 200
                        });
#endif
                    }
                    catch (Exception e)
                    {
#if NET46
                        return(context.Request.CreateErrorResponse(HttpStatusCode.InternalServerError, e));
#else
                        // TODO show exception in some more salient way e.g. in logs.
                        // Because an exception will be thrown for invalid CallbackUrl.
                        return(new ObjectResult(new { Type = e.GetType().FullName, Message = e.Message })
                        {
                            StatusCode = 500
                        });
#endif
                    }
                }
            }
        }