0

With my codes I can send text with telegram bot, I can send a photo from url bot I didnt find answer for how can I send image from file

Can you help me?

https://api.telegram.org/botxxxxxxxxxxxxx/sendPhoto?chat_id=xxxxxxxxx&photo=C:/Users/LENOVO/Pictures/tempexcel.JPG
jps
  • 20,041
  • 15
  • 75
  • 79
  • Using `multipart/form-data` in your request should do the trick. See for example https://stackoverflow.com/questions/43969042/how-to-send-photo-by-telegram-bot-using-multipart-form-data . – druskacik Nov 15 '21 at 12:21
  • there is code for phyton i look for vb.net :) – user10823899 Nov 15 '21 at 17:57

1 Answers1

0

Using the following function for flutter, you can send an image as a document via telegram bot.

Required Files/ Variables:

  • File file: The image you want to send
  • botToken
  • chatId: Id of the chat where you want to upload the file

Add dependencies:

http: ^0.13.5

This function will return the file Id which can be used further to get the file path


Future<String> send(File file) async {
Uri url = Uri.parse(
    "https://api.telegram.org/bot$botToken/sendDocument?chat_id=$chatId");
http.MultipartRequest request = http.MultipartRequest("POST", url);
request.files.add(await http.MultipartFile.fromPath('document', file.path));
http.Response r = await http.Response.fromStream(await request.send());
// file sent
if (r.statusCode == 200) {
  debugPrint("Uploaded!");
} else {
  debugPrint("Unable to send the data");
  return "";
}
debugPrint(r.body);
Map sentResponse = jsonDecode(r.body);
if (sentResponse["ok"] == true) {
  fileId = sentResponse['result']["document"]["file_id"];
} else {
  debugPrint("Data is not uploaded. Try again");
  return "";
}
return fileId;
}
AJwarrior
  • 21
  • 2