我正在尝试在Heroku上部署我的Twitter Bot,但不能。
我已经在Heroku Vars中配置了Twitter密钥,package.json没问题...我已经在Google中进行了调查,但是找不到解决方案。我不知道我在做什么错。有人可以帮我吗?
错误是这样:
2020-09-04T09:05:15.326219+00:00 app[worker.1]: at
Object.Module._extensions..js
(internal/modules/cjs/loader.js:1157:10)
2020-09-04T09:05:15.326219+00:00 app[worker.1]: at
Module.load (internal/modules/cjs/loader.js:985:32)
2020-09-04T09:05:15.326219+00:00 app[worker.1]: at
Function.Module._load
(internal/modules/cjs/loader.js:878:14)
2020-09-04T09:05:15.326220+00:00 app[worker.1]: at
Function.executeUserEntryPoint [as runMain]
(internal/modules/run_main.js:71:12)
2020-09-04T09:05:15.326221+00:00 app[worker.1]: at
internal/main/run_main_module.js:17:47 {
2020-09-04T09:05:15.326221+00:00 app[worker.1]: code:
'MODULE_NOT_FOUND',
2020-09-04T09:05:15.326221+00:00 app[worker.1]:
requireStack: [ '/app/bot.js' ]
2020-09-04T09:05:15.326222+00:00 app[worker.1]: }
2020-09-04T09:05:15.389873+00:00 heroku[worker.1]: Process
exited with status 1
2020-09-04T09:05:15.422459+00:00 heroku[worker.1]: State
changed from up to crashed
Package.json :
{
"name": "addicteddev",
"version": "1.0.0",
"description": "Addicted Dev - Twitter Bot",
"main": "bot.js",
"dependencies": {
"twit": "^2.2.11",
"twitter": "^1.7.1"
},
"devDependencies": {},
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"repository": {
"type": "git",
"url":
"git+https://github.com/pablohs1986/
addictedDEV_TwitterBot.git"
},
"keywords": [
"bot",
"twitter",
"javascript",
"nodejs",
"dev"
],
"author": "Pablo Herrero",
"license": "MIT",
"bugs": {
"url":
"https://github.com/pablohs1986/
addictedDEV_TwitterBot/issues"
},
"homepage":
"https://github.com/pablohs1986/
addictedDEV_TwitterBot#readme"
}
Bot.js :
// Setup
console.log('The bot is starting');
var Twit = require('twit');
var config = require('./config/config.js');
var T = new Twit(config);
var addDevStatuses = [
"asdasdasdsa"
];
let hashtags = [
'#java', '#Java', '#js', '#javascript', '#JavaScript', '#python', '#Python', '#nodejs', '#nodeJS', '#100DaysOfCode', '#100daysofcode', '#100DOC', '#100doc', '#stackoverflow',
'#StackOverFlow', '#angular', '#Angular', '#VSCode', '#vscode', '#Netbeans', '#netbeans', '#Oracle', '#oracle', '#dev', '#Dev', '#developer', '#Developer', '#Development', '#development', '#sql', '#SQL'
]
// function randomStatus(){
// return status = addDevStatuses[Math.floor(Math.random()*addDevStatuses.length)];
// }
function returnRandomElementFromArray(array){
return status = array[Math.floor(Math.random()*array.length)];
}
function onlyUniqueTweets(value, index, self){
return self.indexOf(value) === index;
}
// Post every 2 hours
function tweetIt(text){
var params = {
status: text
}
T.post('statuses/update', params, function (err, data){
if(err){
console.log("Shit!!");
console.log(err);
}else{
console.log("It worked! Tweeting!!");
}
})
}
tweetIt(returnRandomElementFromArray(addDevStatuses));
setInterval(tweetIt, 1000*60*120, returnRandomElementFromArray(addDevStatuses));
// Retweet hashtags
function retweetHashtags(hashtag){
var params = {
q: hashtag + ' ',
result_type: 'mixed',
count: '5'
}
T.get('search/tweets', params, function(err_search, data_search, response_search){
let tweets = data_search.statuses;
if(err_search){
console.log("Shit searching!!");
console.log(err_search);
}else{
var tweetIDList = [];
for(let tweet of tweets){
if(tweet.text.startsWith("RT @")){
if(tweet.retweeted_status){
tweetIDList.push(tweet.retweeted_status.id_str);
}else{
tweetIDList.push(tweet.id_str);
}
}else{
tweetIDList.push(tweet.id_str);
}
}
tweetIDList = tweetIDList.filter(onlyUniqueTweets);
console.log("TweetIDList = \n" + tweetIDList);
for (let tweetID of tweetIDList) {
T.post('statuses/retweet/:id', {id : tweetID}, function(err_rt, data_rt, response_rt){
if(!err_rt){
console.log("\n\nRetweeted! ID - " + tweetID);
}
else {
console.log("\nShit retweeting!! Duplication maybe... " + tweetID + "| HASHTAG - " + hashtag);
console.log("Error: " + err_rt);
}
})
}
console.log("It worked! Hashtags retweeted!!!");
}
})
}
retweetHashtags(returnRandomElementFromArray(hashtags));
setInterval(retweetHashtags, 1000*60*10, returnRandomElementFromArray(hashtags));
答案 0 :(得分:0)
对不起,我找到了,所以我将尝试回答问题:
这最终是路由问题。在Heroku上部署bot时,程序无法访问config.js文件或bot.js。
Config.js不想将其公开,因为它具有Twitter密钥。最后,我将其公开,但是引用了Config.env文件,该文件实际上是读取Twitter密钥(存储在Heroku Vars中)的文件,我可以通过[dotenv]模块访问该文件(https:// www .npmjs .com / package / dotenv),如下所示:
Config.js :
const path = require('path')
const dotenv = require('dotenv')
require('dotenv').config();
// Load config
dotenv.config({ path: './config/config.env' })
module.exports = {
consumer_key : process.env.API_KEY,
consumer_secret : process.env.API_SECRET_KEY,
access_token : process.env.ACCESS_TOKEN,
access_token_secret : process.env.ACCESS_TOKEN_SECRET
};
一旦完成推送,它就会完美地工作。
希望这对以后某人发生类似的事情很有帮助:)