gpt4 book ai didi

c# - 如何在 C# 中处理 Telegram bot 上的多个用户?

转载 作者:太空狗 更新时间:2023-10-30 00:48:23 25 4
gpt4 key购买 nike

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

    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"

您需要为每个用户提供一个唯一 文件名。或者更好的是,根本不使用文件。您可能只使用本地内存流,而根本不会让文件堆满磁盘。

关于c# - 如何在 C# 中处理 Telegram bot 上的多个用户?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46908506/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com