Reputation: 91
The sendPhoto command require an argument photo defined as InputFile or String.
The API doc tells:
Photo to send. You can either pass a file_id as String to resend a photo that is already on the Telegram servers, or upload a new photo using multipart/form-data. And
InputFile
This object represents the contents of a file to be uploaded. Must be posted using multipart/form-data in the usual way that files are uploaded via the browser.
Upvotes: 1
Views: 7468
Reputation: 41
here is a working, parametrized code sample:
using System.Linq;
using System.IO;
using System.Text;
using System.Net.Http;
using System.Threading.Tasks;
namespace ConsoleApplication
{
public class Program
{
public static void Main(string[] args)
{
SendPhoto(args[0], args[1], args[2]).Wait();
}
public async static Task SendPhoto(string chatId, string filePath, string token)
{
var url = string.Format("https://api.telegram.org/bot{0}/sendPhoto", token);
var fileName = filePath.Split('\\').Last();
using (var form = new MultipartFormDataContent())
{
form.Add(new StringContent(chatId.ToString(), Encoding.UTF8), "chat_id");
using (FileStream fileStream = new FileStream(filePath, FileMode.Open, FileAccess.Read))
{
form.Add(new StreamContent(fileStream), "photo", fileName);
using (var client = new HttpClient())
{
await client.PostAsync(url, form);
}
}
}
}
}
}
Upvotes: 2
Reputation: 664
I'm not a C# Developer but I generated this code using Postman, it uses RestSharp lib
var client = new RestClient("https://api.telegram.org/bot%3Ctoken%3E/sendPhoto");
var request = new RestRequest(Method.POST);
request.AddHeader("postman-token", "7bb24813-8e63-0e5a-aa55-420a7d89a82c");
request.AddHeader("cache-control", "no-cache");
request.AddHeader("content-type", "multipart/form-data; boundary=---011000010111000001101001");
request.AddParameter("multipart/form-data; boundary=---011000010111000001101001", "-----011000010111000001101001\r\nContent-Disposition: form-data; name=\"photo\"; filename=\"[object Object]\"\r\nContent-Type: false\r\n\r\n\r\n-----011000010111000001101001\r\nContent-Disposition: form-data; name=\"chat_id\"\r\n\r\n2314123\r\n-----011000010111000001101001--", ParameterType.RequestBody);
IRestResponse response = client.Execute(request);
Just tweak it and it should work.
Upvotes: 1