Exemplo n.º 1
0
        public void Notify(string destination, List <EventItem> events)
        {
            try
            {
                LogNotify(destination, events);
                string[] specificChannelKeywords = new[] { "camera", "cam", "c", "picture", "pic", "p", "channel", "ch", "กล้อง" };

                string[]   interestEventTypes = new string[] { "follow", "message" };
                LineClient client             = new LineClient(_accessToken);
                foreach (var ev in events)
                {
                    if (!interestEventTypes.Contains(ev.type))
                    {
                        continue;
                    }
                    if (ev.source != null && ev.source.type == "user")
                    {
                        if (ev.message != null)
                        {
                            if (ev.message.type == "text")
                            {
                                var validCommand = false;
                                var comandBlocks = ev.message.text.Split(new char[] { ' ' });
                                if (comandBlocks.Length == 2)
                                {
                                    var key       = comandBlocks[0].Trim().ToLower();
                                    var channel   = comandBlocks[1].Trim();
                                    int channelId = 0;
                                    if (specificChannelKeywords.Contains(key) && int.TryParse(channel, out channelId))
                                    {
                                        _logger.Debug(string.Format("process-capture-command>> channel: {0}, mId:{1}", channelId, ev.message.id));
                                        var camera = GetCameraInfo(channelId);
                                        if (camera != null)
                                        {
                                            var imageUrl = GetCameraImage(camera.StillImageUrl, camera.UserName, camera.Password);
                                            if (!string.IsNullOrEmpty(imageUrl))
                                            {
                                                client.AddMessageQueue(new SendMessageItem
                                                {
                                                    type = "image",
                                                    originalContentUrl = imageUrl,
                                                    previewImageUrl    = imageUrl,
                                                });
                                                var replyRes = client.ReplyToUser(ev.replyToken);
                                                validCommand = true;
                                                _logger.Debug("reply-image>>" + imageUrl + ", res:" + replyRes.Success.ToString() + ", msg:" + replyRes.Message);
                                            }
                                        }
                                    }
                                }

                                if (!validCommand)
                                {
                                    client.AddMessageQueue(new SendMessageItem
                                    {
                                        type = "text",
                                        text = "คำสังไม่ถูกต้อง หรือไม่พบช่องที่ระบุ กรุณาลองใหม่อีกครั้ง"
                                    });

                                    var replyRes = client.ReplyToUser(ev.replyToken);
                                    _logger.Debug("reply-invalid message>> " + ev.message.text + ", res:" + replyRes.Success.ToString() + ", msg:" + replyRes.Message);
                                }
                            }
                        }
                        else if (ev.type == "follow")
                        {
                            client.AddMessageQueue(new SendMessageItem
                            {
                                type = "text",
                                text = "ยินดีต้อนรับเข้าสู่ระบบดูภาพนิ่งจากกล้องผ่าน LINE\r\nคุณสามารถพิมพ์คำสั่งเพื่อดูกล้องได้ดังนี้\r\n\r\ncam [เลขที่]\r\ncamera [เลขที่]\r\npic [เลขที่]\r\npicture [เลขที่]"
                            });

                            var replyRes = client.ReplyToUser(ev.replyToken);
                            _logger.Debug("reply-greeting message>> res:" + replyRes.Success.ToString() + ", msg:" + replyRes.Message);
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                _logger.Error("Notify-incoming:" + "des:" + destination + ", error:" + ex.Message);
            }
        }