如何在C#中的Telegram机器人上处理多个用户? [英] How to handle multiple users on Telegram bot in C#?

查看:247
本文介绍了如何在C#中的Telegram机器人上处理多个用户?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I wrote a bot which asks your name and writes it on a photo and sends it to you and it works. But the problem is when there is more than one user on the bot
It doesn't work and crashes and I wanted to know how to separate users entries and outputs.(like each user who connects get a separated session because now everything happens in one session and it crashes) This is my code:

    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;

                }
            }
        }
    }
}

推荐答案

您正在为每个用户编写(然后读取)完全相同的文件

You are writing (and subsequently reading from) the very same file for each user:

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

您需要为每个用户提供一个唯一文件名.或者更好的是,根本不使用文件.您可能只需要使用本地内存流,而不会使磁盘上的文件杂乱无章.

You need to have a unique filename for each user. Or better yet, don't use a file at all. You could probably just use a local memory stream without cluttering your disk with files at all.

这篇关于如何在C#中的Telegram机器人上处理多个用户?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆