首页 > 解决方案 > twitter API/bot 的 Node.js 问题

问题描述

我正在尝试设置一个 twitter bot 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');
	}
}	
	

我不太确定从哪里开始寻找,在我看来一切都很好 - 所以如果有人能告诉我我在这里做错了什么,我将不胜感激

标签: javascriptnode.js

解决方案


It has nothing to do with twitter API, it's just a SyntaxError you're missing a closing } at the end of 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

Or depending what you were trying to do:

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

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

Update

I've now got the following error after making that change: D:\TwitterBot\Node1\bot.js:39 media_ids: [id] ^^^^^^^^^ SyntaxError: Unexpected identifier I've updated the original post with the new code

You're missing a comma (,) after status property.

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);
}

推荐阅读