Пример #1
0
        public override async Task <MessageResponse> HandleMessage(MessageRequest messageRequest, ServerCallContext context)
        {
            _logger.LogInformation("Begin grpc call TelegramService.HandleMessage");
            string msg          = "";
            int    responseCode = 200;

            var update = messageRequest.Update;

            var sendMessageObject = new models.SendMessage
            {
                ChatId = update.Message.Chat.Id,
            };

            if (update.Message.Entities == null ||
                update.Message.Entities.Count != 1 ||
                update.Message.Entities[0].Type != "bot_command")
            {
                sendMessageObject.Text = "ERROR. I can only handle single bot command.";
                responseCode           = (int)System.Net.HttpStatusCode.Accepted;
            }
            else
            {
                var command = update.Message.Text.Substring(
                    update.Message.Entities[0].Offset,
                    update.Message.Entities[0].Length
                    );
                msg = await _service.GenerateResponseTextAsync(command, messageRequest.BotToken, update.Message.Chat.Id);

                if (string.IsNullOrEmpty(msg))
                {
                    msg = "No commands found";
                }

                sendMessageObject.Text = msg;
                responseCode           = (int)System.Net.HttpStatusCode.OK;
            }

            await _service.SendMessageToBotAsync(messageRequest.BotToken, sendMessageObject);

            return(new MessageResponse
            {
                ResponseCode = responseCode
            });
        }
Пример #2
0
        public async Task <ActionResult> HandleMessageAsync(string botToken, [FromBody] Update update)
        {
            ActionResult resultCode    = null;
            string       msg           = "";
            var          messageObject = new SendMessage()
            {
                ChatId = update.Message.Chat.Id,
            };

            if (update.Message.Entities == null ||
                update.Message.Entities.Length != 1 ||
                update.Message.Entities[0].Type != "bot_command")
            {
                messageObject.Text = "ERROR. I can only handle single bot command.";
                resultCode         = Accepted();
            }
            else
            {
                var command = update.Message.Text.Substring(
                    update.Message.Entities[0].Offset,
                    update.Message.Entities[0].Length
                    );
                msg = await _telegramService.GenerateResponseTextAsync(command, botToken, update.Message.Chat.Id);

                if (string.IsNullOrEmpty(msg))
                {
                    msg = "No commands found";
                }

                messageObject.Text = msg;
                resultCode         = Ok();
            }

            await _telegramService.SendMessageToBotAsync(botToken, messageObject);

            return(resultCode);
        }