简体   繁体   English

如何在hubot中编写脚本?

[英]How to script in hubot?

The question is: I joined in a website and got a script to use as bot.问题是:我加入了一个网站并获得了一个用作机器人的脚本。 I'm using hubot by the way.顺便说一句,我正在使用hubot。

The code is:代码是:

var myHeaders = new Headers();
myHeaders.append("Authorization", "Bearer xxxxxxxxxxxxxxxxxxx");
myHeaders.append("Content-Type", "application/json");
myHeaders.append("Cookie", "__xxxxxxxxxxxxxxxxxxxx");

var raw = JSON.stringify({"language":"pt_br","text":"sim"});

var requestOptions = {
  method: 'POST',
  headers: myHeaders,
  body: raw,
  redirect: 'follow'
};

fetch("https://yyyyyy/v2/parse/", requestOptions)
  .then(response => response.text())
  .then(result => console.log(result))
  .catch(error => console.log('error', error));

When I start the bot, I get this error:当我启动机器人时,我收到此错误:

*Headers is not defined
at Object. (/home/ubuntu/hubot-rocketchat-boilerplate/scripts/leadtest.js:1:17)
at Module._compile (internal/modules/cjs/loader.js:1200:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:1220:10)
at Module.load (/home/ubuntu/hubot-rocketchat-boilerplate/node_modules/coffeescript/lib/coffeescript/register.js:53:36)
at Function.Module._load (internal/modules/cjs/loader.js:937:14)
at Module.require (internal/modules/cjs/loader.js:1089:19)
at require (internal/modules/cjs/helpers.js:73:18)
at Robot.loadFile (/home/ubuntu/hubot-rocketchat-boilerplate/node_modules/hubot/src/robot.js:360:22)
at /home/ubuntu/hubot-rocketchat-boilerplate/node_modules/hubot/src/robot.js:383:52
at Array.map (:null:null)
at Robot.load (/home/ubuntu/hubot-rocketchat-boilerplate/node_modules/hubot/src/robot.js:383:35)
at RocketChatBotAdapter.loadScripts (/home/ubuntu/hubot-rocketchat-boilerplate/node_modules/hubot/bin/hubot.js:115:9)
at Object.onceWrapper (events.js:421:28)
at RocketChatBotAdapter.emit (events.js:315:20)
at /home/ubuntu/hubot-rocketchat-boilerplate/node_modules/hubot-rocketchat/index.js:80:14
at processTicksAndRejections (internal/process/task_queues.js:97:5)*

I got the problem is in the first line of the code, what is header and what should I do?我得到的问题是在代码的第一行,什么是 header,我该怎么办? What I'm missing?我错过了什么? Thank you guys.感谢你们。

You created a Header object in the first line of code as:您在第一行代码中创建了 Header object,如下所示:

var myHeaders = new Headers();

Since the program is unable to find the Headers() object in the program, it is not able to initialize it.由于程序在程序中找不到 Headers() object,所以无法对其进行初始化。 Thus giving the Headers not defined error.从而给出Headers not defined错误。

Moving on to the next question: What is headers?继续下一个问题:什么是标题? :

The Headers interface of the Fetch API allows you to perform various actions on HTTP request and response headers. Fetch API 的 Headers 接口允许您对 HTTP 请求和响应标头执行各种操作。 You can refer this documentation to read more about it.您可以参考此文档以了解更多信息。

Finally, this issue is already addressed here , kindly refer it.最后,这个问题已经在这里解决了,请参考。 However, if you want your answer here, then the workaround suggested is:但是,如果您想在这里得到答案,那么建议的解决方法是:

const fetch = require('node-fetch');
global.fetch = fetch
global.Headers = fetch.Headers;

This is done because Headers() is a native construct to Fetch API and you will probably need to polyfill the Fetch API.这样做是因为Headers()是 Fetch API 的本机构造,您可能需要对 Fetch API 进行 polyfill。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM