Пример #1
0
        public static void ProcessQueueMessage([QueueTrigger("line-bot-workitems")] string message, TextWriter log)
        {
            log.WriteLine(message);

            var data = JsonConvert.DeserializeObject <LineMessageObject>(message);

            if (data?.Results != null)
            {
                Task.WhenAll(data.Results.Select(lineMessage =>
                {
                    if (lineMessage.Content != null)
                    {
                        log.WriteLine("Content: " + string.Join(Environment.NewLine,
                                                                lineMessage.Content.Select(x => $"{x.Key}={x.Value}")));
                    }

                    log.WriteLine("ContentType: " + lineMessage.ContentType);
                    switch (lineMessage.ContentType)
                    {
                    case ContentType.Text:
                        if (lineMessage.TextContent != null)
                        {
                            log.WriteLine("text: " + lineMessage.TextContent.Text);

                            var sendingMessage = new SendingMessage();

                            sendingMessage.AddTo(lineMessage.TextContent.From);

                            //TODO: Use Sending multiple messages, LINE response is {"statusCode":"422","statusMessage":"contentType is not valid : 0"}
                            var sendingMessageApi = new SendMessageApi(log);
                            var result            = new GitHubSearchApi(log).Search(lineMessage.TextContent.Text).Result;
                            if (result == null || !result.Any())
                            {
                                sendingMessage.SetSingleContent(new SendingTextContent("There is no content."));
                                return(Task.WhenAll(sendingMessageApi.Post(sendingMessage)));
                            }
                            return(Task.WhenAll(result.Where(x => !string.IsNullOrEmpty(x))
                                                .Select(s =>
                            {
                                if (s.Length > 1024)
                                {
                                    s = s.Substring(0, 1024);
                                }

                                sendingMessage.SetSingleContent(new SendingTextContent(s));
                                return sendingMessageApi.Post(sendingMessage);
                            })));
                        }
                        break;

                    default:
                        log.WriteLine("Not implemented contentType: " + lineMessage.ContentType);
                        break;
                    }

                    return(Task.FromResult((SendingMessageResponse[])null));
                })).Wait();
            }
        }
Пример #2
0
        public static void ProcessQueueMessage([QueueTrigger("line-bot-workitems")] string message, TextWriter log)
        {
            log.WriteLine(message);

            var data = JsonConvert.DeserializeObject <LineMessageObject>(message);

            if (data?.Results != null)
            {
                Task.WhenAll(data.Results.Select(lineMessage =>
                {
                    if (lineMessage.Content != null)
                    {
                        log.WriteLine("Content: " + string.Join(Environment.NewLine,
                                                                lineMessage.Content.Select(x => $"{x.Key}={x.Value}")));
                    }

                    log.WriteLine("ContentType: " + lineMessage.ContentType);
                    switch (lineMessage.ContentType)
                    {
                    case ContentType.Text:
                        if (lineMessage.TextContent != null)
                        {
                            log.WriteLine("text: " + lineMessage.TextContent.Text);

                            var sendingMessage = new SendingMessage();

                            sendingMessage.AddTo(lineMessage.TextContent.From);

                            var translateApi = new TranslateApi();
                            var translated   = translateApi.Translate(lineMessage.TextContent.Text);
                            if (string.IsNullOrEmpty(translated))
                            {
                                throw new ApplicationException("reply message is empty");
                            }

                            sendingMessage.SetSingleContent(new SendingTextContent(translated));
                            return(new SendMessageApi(log).Post(sendingMessage));
                        }
                        break;

                    default:
                        log.WriteLine("Not implemented contentType: " + lineMessage.ContentType);
                        break;
                    }

                    return(Task.FromResult((SendingMessageResponse)null));
                })).Wait();
            }
        }