Exemple #1
0
        private async Task ReplyImageCarouselAsync(string replyToken, string blobDirectoryName)
        {
            var columns = BlobStorage.ListBlobUri(blobDirectoryName)
                          .Select(uri =>
                                  new ImageCarouselColumn(uri.ToString(),
                                                          new MessageTemplateAction(uri.Segments.Last(), $"I selected image {uri.Segments.Last()}!"))).ToList();

            if (columns.Count == 0)
            {
                await MessagingClient.ReplyMessageAsync(replyToken, "Upload image for \"Carousel Message\".");

                return;
            }
            var template = new ImageCarouselTemplate(columns);

            await MessagingClient.ReplyMessageAsync(replyToken, new[] { new TemplateMessage("imageCarousel", template) });
        }
        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));
        }
Exemple #3
0
        private async Task UploadImageAsync(string replyToken, string messageId, string blobDirectoryName)
        {
            var imageStream = await MessagingClient.GetContentStreamAsync(messageId);

            var image = Image.FromStream(imageStream);

            var imageCount = BlobStorage.ListBlobUri(blobDirectoryName).Count();

            if (imageCount == 5)
            {
                await BlobStorage.DeleteDirectoryAsync(blobDirectoryName);

                imageCount = 0;
            }

            await BlobStorage.UploadImageAsync(image, blobDirectoryName, (imageCount + 1) + ".jpeg");

            await MessagingClient.ReplyMessageAsync(replyToken, $"Image uploaded ({imageCount + 1}).");
        }
Exemple #4
0
        private async Task ReplyImagemapAsync(string replyToken, string messageId, string blobDirectoryName)
        {
            var imageStream = await MessagingClient.GetContentStreamAsync(messageId);

            var image = Image.FromStream(imageStream);

            using (var g = Graphics.FromImage(image))
            {
                g.DrawLine(Pens.Red, image.Width / 2, 0, image.Width / 2, image.Height);
                g.DrawLine(Pens.Red, 0, image.Height / 2, image.Width, image.Height / 2);
            }

            var uri = await UploadImageAsync(1040);

            await UploadImageAsync(700);
            await UploadImageAsync(460);
            await UploadImageAsync(300);
            await UploadImageAsync(240);

            var imageSize       = new ImagemapSize(1024, (int)(1040 * (double)image.Height / image.Width));
            var areaWidth       = imageSize.Width / 2;
            var areaHeight      = imageSize.Height / 2;
            var imagemapMessage = new ImagemapMessage(uri.ToString().Replace("/1040", ""),
                                                      "Sample Imagemap",
                                                      imageSize,
                                                      new IImagemapAction[] {
                new MessageImagemapAction(new ImagemapArea(0, 0, areaWidth, areaHeight), "Area Top-Left"),
                new MessageImagemapAction(new ImagemapArea(areaWidth, 0, areaWidth, areaHeight), "Area Top-Right"),
                new MessageImagemapAction(new ImagemapArea(0, areaHeight, areaWidth, areaHeight), "Area Bottom-Left"),
                new MessageImagemapAction(new ImagemapArea(areaWidth, areaHeight, areaWidth, areaHeight), "Area Bottom-Right"),
            });

            await MessagingClient.ReplyMessageAsync(replyToken, new[] { imagemapMessage });

            async Task <Uri> UploadImageAsync(int baseSize)
            {
                var img = image.GetThumbnailImage(baseSize, image.Height * baseSize / image.Width, () => false, IntPtr.Zero);

                return(await BlobStorage.UploadImageAsync(img, blobDirectoryName + "/" + messageId, baseSize.ToString()));
            }
        }
Exemple #5
0
 public ImagemapSampleApp(LineMessagingClient lineMessagingClient, BlobStorage blobStorage, TraceWriter log)
 {
     MessagingClient = lineMessagingClient;
     BlobStorage     = blobStorage;
     Log             = log;
 }
        protected override async Task OnMessageAsync(MessageEvent ev)
        {
            var msg = ev.Message as TextEventMessage;

            if (msg == null)
            {
                return;
            }

            switch (msg.Text)
            {
            case "Rectangle-Cover":
                await ReplyCarouselTemplateMessageAsync(ev.ReplyToken, ImageAspectRatioType.Rectangle, ImageSizeType.Cover);

                break;

            case "Square-Contain":
                await ReplyCarouselTemplateMessageAsync(ev.ReplyToken, ImageAspectRatioType.Square, ImageSizeType.Contain);

                break;

            case "Square-Cover":
                await ReplyCarouselTemplateMessageAsync(ev.ReplyToken, ImageAspectRatioType.Square, ImageSizeType.Cover);

                break;

            case "Rectangle-Contein":
            default:
                await ReplyCarouselTemplateMessageAsync(ev.ReplyToken, ImageAspectRatioType.Rectangle, ImageSizeType.Contain);

                break;
            }

            async Task ReplyCarouselTemplateMessageAsync(string replyToken, ImageAspectRatioType imageAspectRatio, ImageSizeType imageSize)
            {
                var imageUri = BlobStorage.ListBlobUri(blobDirectoryName).FirstOrDefault(uri => uri.ToString().EndsWith(imageName));

                if (imageUri == null)
                {
                    imageUri = await BlobStorage.UploadImageAsync(Properties.Resources.sample_image, blobDirectoryName, imageName);
                }
                var defaultAction   = new MessageTemplateAction("Default-Action", "Default-Action");
                var templateMessage = new TemplateMessage("CarouselTemplate",
                                                          new CarouselTemplate(new[]
                {
                    new CarouselColumn(
                        imageAspectRatio + "-" + imageSize,
                        imageUri.ToString(),
                        "Test of thumbnail image settings",
                        new [] { new MessageTemplateAction("Rectangle-Contain", "Rectangle-Contain") },
                        "#FF0000",
                        defaultAction),
                    new CarouselColumn(
                        imageAspectRatio + "-" + imageSize,
                        imageUri.ToString(),
                        "Test of thumbnail image settings",
                        new [] { new MessageTemplateAction("Rectangle-Cover", "Rectangle-Cover") },
                        "#00FF00",
                        defaultAction),
                    new CarouselColumn(
                        imageAspectRatio + "-" + imageSize,
                        imageUri.ToString(),
                        "Test of thumbnail image settings",
                        new [] { new MessageTemplateAction("Square-Contain", "Square-Contain") },
                        "#0000FF",
                        defaultAction),
                    new CarouselColumn(
                        imageAspectRatio + "-" + imageSize,
                        imageUri.ToString(),
                        "Test of thumbnail image settings",
                        new [] { new MessageTemplateAction("Square-Cover", "Square-Cover") },
                        "#FF00FF",
                        defaultAction)
                },
                                                                               imageAspectRatio,
                                                                               imageSize));

                await MessagingClient.ReplyMessageAsync(replyToken, new[] { templateMessage });
            }
        }
 public CarouselTemplateSampleApp(LineMessagingClient lineMessagingClient, BlobStorage blobStorage, TraceWriter log)
 {
     MessagingClient = lineMessagingClient;
     BlobStorage     = blobStorage;
     Log             = log;
 }
 public LineBotApp(LineMessagingClient lineMessagingClient, TableStorage <BotStatus> tableStorage, BlobStorage blobStorage, TraceWriter log)
 {
     MessagingClient = lineMessagingClient;
     SourceState     = tableStorage;
     BlobStorage     = blobStorage;
     Log             = log;
 }
        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);
        }