Example #1
0
        public async Task <HttpResponseMessage> Post()
        {
            var content = await Request.Content.ReadAsStringAsync();

            var signature = Request.Headers.GetValues("X-Line-Signature").FirstOrDefault();

            if (signature == null || !Client.ValidateSignature(content, signature))
            {
                return(Request.CreateErrorResponse(HttpStatusCode.NotFound, "not found"));
            }

            var events = Client.ParseEvent(content);

            foreach (var ev in events)
            {
                switch (ev.Type)
                {
                case EventType.Message:
                    var mev = (MessageEvent)ev;
                    switch (mev.Message.Type)
                    {
                    case MessageType.Text:
                    {
                        var message = (TextEventMessage)mev.Message;
                        await Client.ReplyTextAsync(mev.ReplyToken, message.Text);

                        break;
                    }

                    case MessageType.Image:
                    {
                        var message = (ImageEventMessage)mev.Message;
                        var image   = await Client.GetContentAsync(message.Id);

                        var key = AddToCache(image);
                        await Client.ReplyMessageAsync(mev.ReplyToken, new ImageMessage
                            {
                                OriginalContentUrl = GetContentUrl(key),
                                PreviewImageUrl    = GetContentUrl(key),
                            });

                        break;
                    }

                    case MessageType.Video:
                    {
                        var message = (VideoEventMessage)mev.Message;
                        var video   = await Client.GetContentAsync(message.Id);

                        var key      = AddToCache(video);
                        var thumb    = MakeThumbnail(video);
                        var thumbkey = AddToCache(thumb);
                        await Client.ReplyMessageAsync(mev.ReplyToken, new VideoMessage
                            {
                                OriginalContentUrl = GetContentUrl(key),
                                PreviewImageUrl    = GetContentUrl(thumbkey),
                            });

                        break;
                    }

                    case MessageType.Audio:
                    {
                        var message = (AudioEventMessage)mev.Message;
                        var audio   = await Client.GetContentAsync(message.Id);

                        var key = AddToCache(audio);
                        await Client.ReplyMessageAsync(mev.ReplyToken, new AudioMessage
                            {
                                OriginalContentUrl = GetContentUrl(key),
                                Duration           = 0.5,
                            });

                        break;
                    }

                    case MessageType.Location:
                    {
                        var message = (LocationEventMessage)mev.Message;
                        await Client.ReplyMessageAsync(mev.ReplyToken, new LocationMessage
                            {
                                Address   = message.Address,
                                Title     = message.Title,
                                Latitude  = message.Latitude,
                                Longitude = message.Longitude,
                            });

                        break;
                    }

                    case MessageType.Sticker:
                    {
                        var message = (StickerEventMessage)mev.Message;
                        await Client.ReplyMessageAsync(mev.ReplyToken, new StickerMessage
                            {
                                PackageId = message.PackageId,
                                StickerId = message.StickerId,
                            });

                        break;
                    }
                    }
                    break;
                }
            }

            return(Request.CreateResponse(HttpStatusCode.OK, "OK"));
        }