我如何用电报发送照片



使用我的代码我可以用电报机器人发送文本,我可以从url机器人发送照片。我没有找到如何从文件发送图像的答案

你能帮我吗?

https://api.telegram.org/botxxxxxxxxxxxxx/sendPhoto?chat_id=xxxxxxxxx&photo=C:/Users/LENOVO/Pictures/tempexcel.JPG

使用下面的flutter功能,您可以通过telegram bot将图像作为文档发送。

所需文件/变量:

  • 文件文件:要发送的图像
  • botToken
  • chatId:您要上传文件的聊天室的Id

添加依赖项:

http:^0.13.5

此函数将返回文件Id,该文件Id可用于进一步获取文件路径


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;
}

最新更新