保存发送给机器人的用户消息并将完成的表单发送给其他用户

几天来,我一直试图用我的 Telegram Bot 解决这个问题。


我试图在用户“/开始”成为机器人后向他发送一些问题。


我想捕获所有用户答案,然后将其发送给我希望在一条消息中查看用户答案的某个用户。


我试图通过发送内联按钮来实现,但找不到等待用户下一条消息的方法。我试图将答案存储在一个字符串数组中,但它也不起作用。


在问题的最后,我想将所有用户的答案发送到某个用户 ID/频道,并在一条消息中包含所有用户问题。


我使用 Telegram.Bot 库。


这是我的代码


static string gotName = "0";

static string gotAge = "0";

static string gotMessage = "0";


static string[] Forminfo = { gotName, gotAge, gotMessage };



 async  private void Bot_OnUpdate(object sender, Telegram.Bot.Args.UpdateEventArgs e)

{

    if (e.Update.Type == UpdateType.Message && e.Update.Message.Text == "/start")

    {

        var streg = new InlineKeyboardMarkup(new[]

        {

            new [] // first row

            {

                InlineKeyboardButton.WithCallbackData("Next Step","start")

            }

        });


        var update = e.Update.Message.Text;


        var strmsg = "To Start The Register please send the bot your name and click on Next Step";

        await bot.SendTextMessageAsync(e.Update.Message.Chat.Id, strmsg, ParseMode.Html, replyMarkup: streg);

        var usermsg = await bot.GetUpdatesAsync();

        Forminfo[0] = usermsg.ToString();

    }

}



async private void Bot_OnCallbackQuery(object sender, Telegram.Bot.Args.CallbackQueryEventArgs e)

{            

    var streg1 = new InlineKeyboardMarkup(new[]

    {

        new [] // first row

        {

            InlineKeyboardButton.WithCallbackData("Next","start1")

        }

    });


    if (Forminfo[0] != "0")

    {

        var startedmsg = "Hello " + Forminfo[0].ToString() + "\n" +

                "Please Send us your Age and click Next";

        try

        {

            await bot.SendTextMessageAsync(e.CallbackQuery.Message.Chat.Id, startedmsg, ParseMode.Html, replyMarkup: streg1);

        }

        catch(HttpRequestException ex)

        {

            await bot.SendTextMessageAsync(e.CallbackQuery.Message.Chat.Id, "To Many Request Please Try Later.", ParseMode.Html);

        }

    }

}


子衿沉夜
浏览 189回答 1
1回答

守着星空守着你

有几件事需要解决:如果您在OnUpdate回调中处理的所有消息都是消息,请OnMessage改用您正在使用OnUpdate然后手动调用GetUpdates.&nbsp;您不能混合使用多种获取更新的方法 -StartReceiving调用已经在GetUpdates内部处理调用。通过将一个 string[] 作为结果,您假设只有一个用户将同时使用该机器人。更好的方法是使用Dictionary<userId, result>.在SendTextMessageAsync通话中,您无需设置ParseMode是否发送常规文本如果您从不检查用户是否点击了按钮,我就看不到您使用这些按钮的目的这是一个代码示例,可以执行您想要的操作,但根本不验证输入:const long TargetChannelId = 123456;static readonly ConcurrentDictionary<int, string[]> Answers = new ConcurrentDictionary<int, string[]>();private static async void Bot_OnMessage(object sender, MessageEventArgs e){&nbsp; &nbsp; Message message = e.Message;&nbsp; &nbsp; int userId = message.From.Id;&nbsp; &nbsp; if (message.Type == MessageType.Text)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; if (Answers.TryGetValue(userId, out string[] answers))&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (answers[0] == null)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; answers[0] = message.Text;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; await Bot.SendTextMessageAsync(message.Chat, "Now send me your age");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; else if (answers[1] == null)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; answers[1] = message.Text;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; await Bot.SendTextMessageAsync(message.Chat, "Now send me your message");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; else&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Answers.TryRemove(userId, out string[] _);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; await Bot.SendTextMessageAsync(message.Chat, "Thank you, that's all I need from you");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; string answersText = $"User {answers[0]}, aged {answers[1]} sent the following message:\n{message.Text}";&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; await Bot.SendTextMessageAsync(TargetChannelId, answersText);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; else&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Answers.TryAdd(userId, new string[2]);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; await Bot.SendTextMessageAsync(message.Chat, "Please send me your name");&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }}
打开App,查看更多内容
随时随地看视频慕课网APP