简体   繁体   中英

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.

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? What I'm missing? Thank you guys.

You created a Header object in the first line of code as:

var myHeaders = new Headers();

Since the program is unable to find the Headers() object in the program, it is not able to initialize it. Thus giving the Headers not defined error.

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. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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