我设置了将照片发送到电报的代码,首先是在我的localhost上工作,在将telegram.bot软件包更新到ver 9.0.0并发布到服务器后,在本地主机和服务器上工作。
我使用try / catch发送Text而不是照片,现在它们无法正常工作,这意味着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);
}
}
答案 0 :(得分:0)
用流阅读图片 示例
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();