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));
        }
Exemplo n.º 2
0
        public static async Task <WebhookApplication> SwitchAppsAsync(IEnumerable <WebhookEvent> events, LineMessagingClient line, TableStorage <BotStatus> botStatus, BlobStorage blobStorage, TraceWriter log)
        {
            var ev     = events.First();
            var status = await botStatus.FindAsync(ev.Source.Type.ToString(), ev.Source.Id);

            if (status == null)
            {
                status = new BotStatus()
                {
                    SourceType = ev.Source.Type.ToString(),
                    SourceId   = ev.Source.Id,
                    CurrentApp = "@"
                };
            }

            var message = (ev as MessageEvent)?.Message as TextEventMessage;
            var text    = message?.Text;

            if (text == null || !text.StartsWith("@"))
            {
                text = status.CurrentApp;
            }
            text = text.Trim().ToLower();
            WebhookApplication app = null;

            if (text != "@richmenu")
            {
                try
                {
                    await line.UnLinkRichMenuFromUserAsync(ev.Source.Id);
                }
                catch (LineResponseException e)
                {
                    if (e.StatusCode != HttpStatusCode.NotFound)
                    {
                        throw;
                    }
                }
            }

            switch (text)
            {
            case "@":
                app = new LineBotApp(line, botStatus, blobStorage, log);
                break;

            case "@buttons":
                app = new ButtonsTemplateSampleApp(line, blobStorage, log);
                break;

            case "@carousel":
                app = new CarouselTemplateSampleApp(line, blobStorage, log);
                break;

            case "@postback":
                app = new PostbackMessageSampleApp(line, botStatus, log);
                break;

            case "@imagemap":
                app = new ImagemapSampleApp(line, blobStorage, log);
                break;

            case "@imagecarousel":
                app = new ImageCarouselSampleApp(line, blobStorage, log);
                break;

            case "@datetime":
                app = new DateTimePickerSampleApp(line, log);
                break;

            case "@richmenu":
                app = new RichMenuSampleApp(line, log);
                break;

            default:
                text = "@";
                app  = new LineBotApp(line, botStatus, blobStorage, log);
                break;
            }
            status.CurrentApp = text;
            await botStatus.UpdateAsync(status);

            return(app);
        }