To collect Telegram messages sent from my profile, register in the spreadsheet and create a tweet from these messages, I set this model:
var token = "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX";
var telegramUrl = "https://api.telegram.org/bot" + token;
var webAppUrl = "https://script.google.com/macros/s/UUUUUUUUUUUUUUUUUUUUUUUUUUU";
function setWebhook() {
var url = telegramUrl + "/setWebhook?url=" + webAppUrl;
var response = UrlFetchApp.fetch(url);
}
function doPost(e) {
var contents = JSON.parse(e.postData.contents);
var dateNow = Utilities.formatDate(new Date(), "GMT-03:00", "yyyy-MM-dd' 'HH:mm:ss' '");
var id = contents.message.from.id;
if (id != 123456789) return;
var username = contents.message.from.username;
var name = contents.message.from.first_name + " " + contents.message.from.last_name;
var text = contents.message.text;
if (!text) return;
var ssId = "ID TO SPREADSHEET";
var sheet = SpreadsheetApp.openById(ssId).getSheetByName("SPREADSHEET NAME");
sheet.appendRow([dateNow, id, username, name, text]);
var twitterKeys= {
TWITTER_CONSUMER_KEY: "<<consumer key>>",
TWITTER_CONSUMER_SECRET: "<<consumer secret>>",
TWITTER_ACCESS_TOKEN: "<<access token>>",
TWITTER_ACCESS_SECRET: "<<access secret>>"
};
var props = PropertiesService.getScriptProperties();
props.setProperties(twitterKeys);
twit = new Twitter.OAuth(props);
var service = new Twitter.OAuth(props);
if ( service.hasAccess() ) {
var response = twit.sendTweet(text);
if (response) {
Logger.log("Tweet ID " + response.id_str);
} else {
// Tweet could not be sent
// Go to View -> Logs to see the error message
}
}
}
The library I identified with the name of Twitter
I'm using is:
MKvHYYdYA4G5JJHj7hxIcoh8V4oX7X1M_
But tweets are not being sent, when I try to use only the sending part of the tweet separately, the message Send tweet failure. Error was: {"name": "Exception"}
appears in the log records.
Separated send test:
function sendTweet(status) {
status = status || "Teste";
var twitterKeys= {
TWITTER_CONSUMER_KEY: "<<consumer key>>",
TWITTER_CONSUMER_SECRET: "<<consumer secret>>",
TWITTER_ACCESS_TOKEN: "<<access token>>",
TWITTER_ACCESS_SECRET: "<<access secret>>"
};
var props = PropertiesService.getScriptProperties();
props.setProperties(twitterKeys);
twit = new Twitter.OAuth(props);
var service = new Twitter.OAuth(props);
if ( service.hasAccess() ) {
var response = twit.sendTweet(status);
if (response) {
Logger.log("Tweet ID " + response.id_str);
} else {
// Tweet could not be sent
// Go to View -> Logs to see the error message
}
}
}
I would like some help on what I need to edit in the script so that the messages collected on Telegram become text and are sent to Twitter as a new tweet.