该容器包含一个电报机器人。它可以在计算机上运行,但不能在服务器上运行。它只是关闭。在容器写入的状态中Exited (0) 22 seconds ago
docker ps显示什么。
程序执行达到
async Task HandleUpdateAsync(ITelegramBotClient botClient, Update update, CancellationToken cancellationToken)
从示例 https://telegrambots.github.io/book/1/example-bot.html
using Telegram.Bot;
using Telegram.Bot.Exceptions;
using Telegram.Bot.Extensions.Polling;
using Telegram.Bot.Types;
using Telegram.Bot.Types.Enums;
var botClient = new TelegramBotClient("{YOUR_ACCESS_TOKEN_HERE}");
using var cts = new CancellationTokenSource();
// StartReceiving does not block the caller thread. Receiving is done on the ThreadPool.
var receiverOptions = new ReceiverOptions
{
AllowedUpdates = { } // receive all update types
};
botClient.StartReceiving(
HandleUpdateAsync,
HandleErrorAsync,
receiverOptions,
cancellationToken: cts.Token);
var me = await botClient.GetMeAsync();
Console.WriteLine($"Start listening for @{me.Username}");
Console.ReadLine();
// Send cancellation request to stop bot
cts.Cancel();
async Task HandleUpdateAsync(ITelegramBotClient botClient, Update update, CancellationToken cancellationToken)
{
// Only process Message updates: https://core.telegram.org/bots/api#message
if (update.Type != UpdateType.Message)
return;
// Only process text messages
if (update.Message!.Type != MessageType.Text)
return;
var chatId = update.Message.Chat.Id;
var messageText = update.Message.Text;
Console.WriteLine($"Received a '{messageText}' message in chat {chatId}.");
// Echo received message text
Message sentMessage = await botClient.SendTextMessageAsync(
chatId: chatId,
text: "You said:\n" + messageText,
cancellationToken: cancellationToken);
}
Task HandleErrorAsync(ITelegramBotClient botClient, Exception exception, CancellationToken cancellationToken)
{
var ErrorMessage = exception switch
{
ApiRequestException apiRequestException
=> $"Telegram API Error:\n[{apiRequestException.ErrorCode}]\n{apiRequestException.Message}",
_ => exception.ToString()
};
Console.WriteLine(ErrorMessage);
return Task.CompletedTask;
}
我立即在那里有 Console.WriteLine 并且它不再显示它。我有消息挂在机器人中,因此在启动后它应该立即接收并响应,但这不会发生。尽管在容器中的 PC 上一切正常。
这可能是什么原因?
似乎没有在docker
Console.ReadLine()中暂停代码。创建一个期望而不是
Console.ReadLine()例如。添加方法
并在方法中
HandleUpdateAsync写入,例如,当您收到/stop自己的命令时,即不要让机器人被任何用户停止,而只能由您自己停止。