Twitter API / bot

时间:2018-06-22 19:11:15

标签: javascript node.js

我正在尝试建立一个Twitter机器人API,以创建自定义图像。当我运行脚本时,出现以下错误:

D:\ TwitterBot \ Node1 \ bot.js:39                         media_ids:[id]                         ^^^^^^^^^^

SyntaxError:意外的标识符

到目前为止,我的js文件看起来像这样(出于明显的原因删除了API凭据)

console.log("The bot is starting...");

var Twit = require("twit");				

var T = new Twit({
  consumer_key:         "...",
  consumer_secret:      "...",
  access_token:         "...",
  access_token_secret:  "...",
})

var exec = require('child_process').exec;
var fs = require('fs');

function processing() {
	console.log('finished');
}

tweetIt();
//setInterval(tweetIt, 1000*60) //1 min

function tweetIt(){
	var cmd = 'processing-java --sketch=rainbow --run'
	exec(cmd, processing);

	function processing() {
		var filename = 'rainbow/output.png';
		var params = {
			encoding: 'base64'
		}
		var b64 = fs.readFileSync(filename, params);

		T.post('media/upload', { media_data: b64 }, uploaded);

		function uploaded(err, data, response) {
			var id = data.media_id_string;
			var tweet = {
  	  		status: '#test'
  	  		media_ids: [id]

			}
			T.post('statuses/update', tweet, tweeted);
		}

	}
}

function tweeted(err, data, response) {
	if (err) {
		console.log('fail');
	} else {
		console.log('pass');
	}
}	
	

我不太确定从哪里开始寻找东西,在我眼中一切似乎都很好-因此,如果有人可以在这里建议我做错了事,我将不胜感激

1 个答案:

答案 0 :(得分:1)

它与twitter API无关,只是SyntaxError,您在}的末尾缺少结尾function tweetIt() {

function tweetIt() {
    var cmd = 'processing-java --sketch="%cd%\\rainbow" --run'
    exec(cmd, processing);

    function processing() {
        var filename = 'rainbow/output.png';
        var params = {
            encoding: 'base64'
        }
        var b64content = fs.readFileSync(filename, params);

        T.post('media/upload', { media_data: b64content }, uploaded);
    }
} // This was missing

或者根据您要执行的操作:

 function tweetIt() {
    /* ... */
 } // This was missing

 function processing() {
    /* ... */
 }

更新

  

进行更改后,现在出现以下错误:   D:\ TwitterBot \ Node1 \ bot.js:39 media_ids:[id] ^^^^^^^^^^语法错误:   意外的标识符,我用新代码更新了原始帖子

(,)属性之后,您缺少逗号status

function uploaded(err, data, response) {
    var id = data.media_id_string;
    var tweet = {
        status: '#test', // Missing comma (,)
        media_ids: [id]

    }
    T.post('statuses/update', tweet, tweeted);
}