Node.js Nodejs:Twit节点API状态已发布,无图像且无错误
我希望有人能帮助我。 我尝试过搜索,但似乎找不到任何遇到过此问题的人 我正忙着学习NodeJS,在路上我决定制作一个twitter机器人来帮助我学习。 我将使用:Node.js Nodejs:Twit节点API状态已发布,无图像且无错误,node.js,api,file-upload,twitter,Node.js,Api,File Upload,Twitter,我希望有人能帮助我。 我尝试过搜索,但似乎找不到任何遇到过此问题的人 我正忙着学习NodeJS,在路上我决定制作一个twitter机器人来帮助我学习。 我将使用: NodeJS Twit节点API 在我的Win 10桌面上运行 这个Twitter机器人本质上是从一个包含.png图像的文件夹中发布一个随机图像,并以prefs.js文件中存储的两个给定时间间隔从分隔的.txt文件中发布一个随机状态 e、 g.1.png,Hello World,最小间隔时间:400分钟,最大间隔时间:600分钟
- NodeJS
- Twit节点API
- 在我的Win 10桌面上运行
const fs = require("fs"),
path = require("path"),
Twit = require("twit"),
prefs = require("./prefs.js");
config = require(path.join(__dirname, "config.js"));
// Tweet random image along with a random status
function tweetRandomImage() {
//read filenames from directory into an array
fs.readdir(__dirname + "/images", function (err, files) {
if (err) {
console.log("error:", err);
} else {
let images = [];
files.forEach(function (f) {
let imagePath = path.join(__dirname, "/images/" + f);
let filesize = getFilesizeInMegabytes(imagePath);
let file_ext = path.extname(f);
images.push(f);
});
console.log("opening an image...");
const imagePath = path.join(__dirname, "/images/" + randomFromArray(images)),
b64content = fs.readFileSync(imagePath, { encoding: "base64" });
console.log("uploading an image...");
console.log("uploading image : " + imagePath);
T.post(
"media/upload",
{ media_data: b64content },
function (err, data, response) {
if (err) {
console.log("error:", err);
} else {
let randStatus = getRandomText(";", "tweets.txt");
console.log("\n");
console.log("Random Status :");
console.log(randStatus + "\n");
console.log("\n");
// post status
T.post(
"statuses/update",
{
status: randStatus,
media_ids: new Array(data.media_id_string),
},
function (err, data, response) {
if (err) {
console.log("error:", err);
} else {
console.log("posted an image!\n");
console.log("\n");
}
}
);
}
}
);
}
});
}
tweetRandomImage();