0

Up to this point I've managed to get it to work for a single argument (body) but without the second argument (photoUrl):

function SendTelegram(body,photoUrl) {
  if (body.map) {
    var response = body.map(function(b) {return SendTelegram(b);});
  } else {
    var response = UrlFetchApp.fetch(
      "https://api.telegram.org/bot" + 'AAA' + 
        "/sendPhoto?caption=" + encodeURIComponent(body) + 
          "&photo=" + encodeURIComponent(photoUrl) + 
            "&chat_id=" + 'BBB' + 
              "&disable_web_page_preview=true&parse_mode=HTML"
    );
  }
}

I'm not able to understand how I should proceed to work with this two different arguments, for example, the function in the spreadsheet would be:

=ARRAYFORMULA(SendTelegram(A1:A,B1:B))

In my attempts when I try to add the second argument, it always uses the first row value in all calls, it doesn't follow the array one by one.

1 Answer 1

1

In your script, at SendTelegram(b) of var response = body.map(function(b) {return SendTelegram(b);});, the 2nd argument is not set. By this, at 2nd loop, photoUrl is not declared. I thought that this might be the reason of your issue.

And, in your script, I thought that it might be required to return the response value. So, how about the following modification?

Modified script:

function SendTelegram(body, photoUrl) {
  if (body.map) {
    return body.map((b, i) => SendTelegram(b, photoUrl[i]));
  } else if (body && photoUrl) {
    return UrlFetchApp.fetch(
      "https://api.telegram.org/bot" + 'AAA' +
      "/sendPhoto?caption=" + encodeURIComponent(body) +
      "&photo=" + encodeURIComponent(photoUrl) +
      "&chat_id=" + 'BBB' +
      "&disable_web_page_preview=true&parse_mode=HTML", { muteHttpExceptions: true }
    ).getContentText();
  }
  return null;
}

Note:

  • This is a simple modification. Because I cannot understand your expected value from the URL. So, please modify the script for your situation.
Sign up to request clarification or add additional context in comments.

2 Comments

Hi @Tanaike When called, it sends the text and image to a channel alert on Telegram, the direct use is for that, I end up not needing to register anything, I just send it and finish using the function. Thanks for support!
@Brondby IF Thank you for replying. I'm glad your issue was resolved. Thank you, too.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.