Telegram Bot ASP.NET 中的 Telegram SendPhoto() 方法
Telegram SendPhoto() Methode in ASP.NET by Telegram Bot
我设置了将照片发送到电报的代码,首先是在我的本地主机上工作,在将 telegram.bot 包更新到版本 9.0.0 并发布到服务器后,在本地主机和服务器上不起作用。
我使用 try/catch 发送文本而不是照片,现在它们不起作用,这意味着 Try 块正在工作但它不能生效。
if (offerListDetail != null)
{
StringBuilder botTextA2 = new StringBuilder();
StringBuilder botTextB2 = new StringBuilder();
string remoteImgPath = offerListDetail.OFL_OfferImageUrl;
Uri remoteImgPathUri = new Uri(remoteImgPath);
string remoteImgPathWithoutQuery = remoteImgPathUri.GetLeftPart(UriPartial.Path);
string fileName = Path.GetFileName(remoteImgPathWithoutQuery);
string localPath = Server.MapPath("~/Images/telegram/"); //AppDomain.CurrentDomain.BaseDirectory + "Images/telegram/" + fileName;
WebClient webClient = new WebClient();
webClient.DownloadFile(remoteImgPath, localPath + fileName);
var botphoto = new FileToSend()
{
Content = OpenFile(localPath + fileName),
Filename = fileName
};
//var botClient = new Telegram.Bot.Api("157612108:AAFr4y7WWT32xX41EMOVkmEW19pIgcHImv4"); // استانبولیار
var botClient = new Telegram.Bot.Api("186221188:AAHrihjOH7__4vlF0DCNWLEzYQ3p3ORO0_k"); // ربات ری را
try
{
botTextA2.AppendLine(" http://order.reera.ir/offers.aspx?offer=" + offerListDetail.OFL_ID);
botTextA2.AppendLine(" " + offerListDetail.OFL_OfferName);
botTextA2.AppendLine(" " + offerListDetail.brn_Name);
botTextA2.AppendLine(" مهلت " + offerListDetail.remainday + " روز");
botTextA2.AppendLine(" سفارش در http://order.reera.ir");
botTextA2.AppendLine(" یا تلگرام @reerabrand");
string botTextA = botTextA2.ToString().Replace(Environment.NewLine, "\n");
botClient.SendPhoto("@istanbulyar", botphoto, "ddd");//botTextA);
botClient.SendPhoto("@reera", botphoto, "ddd");//botTextA);
}
catch
{
botTextB2.AppendLine(offerListDetail.OFL_OfferImageUrl);
botTextB2.AppendLine("*********************************");
botTextB2.AppendLine("<b> حراجی " + offerListDetail.OFL_OfferName + "</b> ");
botTextB2.AppendLine("<i> توسط وبسایت " + offerListDetail.brn_Name + "</i> ");
botTextB2.AppendLine(" <b>مهلت خرید تا " + offerListDetail.remainday + " روز دیگر</b> ");
botTextB2.AppendLine(" <a href='http://order.reera.ir/offers.aspx?offer=" + offerListDetail.OFL_ID + "'> مشاهده بوتیک </a> ");
botTextB2.AppendLine("");
botTextB2.AppendLine(" سفارش در http://order.reera.ir");
botTextB2.AppendLine(" یا تلگرام @reerabrand");
string botTextB = botTextB2.ToString().Replace(Environment.NewLine, "\n");
botClient.SendTextMessage("@istanbulyar", botTextB, parseMode: ParseMode.Html);
botClient.SendTextMessage("@reera", botTextB, disableNotification: true, parseMode: ParseMode.Html);
}
}
用流读取图片
例子
string pic = "نام عکس مورد نظر";
string yourpath = Environment.CurrentDirectory + @"\Pic\"+pic;
FileStream stream = new FileStream(yourpath, FileMode.Open, FileAccess.Read);
FileToSend fs = new FileToSend("photo3.jpg",stream);
bot.MakeRequestAsync(new SendPhoto(update.Message.Chat.Id, fs)).Wait();
我设置了将照片发送到电报的代码,首先是在我的本地主机上工作,在将 telegram.bot 包更新到版本 9.0.0 并发布到服务器后,在本地主机和服务器上不起作用。
我使用 try/catch 发送文本而不是照片,现在它们不起作用,这意味着 Try 块正在工作但它不能生效。
if (offerListDetail != null)
{
StringBuilder botTextA2 = new StringBuilder();
StringBuilder botTextB2 = new StringBuilder();
string remoteImgPath = offerListDetail.OFL_OfferImageUrl;
Uri remoteImgPathUri = new Uri(remoteImgPath);
string remoteImgPathWithoutQuery = remoteImgPathUri.GetLeftPart(UriPartial.Path);
string fileName = Path.GetFileName(remoteImgPathWithoutQuery);
string localPath = Server.MapPath("~/Images/telegram/"); //AppDomain.CurrentDomain.BaseDirectory + "Images/telegram/" + fileName;
WebClient webClient = new WebClient();
webClient.DownloadFile(remoteImgPath, localPath + fileName);
var botphoto = new FileToSend()
{
Content = OpenFile(localPath + fileName),
Filename = fileName
};
//var botClient = new Telegram.Bot.Api("157612108:AAFr4y7WWT32xX41EMOVkmEW19pIgcHImv4"); // استانبولیار
var botClient = new Telegram.Bot.Api("186221188:AAHrihjOH7__4vlF0DCNWLEzYQ3p3ORO0_k"); // ربات ری را
try
{
botTextA2.AppendLine(" http://order.reera.ir/offers.aspx?offer=" + offerListDetail.OFL_ID);
botTextA2.AppendLine(" " + offerListDetail.OFL_OfferName);
botTextA2.AppendLine(" " + offerListDetail.brn_Name);
botTextA2.AppendLine(" مهلت " + offerListDetail.remainday + " روز");
botTextA2.AppendLine(" سفارش در http://order.reera.ir");
botTextA2.AppendLine(" یا تلگرام @reerabrand");
string botTextA = botTextA2.ToString().Replace(Environment.NewLine, "\n");
botClient.SendPhoto("@istanbulyar", botphoto, "ddd");//botTextA);
botClient.SendPhoto("@reera", botphoto, "ddd");//botTextA);
}
catch
{
botTextB2.AppendLine(offerListDetail.OFL_OfferImageUrl);
botTextB2.AppendLine("*********************************");
botTextB2.AppendLine("<b> حراجی " + offerListDetail.OFL_OfferName + "</b> ");
botTextB2.AppendLine("<i> توسط وبسایت " + offerListDetail.brn_Name + "</i> ");
botTextB2.AppendLine(" <b>مهلت خرید تا " + offerListDetail.remainday + " روز دیگر</b> ");
botTextB2.AppendLine(" <a href='http://order.reera.ir/offers.aspx?offer=" + offerListDetail.OFL_ID + "'> مشاهده بوتیک </a> ");
botTextB2.AppendLine("");
botTextB2.AppendLine(" سفارش در http://order.reera.ir");
botTextB2.AppendLine(" یا تلگرام @reerabrand");
string botTextB = botTextB2.ToString().Replace(Environment.NewLine, "\n");
botClient.SendTextMessage("@istanbulyar", botTextB, parseMode: ParseMode.Html);
botClient.SendTextMessage("@reera", botTextB, disableNotification: true, parseMode: ParseMode.Html);
}
}
用流读取图片 例子
string pic = "نام عکس مورد نظر";
string yourpath = Environment.CurrentDirectory + @"\Pic\"+pic;
FileStream stream = new FileStream(yourpath, FileMode.Open, FileAccess.Read);
FileToSend fs = new FileToSend("photo3.jpg",stream);
bot.MakeRequestAsync(new SendPhoto(update.Message.Chat.Id, fs)).Wait();