javascript - 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');
}
}
我不太确定从哪里开始寻找,在我看来一切都很好 - 所以如果有人能告诉我我在这里做错了什么,我将不胜感激
解决方案
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);
}
推荐阅读
- python - 如何在我的画布小部件(tkinter)上弹出消息框
- javascript - 如何访问远程脚本标签的 innerHTML / text / 内容
- python - 通过 Django HttpResponse 提供的文件是否从磁盘进入内存或流?
- java - 对HashMap中容量计算算法的理解
- android - 由多种构建风格处理的单个应用程序链接
- python - 如何使用python获取标签中的html文本
- java - JavaFx JFoenix 水平 JFXNodesList
- c# - 如何在c#中获取具有特定属性的特定节点
- c# - 添加组合框,其中包含按按钮调用的功能
- asp.net-web-api - WebAPI 和 OData 函数