繁体   English   中英

通过电报机器人发送图像

[英]Sending Image via Telegram Bot

我想通过Telegram中的机器人将本地图像文件发送给用户,但似乎无法弄清楚该如何做。

Telegram网站指出,要执行此操作,我应该使用multipart / form-data并以“常规方式”发送图像。 查找multipart / form-data后,我目前正在尝试将文件上传到表单,并将表单数据发送到Telegram API。

 let form = document.createElement("FORM"); form.setAttribute("action", "https://api.telegram.org/bot{some token}/sendMessage"); form.setAttribute("method", "POST"); form.setAttribute("id", "f"); form.setAttribute("enctype", "multipart/form-data"); let file = document.createElement("input"); file.setAttribute("type", "file"); file.setAttribute("value", "\\some\\local\\path"); file.setAttribute("id", "file"); let text = document.createElement("input"); text.setAttribute("type", "text"); text.setAttribute("name", "text"); text.setAttribute("value", '"test"'); let ID = document.createElement("input"); ID.setAttribute("type", "text"); ID.setAttribute("name", "chat_ID") ID.setAttribute("value", "123456789"); let s = document.createElement("input"); s.setAttribute("type", "submit"); form.appendChild(text); form.appendChild(ID); form.appendChild(s); form.appendChild(file); document.getElementsByTagName("body")[0].appendChild(form); var formJSON = $("form").serializeArray(); console.log(formJSON); var j = "{" for (let i = 0; i < formJSON.length; i++) { j += '"' + (formJSON[i]["name"]) + '"' + ":" + (formJSON[i]["value"]); if (i != formJSON.length - 1) { j += ","; } } j += "}"; jP = JSON.parse(String(j)); console.log(document.getElementById("file").files); form.setAttribute("action", "https://api.telegram.org/bot{some token}/sendPhoto?" + "chat_id=" + (jP["chat_ID"]) + "&photo=" + "\\some\\local\\path"); document.getElementById("f").submit(); 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.6.2/angular.min.js"></script> <!DOCTYPE html> <html lang="en" xmlns="http://www.w3.org/1999/xhtml"> <script type="text/javascript" src="http://ajax.microsoft.com/ajax/jquery/jquery-1.6.2.min.js" /> <head> <meta charset="utf-8" /> <title>This is a test</title> </head> <body> </body> <script src="formData.js" /> </html> 

目前,我不确定是否正在上传文件,因为我在Chrome上运行HTML时没有选择文件,并且fileList为空。 另外,Telegram坚持将数据作为JSON发送(这就是为什么我将表单数据解析为JSON)。 我尝试传递本地文件路径,但似乎认为我正在尝试在电报服务器上找到文件ID。 我不太确定如何从这里继续。

如果使用此代码,则做得很好:

 <form method="POST" action="https://api.telegram.org/<YOU_TOKEN>/sendPhoto" enctype="multipart/form-data"> chat ID <input type="text" name="chat_id" value="72XXXX61" /> <br/> caption(comment on photo) <input type="text" name="caption" value="hi world"/> <br/> select File from you pc <input type="file" name="photo"/> <br/> send btn. <input type="submit" value="sendPhoto" /> </form> 

现在应该通过电报bot api发送照片的3种方式:

  1. 通过真实网址(例如: http : //example.net/image.jpg
  2. 通过电报服务器中文件的ID(当您在file_id下发送/获取照片消息时,u可以进入)
  3. 发送真实的文件,如upper ex。

如果您想直接从脚本发送照片,我不知道它是如何工作的,因为每次我收到错误T_T时,我都会尝试将图像转换为Base64 / readAsText / blob ... etc

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM