1

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.

Digital Farmer
  • 1,705
  • 5
  • 17
  • 67
  • 1
    About `the message Send tweet failure. Error was: {"name": "Exception"} appears in the log records.`, where is the error occurs in your script? And, the error message is only that? For example, if the error occurs at the API, the error number is shown? – Tanaike Feb 18 '21 at 01:17
  • Hi @Tanaike, in fact when I run the script, there are no errors causing it to not be finalized. But when I leave the script for sending the tweet separately from the rest, when running the script it appears the log message I sent. The complete script the message is saved perfectly in the spreadsheet, but it finalizes the script without publishing the tweet, so I believe that the fail is the same. As the script is activated when a message is sent by the bot, so I'm not the one who activates it, I don't know how to look at the API response, if you can help me with that, I'm available. – Digital Farmer Feb 18 '21 at 01:27
  • 1
    Thank you for replying. From your replying, you cannot find the line in your script that the error occurs and the error number related to the API cannot be also found. Is my understanding correct? And, I have to apologize for my poor English skill. I cannot understand about `But when I leave the script for sending the tweet separately from the rest, when running the script it appears the error message I sent.`, `The complete template the message is saved perfectly in the spreadsheet, but it finalizes the script without publishing the tweet, so I believe that the error is the same.`. – Tanaike Feb 18 '21 at 01:32
  • I'll rephrase it to help: The message of the log that is in the question appears when I run the second script of the question. The first script in the running history all ends without error, but the tweet is not published. – Digital Farmer Feb 18 '21 at 01:38
  • I don't know how I look at the Twitter API response, I would need help with that so I could add those details to the question. – Digital Farmer Feb 18 '21 at 01:39
  • 1
    Thank you for replying. At first, can I consider that your `new Twitter.OAuth(props)` can be used for sending the text to the API? And, in your script, `new Twitter.OAuth(props)` is used 2 times. For example, when you use it one time, what result will you obtain? By the way, in my current environment, I cannot directly test the API because I have no account for it. So these are just my guess. I deeply apologize for this. – Tanaike Feb 18 '21 at 01:43
  • I created a chat discussion for limit messages here, ok? – Digital Farmer Feb 18 '21 at 01:48
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/228874/discussion-between-brondby-if-and-tanaike). – Digital Farmer Feb 18 '21 at 01:48

1 Answers1

2

Modification points:

When I tested for posting a text using the following sample script with your provided token,

const url = "https://api.twitter.com/1.1/statuses/update.json"
const params = {muteHttpExceptions: true, "method":"POST","payload":"status=test","headers":{"Authorization":"###"},"escaping":false};
const res = UrlFetchApp.fetch(url, params);
console.log(res.getContentText())

I confirmed the error message of {"request":"\/1.1\/statuses\/update.json","error":"Read-only application cannot POST."}. From this error message, it seems that the reason of your current issue is due to the settings of the application side of your twitter account for using twitter API. Ref Unfortunately, this error message is not shown in the library you are using. By this, unfortunately, the reason of the issue couldn't be directly found.

For this, please modify the permission of the app for twitter API from "Read" to "Read and Write" and "Read + Write + Direct Messages". After you modified the permission, please get new access token and secret. By this flow, when the new access token and secret are used, your script works.

Reference:

Tanaike
  • 181,128
  • 11
  • 97
  • 165