skip to Main Content

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.

2

Answers


  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:

    Login or Signup to reply.
  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)
    
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search