不幸的是,我对node.js的了解为零,因为到目前为止,我一直使用Ruby及其称为Pry的REPL。我发现node.js也有这样的软件包,可以通过“ npm”软件包管理器进行安装。我这样做的原因是node.js包“ facebook-chat-api”,该包可用于以编程方式发送Facebook聊天消息,据我所知,这在Ruby中(或者也可能是其他语言)无法实现。我安装了在https://www.npmjs.com/package/facebook-chat-api处找到的包,并成功尝试了它,并为示例提供了帮助(face.js,并且我已使用“ node face.js”运行它) :
var login = require("facebook-chat-api");
login({email: "[email protected]", password: "XXXXXX"}, function(err,api) {
if(err) return console.error(err);
var yourID = "000000000000000";
var msg = {body: "Hey! My first programmatic message!"};
api.sendMessage(msg, yourID);
});
在为用户设置了正确的ID之后,它可以正常工作并发送消息而没有任何缺陷。然后我也安装了一个称为“ locus”(https://www.npmjs.com/package/locus)的REPL ,因为我想在消息发送后停止node.js脚本,并从中发送另一个REPL命令行。因此,我的脚本如下:
var login = require("facebook-chat-api");
var locus = require('locus')
login({email: "[email protected]", password: "XXXXXX"}, function(err,api) {
if(err) return console.error(err);
var yourID = "000000000000000";
var msg = {body: "Hey! My first programmatic message!"};
api.sendMessage(msg, yourID);
eval(locus);
});
不幸的是,我的第二个脚本无法按预期工作。我确实收到了“位置” REPL提示,但是直到我使用命令“ quit”退出REPL时,才发送facebook聊天消息。我想在消息发送后立即停止脚本,我想获取一个REPL promt,然后在可能的情况下再次从REPL调用“ api.sendMessage”。我应该怎么做,或者如何重组脚本以使其正常工作。也许将匿名函数放到一个真正的命名函数中,但是我不知道如何正确地做到这一点。
我进行了一个小型测试,将setTimeout用于异步请求,并在您仍处于场所时伪造发送请求。
这是代码:
var locus = require('locus');
function login () {
setTimeout(function () {
console.log('message sent');
},2000);
}
login();
eval(locus);
这是我输入一些命令的控制台。
——————————————————————————————————————————————————————————————————————————
3 : function login () {
4 : setTimeout(function () {
5 : console.log('message sent');
6 : },2000);
7 : }
8 :
9 : login();
10 :
ʆ: message sent // 2 seconds after the repl opened the first message sent
typeof login
'function' // locus is aware of the login function
ʆ: login();
login(); // run the login function
undefined
ʆ: message sent // the message was (fake) sent without quitting
login(); // test a second send
undefined
ʆ: message sent // another message was sent.
如果上面的代码显示了您期望的行为,则您的代码可能是:
var login = require("facebook-chat-api");
var locus = require('locus');
login({email: "[email protected]", password: "XXXXXX"}, loginHandler);
eval(locus);
function loginHandler (err,api) {
if(err) return console.error(err);
var yourID = "000000000000000";
var msg = {body: "Hey! My first programmatic message!"};
api.sendMessage(msg, yourID);
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句