Ejemplo n.º 1
0
        public static async Task ProcessQueueMessage([QueueTrigger("line-bot-workitems")] string message, TextWriter log)
        {
            log.WriteLine(message);

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

            if (data?.Events != null)
            {
                foreach (var webhookEvent in data.Events)
                {
                    log.WriteLine("event type: " + webhookEvent.Type);
                    switch (webhookEvent.Type)
                    {
                    case WebhookRequestEventType.Message:
                        if (webhookEvent.Message.Type == MessageType.Text)
                        {
                            log.WriteLine("text: " + webhookEvent.Message.Text);

                            if (tokenResponse == null || tokenResponse.ExpiresIn < DateTime.Now.AddDays(-1))
                            {
                                tokenResponse = await oAuthClient.GetAccessToken();
                            }

                            var client = new LineMessagingClient(tokenResponse.AccessToken);

                            var result = await new GitHubSearchApi(log).Search(webhookEvent.Message.Text);
                            if (result == null || !result.Any())
                            {
                                await client.PushMessage(webhookEvent.Source.UserId, "There is no content.");

                                return;
                            }

                            foreach (var s in result.Where(x => !string.IsNullOrEmpty(x)))
                            {
                                if (s.Length > 1024)
                                {
                                    await client.PushMessage(webhookEvent.Source.UserId, s.Substring(0, 1024));
                                }
                                else
                                {
                                    await client.PushMessage(webhookEvent.Source.UserId, s);
                                }
                            }
                        }
                        break;

                    default:
                        log.WriteLine("Not implemented event type: " + webhookEvent.Type);
                        break;
                    }
                }
            }
        }
Ejemplo n.º 2
0
        public static async Task ProcessQueueMessage([QueueTrigger("line-bot-workitems")] string message, TextWriter log)
        {
            log.WriteLine(message);
            LineWebhookContent data;

            try
            {
                data = JsonConvert.DeserializeObject <LineWebhookContent>(message);
            }
            catch (Exception ex)
            {
                log.WriteLine("Ignore deserialization error: " + ex.ToString());
                return;
            }

            if (data?.Events != null)
            {
                foreach (var webhookEvent in data.Events)
                {
                    log.WriteLine("event type: " + webhookEvent.Type);
                    switch (webhookEvent.Type)
                    {
                    case WebhookRequestEventType.Message:
                        if (webhookEvent.Message.Type == MessageType.Text)
                        {
                            log.WriteLine("text: " + webhookEvent.Message.Text);

                            if (tokenResponse == null || tokenResponse.ExpiresIn < DateTime.Now.AddDays(-1))
                            {
                                tokenResponse = await oAuthClient.GetAccessToken();
                            }

                            var client = new LineMessagingClient(tokenResponse.AccessToken);
                            await client.PushMessage(webhookEvent.Source.UserId, webhookEvent.Message.Text);
                        }
                        break;

                    default:
                        log.WriteLine("Not implemented event type: " + webhookEvent.Type);
                        break;
                    }
                }
            }
        }
Ejemplo n.º 3
0
        public static async Task ProcessQueueMessage([QueueTrigger("line-bot-workitems")] string message, TextWriter log)
        {
            log.WriteLine(message);

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

            if (data?.Events != null)
            {
                foreach (var webhookEvent in data.Events)
                {
                    log.WriteLine("event type: " + webhookEvent.Type);
                    switch (webhookEvent.Type)
                    {
                    case WebhookRequestEventType.Message:
                        if (webhookEvent.Message.Type == MessageType.Text)
                        {
                            log.WriteLine("text: " + webhookEvent.Message.Text);

                            if (tokenResponse == null || tokenResponse.ExpiresIn < DateTime.Now.AddDays(-1))
                            {
                                tokenResponse = await oAuthClient.GetAccessToken();
                            }

                            var client = new LineMessagingClient(tokenResponse.AccessToken);

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

                            await client.PushMessage(webhookEvent.Source.UserId, translated);
                        }
                        break;

                    default:
                        log.WriteLine("Not implemented event type: " + webhookEvent.Type);
                        break;
                    }
                }
            }
        }
Ejemplo n.º 4
0
 public async Task PushMessageTest()
 {
     await apiClient.PushMessage(userId1, "PushMessageTest");
 }