Answer the question
In order to leave comments, you need to log in
How to get the necessary data from UrlFetchApp or JSON.parse?
According to the logs, the data comes out in this format, you need to get
message_id=53204.0
{result={date=1.635769484E9, message_id=53204.0, from={first_name=IPhoшка [Инфобот], username=infoip4_bot, is_bot=true, id=2.09014609loE9}, , type=supergroup}}, ok=true}
var takeid = JSON.parse(UrlFetchApp.fetch('https://api.telegram.org/bot' + token + '/', data));
var idString = takeid.toString();
var start = 0;
var searching = "message_id=" + start.toString() + ".0";
while (idString.indexOf(searching!=-1)){
start++
}
Answer the question
In order to leave comments, you need to log in
You are moving in the right direction, but there are a few caveats.
First , the request url (in your case: ) does not contain a function to run.
It should be like this:'https://api.telegram.org/bot' + token + '/'
'https://api.telegram.org/bot' + token + '/getUpdates'
'https://api.telegram.org/bot' + token + '/getMe'
'https://api.telegram.org/bot' + token + '/sendMessage'
data
is that the second parameter for a fetch(url, data)
class function UrlFetchApp
must be an object. post
. {
"method": `POST`,
"contentType": `application/json`,
"muteHttpExceptions": true,
"payload": JSON.stringify(data)
}
payload
, we indicate if you want to send something to the telegram (depends on the selected function in the request url). fetch(url, data)
returned a result response
, you need to extract data from it by the method response .getContentText()
. And ash then parse through JSON.parse
. /**
* Простой метод проверки токена авторизации вашего бота. Не требует параметров. Возвращает основную информацию о боте в виде объекта User.
*
* @return {Object.User}
*/
function getMe() {
const response = UrlFetchApp
.fetch(
`https://api.telegram.org/bot${TOKEN}/getMe`,
{
"method": `POST`,
"contentType": `application/json`,
"muteHttpExceptions": true
}
);
let result = response
.getContentText();
result = JSON
.parse(result);
if (!result.ok)
throw new Error(result.description);
return result;
}
/**
* Используйте этот метод для отправки текстовых сообщений. В случае успеха отправленное сообщение возвращается.
*
* @param {String|Number} chatId Уникальный идентификатор целевого чата или имя пользователя целевого канала (в формате @channelusername)
* @param {String} text Текст отправляемого сообщения, 1-4096 знаков после синтаксического анализа сущностей.
* @param {Object} options
*
* @return {Object.Message}
*/
function sendMessage(chatId, text, options) {
if (!arguments.length)
throw new TypeError(`Параметры () не соответствуют сигнатуре метода TelegramBot.sendMessage.`);
const data = {};
if (!([`string`, `number`].includes(typeof chatId) && String(chatId).trim().length))
throw new TypeError(`Параметры (${typeof chatId}) не соответствуют сигнатуре метода TelegramBot.sendMessage.`);
data.chat_id = String(chatId).trim();
if (!([`string`, `number`].includes(typeof text) && String(text).trim().length))
throw new TypeError(`Параметры (${typeof chatId}, ${typeof text}) не соответствуют сигнатуре метода TelegramBot.sendMessage.`);
data.text = String(text).trim();
if (options) {
if (typeof options !== `object`)
throw new TypeError(`Параметры (${typeof chatId}, ${typeof text}, ${typeof options}) не соответствуют сигнатуре метода TelegramBot.sendMessage.`);
if ((item => typeof item === `string` && item.trim().length)(options.parse_mode))
data.parse_mode = options.parse_mode;
if (typeof options.entities === `object`)
data.entities = options.entities;
if (typeof options.disable_web_page_preview === `boolean`)
data.disable_web_page_preview = options.disable_web_page_preview;
if (typeof options.disable_notification === `boolean`)
data.disable_notification = options.disable_notification;
if ((item => typeof item === `number` && Number.isInteger(item) && item > 0)(options.reply_to_message_id))
data.reply_to_message_id = options.reply_to_message_id;
if (typeof options.allow_sending_without_reply === `boolean`)
data.allow_sending_without_reply = options.allow_sending_without_reply;
if (typeof options.reply_markup === `object`)
data.reply_markup = options.reply_markup;
}
const response = UrlFetchApp
.fetch(
`https://api.telegram.org/bot${TOKEN}/sendMessage`,
{
"method": `POST`,
"contentType": `application/json`,
"muteHttpExceptions": true,
"payload": JSON.stringify(data)
}
);
let result = response
.getContentText();
result = JSON
.parse(result);
if (!result.ok)
throw new Error(result.description);
return result;
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question