Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
511 views
in Technique[技术] by (71.8m points)

asp.net core - The server does not respond to bot requests

I created a server and decided to add a bot to it. First of all I added a class

public static class AppSettings
    {
        public static string Url { get; set; } = @"https://47bc6f8ba722.ngrok.io/{0}";
        public static string Name { get; set; } = "Hop_hipBot";
        public static string Key { get; set; } = "1549046386:AAHJtCMVaCT-8O3D_P8VLsfq6EKAr4P9JfSU";
    }

Then I created a command and a bot.

public class Bot
    {
        private static TelegramBotClient botClient;
        private static List<Command> commandsList;

        public static IReadOnlyList<Command> Commands => commandsList.AsReadOnly();

        public static async Task<TelegramBotClient> GetBotClientAsync()
        {
            if (botClient != null)
            {
                return botClient;
            }

            commandsList = new List<Command>();
            commandsList.Add(new StartCommand());
            //TODO: Add more commands

            botClient = new TelegramBotClient(AppSettings.Key);
            string hook = string.Format(AppSettings.Url, @"api/message/update");
            await botClient.SetWebhookAsync(hook);
            return botClient;
        }
    }
    public class StartCommand : Command
        {
            public override string Name => @"/start";
    
            public override bool Contains(Message message)
             {
                 if (message.Type != Telegram.Bot.Types.Enums.MessageType.Text)
                     return false;
            
                 return message.Text.Contains(Name);
            }
    
            public override async Task Execute(Message message, TelegramBotClient botClient)
            {
                var chatId = message.Chat.Id;
                await botClient.SendTextMessageAsync(chatId, "Hello I'm ASP.NET Core Bot");
            }
        }

Then I connected everything with the controller and add Bot.GetBotClientAsync().Wait(); in Configure() in Startup.cs

    [Route("api/message/update")]
        public class MessageController : ControllerBase
        {
            // GET api/values
            [HttpGet]
            public string Get()
            {
                return "Method GET unuvalable";
            }
    
            [HttpPost]
            public async Task<OkResult> Post([FromBody] Update update)
            {
                if (update == null) return Ok();
                var commands = Bot.Commands;
                var message = update.Message;
                var botClient = await Bot.GetBotClientAsync();
                foreach (var command in commands)
                {
                    if (command.Contains(message))
                    {
                        await command.Execute(message, botClient);
                        break;
                    }
                }
                return Ok();
            }
        }

I start the server on port 44339 and with the help of ngrox.exe http 44339 I make a url for localhost, then I start everything. The application works, the get request along the api/message/update route also works, messages from the bot come(I see it on ngrok inspect), but the server does not respond to them. What can this be related to?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
等待大神答复

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...