问题
I'm trying some Google Apps Script script for telegram bot API.
var token = "BOT:TOKEN";
var telegramUrl = "https://api.telegram.org/bot" + token;
var chat_id = "CHAT_ID";
var image1 = "https://telegram.org/img/t_logo.png";
var image2 = "https://www.gstatic.com/images/branding/product/2x/apps_script_48dp.png";
var data = {
method: "post",
payload: {
"method": "sendMediaGroup",
"chat_id": chat_id,
"media": [
{"type": "photo", "media": image1},
{"type": "photo", "media": image2},
]
}
}
UrlFetchApp.fetch('https://api.telegram.org/bot' + token + '/', data);
}
Telegram Bot API Docs said that media type is an Array of InputMediaPhoto
. But I dont understand. Could anyone help me an example of inputMediaPhoto
for sending group of photos use sendMediaGroup method?
I did try method sendPhoto
https://core.telegram.org/bots/api#sendphoto, it worked. Now i need to send group of photos.
回答1:
From your replying comments, I could confirm the following situation.
- Your
token
can be used. - When your current script is run, the following error message is returned.
{"ok":false,"error_code":400,"description":"Bad Request: can't parse media JSON object"}
From above situation, I thought that the request body might be required to be sent as JSON. So how about the following modification?
Modified script:
From:var data = {
method: "post",
payload: {
"method": "sendMediaGroup",
"chat_id": chat_id,
"media": [
{"type": "photo", "media": image1},
{"type": "photo", "media": image2},
]
}
}
To:
var data = {
method: "post",
payload: JSON.stringify({
"method": "sendMediaGroup",
"chat_id": chat_id,
"media": [
{"type": "photo", "media": image1},
{"type": "photo", "media": image2},
]
}),
contentType: "application/json"
}
Reference:
- fetch(url, params)
回答2:
Recently, I need to send group photos into Telegram using Telegram Bot API and couldn't figure out much and have land on this page. Getting some idea from @Tanaike and debug through @pyTelegramBotAPI, I have done some Python code that will send local files to Telegram as group photos. So, I just wanted to share it here to benefit people who facing the same problem.
#!/usr/bin/python
import requests
TOKEN = "random-number:random-alpha-numeric"
CHAT_ID = "-random-number"
request_url = "https://api.telegram.org/bot" + TOKEN + "/sendMediaGroup"
params = {
"chat_id": CHAT_ID
, "media":
"""[
{
"type": "photo"
, "media": "attach://random-name-1"},
{
"type": "photo"
, "media": "attach://random-name-2"}
]"""
}
files = {
"random-name-1": open("/home/pc/Desktop/watermark/data.png", "rb")
, "random-name-2": open("/home/pc/Desktop/watermark/data.png", "rb")
}
result = requests.post(request_url, params= params, files= files)
print(result.text)
来源:https://stackoverflow.com/questions/63368751/what-is-inputmediaphoto-and-how-to-send-an-array-of-such-resources-to-telegram-a