发送照片到电报(API/Bot) [英] Sending Photo to Telegram (API / Bot)

查看:21
本文介绍了发送照片到电报(API/Bot)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从Excel向电报发送消息。它工作得很好。 但是我怎么才能发送照片呢?我不明白(https://core.telegram.org/bots/api#sendphoto)

感谢您的帮助!

我的发送消息:

Dim objRequest As Object
Dim strChatId As String
Dim strMessage As String
Dim strPostData As String
Dim strResponse As String

 strChatId = Worksheets("Einstellungen").Cells(3, "AB")
 strMessage = Report
 APIcode = Worksheets("Einstellungen").Cells(2, "AB")

strPostData = "chat_id=" & strChatId & "&text=" & strMessage

 Set objRequest = CreateObject("MSXML2.XMLHTTP")
With objRequest
  .Open "POST", "https://api.telegram.org/" & APIcode & "/sendMessage?", False
  .setRequestHeader "Content-Type", "application/x-www-form-urlencoded"
  .send (strPostData)
   GetSessionId = .responseText
End With

推荐答案

如果您的代码按原样用于纯文本消息,则只需对其进行几处更改。

您当前可能正在使用API的sendMessage方法,该方法接受chat_idtext参数。

您要使用sendPhoto方法,该方法传递chat_idphoto参数(但没有text参数)。

我从来没有用过或听说过Telegram,也没有密钥,所以我不能测试它,但理论上,你可以通过这样的URL发送一张照片:

Sub telegram_SendPhoto()

    Const photoURL = "https://i.imgur.com/0eH6d1v.gif" 'URL of photo

    Dim objRequest As Object, strChatId As String, APIcode As String
    Dim strPostData As String, strResponse As String

    strChatId = Worksheets("Einstellungen").Cells(3, "AB")
    APIcode = Worksheets("Einstellungen").Cells(2, "AB")

    strPostData = "chat_id=" & strChatId & "&photo=" & photoURL

    Set objRequest = CreateObject("MSXML2.XMLHTTP")
    With objRequest
        .Open "POST", "https://api.telegram.org/" & APIcode & "/sendPhoto?", False
        .setRequestHeader "Content-Type", "application/x-www-form-urlencoded"
        .send (strPostData)
        strResponse = .responseText
    End With

    MsgBox strResponse

End Sub

file_id作为字符串传递以发送Telegram服务器上存在的照片(推荐),将HTTP URL作为字符串传递以使Telegram从Internet获取照片(如上),或使用multipart/form-data上载新照片。More info on Sending Files »

这篇关于发送照片到电报(API/Bot)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆