如何使用 Twitter API 发布来自另一个 js 文件的特定功能的推文 [英] How to publish a tweet using Twitter API with specific functions from another js file
问题描述
这是我的 bot.js 文件...它包含一个带有 node.js 的简单 twit 发布功能
This is my bot.js file... it consists of a simple twit publishing function with node.js
const config = require('./config')
const datasport = require('./botele')
const twit = require('twit')
const T = new twit(config)
T.post(
'statuses/update',
{ status: ' : '+ datasport.formattedRes },
(err, data, response) => {
if (err) {
console.log("oof! Something went wrong!");
} else {
console.log("Tweet sent successfully!");
}
}
)
然后,在我的 botele.js 文件中,我必须..
Then, in my botele.js file I have to..
var unirest = require("unirest");
var req = unirest("GET", "xxx");
req.query({
"stats": "true",
"events": "true"
});
req.headers({
"x-x-host": "x",
"x-x-key": "x"
});
req.end(function (res) {
if (res.error) throw new Error(res.error);
const formattedRes = res.body.results.map(singleObject =>{
return {
"homeName": singleObject.homeName,
}
});
module.exports = {
"formattedRes": formattedRes
}
});
formattedRes 准确返回我想要的变量:homeName 像这样:
formattedRes returns exactly the variable I want: homeName like this:
homeName: MyhomeName
我想要的是当我发布推文时我的 MyHomeName 变量出现在 Twitter 上!所以我可以使用它:
What I want is for my MyHomeName variable to appear on Twitter when I publish the twit! so I can work with it as:
{ status: ' :This is my home name call: '+ datasport.formattedRes + 'And yours? :) ' },
在推特帖子上清楚地给了我这个:
clearly gives me this on twitter post:
这是我家的名字电话:undefined 那么你的呢?:)
This is my home name call: undefined And yours? :)
一些帮助!
推荐答案
你的 module.exports
在一个函数中.那不是你导出东西的方式.
您应该做的是导出生成 formattedRes
的函数,然后运行它以获取所需的数据,然后使用该数据.
Your module.exports
is inside a function. That is not the way you export something.
What you should do is export the function which generates formattedRes
then run it to get your required data, and then use that data.
这篇关于如何使用 Twitter API 发布来自另一个 js 文件的特定功能的推文的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!