あなたの名前を尋ねて写真に書いてあなたに送ってくれます。しかし、問題は、ボットに複数のユーザーがいる場合です。
これは動作しなくなり、ユーザーの入力と出力を分ける方法を知りたがっています(接続する各ユーザーのように、 1つのセッションで、それがクラッシュする) これは私のコードです:C#のTelegramボットで複数のユーザーを処理する方法は?
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;
}
}
}
}
}
に債務レコードの作成時に使用されますか? –
出力イメージファイルを書き込むときは、ハードコードされたファイル名 "c:\\ temp \\ postpic.jpg"を使用しているようです。これにより、複数のユーザーがアクセスする際に問題が発生します。代わりに、ユーザー名とタイムスタンプのようなものを使用してoutファイル名を生成します。fileName = string.Format( "c:\\ temp \\ postpic_ {0} .jpg"、DateTime.Now.ToString( "ddMMyyyy-HHmmss")); –