保存发送给机器人的用户消息并将完成的表单发送给其他用户 [英] Save user messages sent to bot and send finished form to other user
问题描述
这几天我一直在尝试用我的 Telegram Bot 解决这个问题.
For days I've been trying to solve this problem with my Telegram Bot.
我试图在用户/start"成为机器人后向他发送一些问题.
I'm trying to send the user some questions after he "/start"s the bot.
我想捕获所有用户的答案,然后将其发送给我希望在一条消息中查看用户答案的某个用户.
I want to capture all user answers and then send it to some user that I want to see user answers in one message.
我尝试通过发送内嵌按钮来实现,但找不到等待用户下一条消息的方法.我试图将答案存储在一个字符串数组中,但它也不起作用.
I tried to do it by sending inline buttons and couldn't find the way to wait for the next message from the user. I tried to store the answers in a string array, and it doesn't work either.
在问题的最后,我想在包含所有用户问题的一条消息中将所有用户的答案发送到某个用户 ID/频道.
And at the end of the question, I want to send all user answers to some userid/channel in one message with all user questions.
我使用 Telegram.Bot 库.
I use Telegram.Bot library.
这是我的代码
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);
}
}
}
推荐答案
有几个问题需要解决:
- 如果您在
OnUpdate
回调中处理的都是消息,请改用OnMessage
- 您正在使用
OnUpdate
,然后手动调用GetUpdates
.您不能混合使用多种获取更新的方法 -StartReceiving
调用已经在内部处理了对GetUpdates
的调用. - 通过将一个 string[] 作为结果,您假设只有一个用户将同时使用该机器人.更好的方法是使用
Dictionary
. - 在您的
SendTextMessageAsync
调用中,如果您要发送常规文本,则不必设置ParseMode
- 如果您从不检查用户是否点击了按钮,我就看不到您使用这些按钮的目的
- If all you are handling inside your
OnUpdate
callback are messages, useOnMessage
instead - You are using
OnUpdate
and then manually callGetUpdates
. You can't mix multiple approaches of getting updates - theStartReceiving
call already handles callingGetUpdates
internally. - By having one string[] as the result, you are assuming only one user will use the bot at the same time. A better approach would be to use a
Dictionary<userId, result>
. - In your
SendTextMessageAsync
call, you don't have to setParseMode
if you are sending regular text - I don't see what you are using the buttons for if you're never checking whether the user clicked them
这是一个代码示例,可以执行您想要的操作,但根本不验证输入:
This is a code example that does what you want, but does not validate the input at all:
const long TargetChannelId = 123456;
static readonly ConcurrentDictionary<int, string[]> Answers = new ConcurrentDictionary<int, string[]>();
private static async void Bot_OnMessage(object sender, MessageEventArgs e)
{
Message message = e.Message;
int userId = message.From.Id;
if (message.Type == MessageType.Text)
{
if (Answers.TryGetValue(userId, out string[] answers))
{
if (answers[0] == null)
{
answers[0] = message.Text;
await Bot.SendTextMessageAsync(message.Chat, "Now send me your age");
}
else if (answers[1] == null)
{
answers[1] = message.Text;
await Bot.SendTextMessageAsync(message.Chat, "Now send me your message");
}
else
{
Answers.TryRemove(userId, out string[] _);
await Bot.SendTextMessageAsync(message.Chat, "Thank you, that's all I need from you");
string answersText = $"User {answers[0]}, aged {answers[1]} sent the following message:\n{message.Text}";
await Bot.SendTextMessageAsync(TargetChannelId, answersText);
}
}
else
{
Answers.TryAdd(userId, new string[2]);
await Bot.SendTextMessageAsync(message.Chat, "Please send me your name");
}
}
}
这篇关于保存发送给机器人的用户消息并将完成的表单发送给其他用户的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!