public static async Task <HttpResponseMessage> Run([HttpTrigger(AuthorizationLevel.Anonymous, "post", Route = null)] HttpRequestMessage req, TraceWriter log)
        {
            try
            {
                var channelSecret = System.Configuration.ConfigurationManager.AppSettings["ChannelSecret"];
                var events        = await req.GetWebhookEventsAsync(channelSecret);

                var connectionString = System.Configuration.ConfigurationManager.AppSettings["AzureWebJobsStorage"];
                var eventSourceState = await TableStorage <EventSourceState> .CreateAsync(connectionString, "eventsourcestate");

                var blobStorage = await BlobStorage.CreateAsync(connectionString, "linebotcontainer");

                //var app = new LineBotApp(lineMessagingClient, eventSourceState, blobStorage, log);

                //Samples app
                //var app = new DateTimePickerSampleApp(lineMessagingClient, log);
                //var app = new ImagemapSampleApp(lineMessagingClient, blobStorage, log);
                //var app = new ImageCarouselSampleApp(lineMessagingClient, blobStorage, log);
                //var app = new RichMenuSampleApp(lineMessagingClient, log);
                //var eventSourceLocation = await TableStorage<EventSourceLocation>.CreateAsync(connectionString, "eventsourcelocation");
                //var app = new PostbackMessageSampleApp(lineMessagingClient, eventSourceLocation, log);
                //var app = new ButtonsTemplateSampleApp(lineMessagingClient, blobStorage, log);
                var app = new CarouselTemplateSampleApp(lineMessagingClient, blobStorage, log);
                await app.RunAsync(events);
            }
            catch (InvalidSignatureException e)
            {
                return(req.CreateResponse(HttpStatusCode.Forbidden, new { Message = e.Message }));
            }
            catch (LineResponseException e)
            {
                log.Error(e.ToString());
                var debugUserId = System.Configuration.ConfigurationManager.AppSettings["DebugUser"];
                if (debugUserId != null)
                {
                    await lineMessagingClient.PushMessageAsync(debugUserId, $"{e.StatusCode}({(int)e.StatusCode}), {e.ResponseMessage.ToString()}");
                }
            }
            catch (Exception e)
            {
                log.Error(e.ToString());
                var debugUserId = System.Configuration.ConfigurationManager.AppSettings["DebugUser"];
                if (debugUserId != null)
                {
                    await lineMessagingClient.PushMessageAsync(debugUserId, e.Message);
                }
            }

            return(req.CreateResponse(HttpStatusCode.OK));
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Main run method
        /// </summary>
        /// <param name="req">HttpRequestMessage</param>
        /// <param name="log">TraceWriter</param>
        /// <returns>Result</returns>
        public static async Task <IActionResult> Run(HttpRequestMessage req, TraceWriter log)
        {
            try
            {
                log.Info("C# HTTP trigger function processed a request.");

                var channelAccessToken  = Environment.GetEnvironmentVariable("ChannelAccessToken");
                var channelSecret       = Environment.GetEnvironmentVariable("ChannelSecret");
                var lineMessagingClient = new LineMessagingClient(channelAccessToken);
                var events = await req.GetWebhookEventsAsync(channelSecret);

                var connectionString = Environment.GetEnvironmentVariable("AzureWebJobsStorage");
                var eventSourceState = await TableStorage <EventSourceState> .CreateAsync(connectionString, "eventsourcestate");

                var blobStorage = await BlobStorage.CreateAsync(connectionString, "linebotcontainer");

                // Create the LineBotApp and run it.
                var app = new LineBotApp(lineMessagingClient, eventSourceState, blobStorage, log);
                await app.RunAsync(events);
            }
            catch (InvalidSignatureException e)
            {
                return(new ObjectResult(e.Message)
                {
                    StatusCode = (int)HttpStatusCode.Forbidden
                });
            }
            catch (LineResponseException e)
            {
                log.Error(e.ToString());
                var debugUserId = Environment.GetEnvironmentVariable("DebugUser");
                if (debugUserId != null)
                {
                    await lineMessagingClient.PushMessageAsync(debugUserId, $"{e.StatusCode}({(int)e.StatusCode}), {e.ResponseMessage.ToString()}");
                }
            }
            catch (Exception e)
            {
                log.Error(e.ToString());
                var debugUserId = Environment.GetEnvironmentVariable("DebugUser");
                if (debugUserId != null)
                {
                    await lineMessagingClient.PushMessageAsync(debugUserId, e.Message);
                }
            }

            return(new OkObjectResult("OK"));
        }
        public static async Task <HttpResponseMessage> Run([HttpTrigger(AuthorizationLevel.Anonymous, "post", Route = null)] HttpRequestMessage req, TraceWriter log)
        {
            try
            {
                var channelSecret = System.Configuration.ConfigurationManager.AppSettings["ChannelSecret"];
                var events        = await req.GetWebhookEventsAsync(channelSecret);

                var connectionString = System.Configuration.ConfigurationManager.AppSettings["AzureWebJobsStorage"];
                var botStatus        = await TableStorage <BotStatus> .CreateAsync(connectionString, "botstatus");

                var blobStorage = await BlobStorage.CreateAsync(connectionString, "linebotcontainer");

                //var app = new LineBotApp(lineMessagingClient, botStatus, blobStorage, log);

                ///* To run sample apps in the samples directory, comment out the above line and cancel this comment out.
                var app = await AppSwitcher.SwitchAppsAsync(events, lineMessagingClient, botStatus, blobStorage, log);

                // */

                await app.RunAsync(events);
            }
            catch (InvalidSignatureException e)
            {
                return(req.CreateResponse(HttpStatusCode.Forbidden, new { e.Message }));
            }
            catch (LineResponseException e)
            {
                log.Error(e.ToString());
                var debugUserId = System.Configuration.ConfigurationManager.AppSettings["DebugUser"];
                if (debugUserId != null)
                {
                    await lineMessagingClient.PushMessageAsync(debugUserId, $"{e.StatusCode}({(int)e.StatusCode}), {e.ResponseMessage.ToString()}");
                }
            }
            catch (Exception e)
            {
                log.Error(e.ToString());
                var debugUserId = System.Configuration.ConfigurationManager.AppSettings["DebugUser"];
                if (debugUserId != null)
                {
                    await lineMessagingClient.PushMessageAsync(debugUserId, e.Message);
                }
            }

            return(req.CreateResponse(HttpStatusCode.OK));
        }