问题描述
我正试图使我的电报机器人向用户发送动画文本,由于每次编辑文本后我的Thread.Sleep(500);
,这需要一些时间。
例如,用户发送:/ animtext hamidrezas120
机器人将以'h'响应,然后编辑为'ha'和...,直到全文显示。
而我想要的是当用户将该命令发送到bot时,而bot正在执行该操作时,如果用户再次发送该命令,我希望我的bot告诉他“动画文本处理尚未完成!请等待...”。
但是我得到的是: 用户发送命令后,机器人将开始为文本添加动画效果,而机器人正在这样做时,如果再次发送命令,机器人仍会完成动画文本,然后自动发送另一个动画文本。
这是我的代码:
OnMessage事件(收到消息时):
public async void botClient_OnMessage(object sender,MessageEventArgs e)
{
if (e.Message.Text != null && e.Message.Text.StartsWith("/animtext"))
{
if (e.Message.Text == "/animtext"")
{
botClient.SendTextMessageAsync(e.Message.Chat.Id,"Usage:\n/animtext <Some Text>");
}
else
{
if (isAlreadyStarted_animText_Process == false)
{
Application.DoEvents();
ChatID = e.Message.Chat.Id.ToString();
animText_Input = e.Message.Text.Substring(10);
DoThe_animtext();
}
else if (isAlreadyStarted_animText_Process == true) //here will not execute because event is focusing and doing the for loop that is in the function
{
botClient.SendTextMessageAsync(e.Message.Chat.Id,"❌ animtext Process is Not Finished Yet! Please Wait...",replyToMessageId: e.Message.MessageId);
Application.DoEvents();
}
}
}
}
以及执行动画文本的功能:
private async Task DoThe_animtext()
{
isAlreadyStarted_animText_Process = true;
var Temp_Starting_animText_Result = botClient.SendTextMessageAsync(ChatID,"Starting...");
Starting_animText_MessageID = Temp_Starting_animText_Result.Result.MessageId;
Thread.Sleep(1000);
botClient.DeleteMessageAsync(ChatID,Starting_animText_MessageID);
var Temp_FirstLetterSending_animText_Result = botClient.SendTextMessageAsync(ChatID,animText_Input[0].ToString());
FirstLetterSending_animText_MessageID = Temp_FirstLetterSending_animText_Result.Result.MessageId;
for (int i = 0; i < animText_Input.Length; i++)
{
EditingText_animText += animText_Input[i];
botClient.EditMessageTextAsync(ChatID,FirstLetterSending_animText_MessageID,EditingText_animText);
Thread.Sleep(500);
if (i == animText_Input.Length - 1)
{
botClient.EditMessageTextAsync(ChatID,"✅ " + EditingText_animText);
EditingText_animText = null;
break;
}
}
isAlreadyStarted_animText_Process = false;
}
我的问题是OnMessage事件将不会执行以下代码部分:
else if (isAlreadyStarted_animText_Process == true)
{
botClient.SendTextMessageAsync(e.Message.Chat.Id,replyToMessageId: e.Message.MessageId);
Application.DoEvents();
}
它会等到过程完成后再发送另一个动画文本(因为在机器人执行动画操作时,用户再次发送了命令)
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)