如何在 C# 中处理 Telegram 机器人上的多个用户?

2023-11-29

我写了一个机器人,它会询问你的名字并将其写在照片上然后发送给你,它可以工作。但问题是当机器人上有多个用户时
它不起作用并且崩溃,我想知道如何分离用户条目和输出。(就像每个连接的用户都会获得一个单独的会话,因为现在所有事情都发生在一个会话中并且崩溃) 这是我的代码:

    void bot_OnMessage(object sender, Telegram.Bot.Args.MessageEventArgs e)
    {
     KeyboardButton[] btns = new KeyboardButton[1];
     btns[0] = new KeyboardButton("ساخت عکس");
        if(e.Message.Text=="ساخت عکس")
        {
            bot.SendTextMessageAsync(e.Message.From.Id, "نام خود را وارد کنید", Telegram.Bot.Types.Enums.ParseMode.Default, false, false, 0);
           // e.Message.Text = null;
            shart = 1;

        }
        else
        {
            if (shart == 0)
            {
                Telegram.Bot.Types.ReplyMarkups.ReplyKeyboardMarkup markup = new Telegram.Bot.Types.ReplyMarkups.ReplyKeyboardMarkup(btns);

                bot.SendTextMessageAsync(e.Message.From.Id, "برای شروع و ساخت عکس روی دکمه ساخت عکس کلید کنید", Telegram.Bot.Types.Enums.ParseMode.Default, false, false, 0, markup);
            }
            if (shart==1)
            {
                bot.StartReceiving();
                bot.OnMessage += bot_OnMessage1;
            }
        }
    }

    void bot_OnMessage1(object sender, Telegram.Bot.Args.MessageEventArgs a)
    {
        string watermarkText = a.Message.Text;

        //Get the file name.
        string fileName = "C:\\temp\\01.jpg";

        //Read the File into a Bitmap.
        using (Bitmap bmp = new Bitmap(fileName))
        {
            using (Graphics grp = Graphics.FromImage(bmp))
            {
                //Set the Color of the Watermark text.
                Brush brush = new SolidBrush(Color.White);

                //Set the Font and its size.
                Font font = new System.Drawing.Font("Arial", 50, FontStyle.Bold, GraphicsUnit.Pixel);

                //Determine the size of the Watermark text.
                SizeF textSize = new SizeF();
                textSize = grp.MeasureString(watermarkText, font);

                //Position the text and draw it on the image.
                Point position = new Point((bmp.Width - ((int)textSize.Width + 10)), (bmp.Height - ((int)textSize.Height + 10)));
                grp.DrawString(watermarkText, font, brush, position);
                bmp.Save("c:\\temp\\postpic.jpg", ImageFormat.Png);

                using (FileStream fs = new FileStream("c:\\temp\\postpic.jpg", FileMode.Open))
                {

                    fs.CanTimeout.ToString();
                    FileToSend fileToSend = new FileToSend("postpic.jpg", fs);
                    //  var =   FileToSend fts = new FileToSend("postpic", fs);
                    var rep = bot.SendPhotoAsync(a.Message.From.Id, fileToSend, "این عکس را پست کنید").Result;

                }
            }
        }
    }
}

您正在写入(并随后读取)同一个文件对于每个用户:

mp.Save("c:\\temp\\postpic.jpg"

你需要有一个unique每个用户的文件名。或者更好的是,根本不使用文件。您可能只使用本地内存流,而不会用文件弄乱磁盘。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何在 C# 中处理 Telegram 机器人上的多个用户? 的相关文章

随机推荐