I want send message with attached image via telegram bot like this:
My code is here but message is nothing:
//var path = "<b>Hello</b>\n"
//+ "";
//+ "<code>and a little bit code</code>\n"
//string path = System.IO.Path.Combine("Image\\2.jpg");
string path = #"D:\2.jpg";
bot.SendTextMessageAsync(chatId, "<a href='" + path + "'>My file</a>", ParseMode.Html, false);
please resolve this.!
If you want your message look exactly like on the screenshot in question, you should send a URL of an image in text parameter of SendTextMessageAsync method (you are now trying to send a file).
However, you can also send your image using SendPhotoAsync method. Just specify you text as a caption parameter and your file as a stream in photo parameter:
Message message;
using (Stream stream = System.IO.File.OpenRead(FILENAME))
{
message = await bot.SendPhotoAsync(
chatId: chatId,
photo: stream,
caption: "test photo caption"
);
}
Related
I'm trying to download a PDF file from blazor sent by my WebAPI as byte[] (byteArray).
I know that my PDF is working because when I send it as filestreamResult and retrieve it directly from my WebAPI (using swagger for example), I get the correct PDF file.
My WebAPI:
var response = await GetResponse(new Query(request.AnalysisDate), ct);
string html = await _templateService.PerformanceReport(response);
MemoryStream stream = _pdfService.GeneratePdf(html);
return await stream.ToArray(); // as byte[]
Blazor Side:
var bytes = await _http.GetByteArrayAsync(endpoint);
await JsRuntime.InvokeVoidAsync("BlazorDownloadFile", "file.pdf", "application/pdf", bytes);
JS:
function BlazorDownloadFile(filename, contentType, content) {
// Create the URL
const file = new File([content], filename, { type: contentType });
const exportUrl = URL.createObjectURL(file);
// Create the <a> element and click on it
const a = document.createElement("a");
document.body.appendChild(a);
a.href = exportUrl;
a.download = filename;
a.target = "_self";
a.click();
// We don't need to keep the object url, let's release the memory
// On Safari it seems you need to comment this line... (please let me know if you know why)
URL.revokeObjectURL(exportUrl);
}
I Correctly download a PDF file named file.pdf but it looks corrupted.
Is this the correct approach with .NET 6?
Should I send a FileStreamResult from the Web API (I Like the fact that I can get the file directly from swagger)?
I can't really find a good example for blazor on how to do this. Any help would be appreciated.
I am using another aproach. I send base64String. I convert byte array to base64 string then i pass the string to javascript function
Server side call:
js.InvokeVoidAsync("jsSaveAsFile",
filename,
Convert.ToBase64String(GetFileByteArrayFunction())
);
javascript function:
function jsSaveAsFile(filename, byteBase64) {
var link = document.createElement('a');
link.download = filename;
link.href = "data:application/octet-stream;base64," + byteBase64;
document.body.appendChild(link);
link.click();
document.body.removeChild(link);}
js -> using Microsoft.JSInterop.
You should send your file like this:
app.MapGet("/download", () =>
{
//...
Results.File("myfile.pdf");
});
there are some overloads of this method you can pick up the one that fit your requirements.
I don't know how to access folder "Images" on android devies, to send file directly to it.
I'm able to successfully send file using below code, however after research I'm not able to specify "Uri" so I can't receive file in "Image" folder or any other than "Download" which is i think default.
var file = #"C:\Users\JDOMINO\Desktop\Personal\Zegarek\1.jpg";
var uri = new Uri("obex://" + device.DeviceAddress + "/" + file);
var request = new ObexWebRequest(uri);
request.ReadFile(file);
var response = (ObexWebResponse)request.GetResponse();
MessageBox.Show(response.StatusCode.ToString());
// check response.StatusCode
response.Close();
Expected output would be something like to change Uri from "obex://" to "obex://Images/Newfolder" however this doesn't work.
I found this solution for showing an image in the body of the email:
Add image to body of an email
And it works fine but it also adds the image as an attachment to the email.
Attachment inlineLogo = new Attachment(EmailLogo.ImageUrl);
mailMsg.Attachments.Add(inlineLogo);
string contentID = "Image";
inlineLogo.ContentId = contentID;
//To make the image display as inline and not as attachment
inlineLogo.ContentDisposition.Inline = true;
inlineLogo.ContentDisposition.DispositionType = DispositionTypeNames.Inline;
//To embed image in email
mailMsg.Body = "<htm><body> <img height=\"49\" width=\"169\" src=\"cid:" + contentID + "\"> </body></html>";
There is a line of code with the comment to display as inline and not as attachment but this line isn't working because the image still gets attached to the email:
//To make the image display as inline and not as attachment
inlineLogo.ContentDisposition.Inline = true;
inlineLogo.ContentDisposition.DispositionType = DispositionTypeNames.Inline;
How can I stop the image from attaching to the email?
Use AlternateView to store your html code with image embedded as LinkedResource:
string contentID = "Image";
var inlineLogo = new LinkedResource(EmailLogo.ImageUrl, "image/png");
inlineLogo.ContentId = contentID;
var htmlView = AlternateView.CreateAlternateViewFromString(
"<html><body> <img height=\"30\" width=\"30\" src=\"cid:" + contentID + "\"> </body></html>",
Encoding.UTF8, "text/html");
htmlView.TransferEncoding = TransferEncoding.QuotedPrintable;
htmlView.LinkedResources.Add(inlineLogo);
mailMsg.AlternateViews.Add(htmlView);
P.S. Embedding image as base24 string is not very good idea, because many mail clients do not support such ability.
If you want to display an image in an email it has to exist somewhere. It is either attached as part of the message payload (regardless of whether it is "displayed inline" or as a true "attachment") - or is fetched from a remote web server when the reader reads the email (and optionally has to choose to "view images")
To not attach the image to the email payload itself:
You have to host the image on a public web server so that the reader opening the message can access it.
You have to use a fully qualified URL in your message body source, so it can find it.
Assuming you have stored the image on your web server at the following URL:
http://www.example.com/images/myimage.jpg
... then your source should simply change to reflect:
mailMsg.Body = "<html><body> <img height=\"49\" width=\"169\" src=\"http://www.example.com/images/myimage.jpg\"> </body></html>";
No need to attach it at all.
An alternative that can be used which embeds the image inline, but also isnt generally filtered by email clients is (which is generally the case today in things like junk mail) You could use a DataURL.
<img src="data:image/<type>;base64,<string>"/>
where <type> is the image type, ie jpg, gif,png, and is a base64 string. Just convert the image to a base64 string and assign it to the source using the above syntax
For example, with a jpeg...
mailMsg.Body = "<html><body> <img height=\"49\" width=\"169\" src=\"data:image/jpg;base64,<myPictureString>"\"> </body></html>";
My colleague has developed an application that sends an html email showing an enclosed picture. The program code is something like the code in Send inline image in email.
The email looks fine when there is no enclosed image but when an image is enclosed and displayed from linked source, all the css is broken.
The solution is to use base64 string:
imgstring = "<IMG SRC='data:image/" + image.file_ext.Replace(".", "").Trim () +
";base64," + CreateBase64Image(imageFullPath) +
"' NAME='image' ALIGN=BOTTOM WIDTH=298 HEIGHT=172 BORDER=0>";
body = body.Replace("*replaceme*", imgstring );
private string CreateBase64Image(String path)
{
byte[] imageArray = System.IO.File.ReadAllBytes(path);
string base64ImageRepresentation = Convert.ToBase64String(imageArray);
return base64ImageRepresentation;
}
In C#, I'm uploading a video file to Facebook from a server, using the following method:
string fullurl = "https://graph-video.facebook.com/me/videos?" + "title=" + title + "&access_token=" + accessToken;
WebClient client = new WebClient();
byte[] returnBytes = client.UploadFile(fullurl, path);
path is the path to the video file on the server. This works, and shows a video uploaded post on the user profile. How can I add a text description (with link) and action links to that post?
please keep the answers in C#
you should pass extra query string parameter - description - with embedded URL. Something like this:
var description = "Bring your conversations to life on Facebook. With face-to-face video calling, now you can watch your friends smile, wink and LOL.\n\nTo get started, visit http://www.facebook.com/videocalling";
string fullurl = string.Format("https://graph-video.facebook.com/me/videos?title={0}&description={1}&access_token={2}", HttpUtility.UrlEncode(title), HttpUtility.UrlEncode(description ), accessToken);
I did it. Following is my code:
string a = "User_Access_Token";
string fullurl = string.Format("https://graph-video.facebook.com/me/videos?title={0}&description={1}&access_token={2}", HttpUtility.UrlEncode("Dulha"), HttpUtility.UrlEncode("hello"), a);
WebClient client = new WebClient();
byte[] returnBytes = client.UploadFile(fullurl, #"C:\Users\Users\Downloads\Dulha.mp4");