I'm trying to code a Hubot listener that would automatically create a JIRA issue from a specific chat dialogue. Here's the example (I used Hubot-Rocketchat boilerplate code from here: https://github.com/RocketChat/hubot-rocketchat-boilerplate)
module.exports = (robot) => {
robot.hear(/^#alerts (.*)/i, (res) => {
const jiraurl="https://jira.example.com/rest/api/2/issue/"
const jiraproject="ALERTS"
text = res.match[1]
data = JSON.stringify({
"fields": {
"project":
{
"key": `#{jiraproject}`
},
"summary": `#{text}`,
"description": "Задача создана автоматически из RocketChat",
"issuetype": {
"name": "Инцидент"
}
}
})
/* res.reply(data) */
res.reply(`Создаю задачу в JIRA: ${text}`)
robot.http(jiraurl)
.header('Content-Type', 'application/json')
.post(data) (err, response, body) =>
if err
res.send `Ошибка: #{err}`
return
})
}
What would be the proper way to do this?
Related
I am trying to GET an event by its id but for some reason i am getting an empty array as result in postman.
This is my route:
import { events } from '../../../db.json';
const handler = async (req , res) => {
const evt = events.filter( (ev) => ev.id === req.query.id );
if(req.method === 'GET'){
return res.status(201).json(evt);
}
return res.status(400).json({ error: 'Event not found' });
}
export default handler;
This is my db.json:
{
"events": [
{
"id": 1,
"title": "Recital coldplay",
"description": "Recital de coldplay en River"
},
{
"title": "Recital metalica",
"description": "Recital de metalica en velez",
"id": 2
},
{
"title": "asdasd",
"description": "asdasdsa",
"id": 3
},
{
"title": "Dave Chappel Standup",
"description": "Dave Chappel standup comedy",
"id": 4
},
{
"title": "test",
"description": "teeesest",
"id": 5
}
]
}
the route is api/events/[id]
Any reason why if in postman i enter a GET route with te following api/events/1 instead of showing the first event data i get []?
You can use req.params.id if your route looks like api/events/:id
If you want to use req.query you can add query parameters like this:
api/events?id=1
// with req.param (api/events/1)
app.get("/api/events/:id", (req, res) => {
const evt = events.filter((ev) => ev.id === req.params.id);
res.json(evt);
});
// with req.query (api/events?id=1)
app.get("/api/events", (req, res) => {
const evt = events.filter((ev) => ev.id === req.query.id);
res.json(evt);
});
i want to render an email of all the users for testing purpose, In fact i have done that using this method. --- {dataFromApi.map((item, i) => {item.email})} but still it didn't work
const [dataFromApi, setDataFromApi] = useState([]);
const URL = 'http://localhost:5000/users'
const requestOptions = {
method: 'GET',
headers: {
'Content-Type': 'application/json'
},
}
const submit = () => {
const data = fetch(URL, requestOptions);
data.then( (userdata) => {
return userdata.json();
}).then( (data) => {
setDataFromApi(data[0]);
}).catch( (err) => {
console.log(err);
})
}
return (
<div className="login">
<h1 className="loginTitle">Choose a Login Method</h1>
<p>{dataFromApi}</p>
<div className="wrapper">
.
.
.
.
.
here is the API response
[
{
"id": 1,
"email": "test1234#gm.com",
"password": null
},
{
"id": 2,
"email": null,
"password": null
},
{
"id": 3,
"email": "test#123.com",
"password": "12345678"
},
{
"id": 4,
"email": "test#231.com",
"password": "12345678"
},
{
"id": 5,
"email": "test#231.com",
"password": "12345678"
},
{
"id": 6,
"email": "test#231.com",
"password": "12345678"
},
{
"id": 7,
"email": "NEWtest#231.com",
"password": "123"
}
]
but getting this error
react_devtools_backend.js:4012 The above error occurred in the component:
and
react-dom.development.js:14887 Uncaught Error: Objects are not valid as a React child (found: object with keys {id, email, password}). If you meant to render a collection of children, use an array instead.
Like Adam said in the comments, you are trying to pack a collection of things into a tag meant for a single thing. You should iterate over the list rendering a thing for each item.
{dataFromApi.map((item, i) => <p key={i}>{item.email}</p>)}
I got the answer why i'm not getting the expected output because in this code
const submit = () => {
const data = fetch(URL, requestOptions);
data.then( (userdata) => {
return userdata.json();
}).then( (data) => {
setDataFromApi(data[0]); // this will store only one object into an array
}).catch( (err) => {
console.log(err);
})
}
here setDataFromApi(data[0]) will store only 1 object and to access the email from the object, we have to use only dataFromApi.email else dataFromApi will give only object which we can't render so that's why it is giving an error.
I’m trying to put a buffer in a request because I have a list of data to import. I want to have success request after one another. The problem I’m encountering is that it waits to upload all data of the request.
Here is the sample data:
[
{
"contacts": "dsds#dsd.com",
"recipient": "dsd#dsd.com",
"date_sent": "07/08/2020 17:05:04",
"subject": "repurchase"
},
{
"contacts": "asd#ret.com",
"recipient": "test#yahoo.com",
"date_sent": "07/10/2020 17:31:51",
"subject": "biz"
},
{
"contacts": "we#sdf.com",
"recipient": "abc#yahoo.com",
"date_sent": "07/09/2020 13:02:54",
"subject": "rock"
}
];
const createEngage = async(body) => {
const BASE_URL = '/api/import'
var requestOptions = {
method: 'POST',
headers: {
'Accept': 'application/json',
"Content-Type": "application/json"
},
body: body
};
fetch(BASE_URL, requestOptions)
.then(response => response.text())
.then(async result => {
console.log(result);
})
.catch(error => console.log('error', error));
}
What you probably want to do is to loop over your data and use async / await to wait at each iteration. Your implementation of your asynchronous function currently does not await anything. Instead it should await the fetch request and the decoding of the body with response.text().
Check the response for errors and wrap the fetch request in a try...catch block. If an error occurs then the catch block will be executed. Otherwise check the response object for any states or errors you want to include.
const data = [
{
"contacts": "dsds#dsd.com",
"recipient": "dsd#dsd.com",
"date_sent": "07/08/2020 17:05:04",
"subject": "repurchase"
},
{
"contacts": "asd#ret.com",
"recipient": "test#yahoo.com",
"date_sent": "07/10/2020 17:31:51",
"subject": "biz"
},
{
"contacts": "we#sdf.com",
"recipient": "abc#yahoo.com",
"date_sent": "07/09/2020 13:02:54",
"subject": "rock"
}
];
const BASE_URL = '/api/import'
/**
* Sends a request for each individual item.
*/
const createEngage = async body => {
const requestOptions = {
method: 'POST',
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json'
},
body
};
try {
const response = await fetch(BASE_URL, requestOptions);
if (!response.ok) {
alert('Your request has failed');
return null;
}
const text = await response.text();
return text;
} catch(error) {
alert('Your request caused an error');
}
};
/**
* Loop over each item and call createEngage.
* Wait for the request to finish and continue.
*/
const createMultipleEngages = async data => {
for (const item of data) {
const result = await createEngage(item); // This will make the loop wait every time.
console.log(result);
}
};
// Call the function and start looping.
createMultipleEngages(data);
I am calling below restAPI using node JS.
I am using below code.
var Request = require("request");
Request.get("http://localhost:8080/ords/hr/rest-v3/item/Sugar", (error, response, body) => {
if (error) {
return console.dir(error);
}
console.log(response.body);
});
It is giving below output which is correct.
{"items":[{"itemid":101,"itemname":"Sugar","itemcost":32.5}],"hasMore":false,"limit":0,"offset":0,"count":1,"links":[{"rel":"self","href":"http://localhost:8080/ords/hr/rest-v3/item/Sugar"},{"rel":"describedby","href":"http://localhost:8080/ords/hr/metadata-catalog/rest-v3/item/item"}]}
How can I access only itemcost, not entire body. I am new to node so not familiar with many things.
Can you please help me on that.
May be it can help you.
var Request = require("request");
Request.get("http://localhost:8080/ords/hr/rest-v3/item/Sugar", (error, response, body) => {
if (error) {
return console.dir(error);
}
let jsonData = JSON.parse(response.body);
let obj= new Object(jsonData);
obj.items.forEach(itemChild => {
let cost=itemChild.itemcost;
console.log(cost);
})
});
you can iterate the response.body.items and get the costs of all items in the items array like this,
var Request = require("request");
Request.get("http://localhost:8080/ords/hr/rest-v3/item/Sugar", (error, response, body) => {
if (error) {
return console.dir(error);
}
console.log(response.body);
const itemCosts = response.body.items.map(item => item.itemcost);
console.log(itemCosts);
});
Considering your input you can simply do this:
let input = {
"items": [
{
"itemid": 101,
"itemname": "Sugar",
"itemcost": 32.5
}
],
"hasMore": false,
"limit": 0,
"offset": 0,
"count": 1,
"links": [
{
"rel": "self",
"href": "http://localhost:8080/ords/hr/rest-v3/item/Sugar"
},
{
"rel": "describedby",
"href": "http://localhost:8080/ords/hr/metadata-catalog/rest-v3/item/item"
}
]
};
let json = JSON.parse(JSON.stringify(input));
json.items.map(item =>{
console.log(item.itemcost)
})
You can find running solution here
I made a messanager chatbot and tried to deploy it for the first time and after fighting with all kinds of errors I did connect my page, app and hooks.
Tough al seems working theres's no response from the bot. This is what i get in the error log.
I send "hi" and nothing back. When i google the response error that comes up, there's no solution that worked for me.
'use strict'
const
express=require('express'),
bodyParser = require('body-parser'),
app=express().use(bodyParser.json()); //creates http server
app.listen(process.env.PORT || 5000, () =>console.log('webhook is listening'));
app.post('/webhook', (req, res) => {
let body=req.body;
if(body.object === 'page'){
body.entry.forEach(function(entry){
//Gets the body of the webhook
let webhook_event=entry.messaging[0];
console.log(webhook_event);
//Gets the sender PSID
let sender_psid=webhook_event.sender.id;
console.log('Sender PSID: ' + sender_psid);
});
res.status(200).send('EVENT_RECEIVED');
}else{
res.sendStatus(404);
}
if(webhook_event.message){
handleMessage(sender_psid, webhook_event.message);
}else if(webhook_event.postback){
handlePostback(sender_psid, webhook_event.postback);
}
});
app.get('/', function (req, res) {
res.send('This is EngiBot Server');
});
app.get('/webhook', (req, res) => {
let VERIFY_TOKEN = "testbot_verify_token"
let mode= req.query['hub.mode'];
let token=req.query['hub.verify_token'];
let challange = req.query['hub.challange'];
if (req.query['hub.verify_token'] === VERIFY_TOKEN) {
res.send(req.query['hub.challenge']);
} else {
res.send('Invalid verify token');
}
if(mode && token){
if(mode==='subscribe' && token === VERIFY_TOKEN){
console.log('WEBHOOK_VERIFIED');
res.status(200).send(challange);
}else{
res.sendStatus(403);
}
}
});
function handleMessages(sender_psid, received_message){
let response;
if(received_message.text){
response = {
"text": 'You sent the message: "${received_message.text}". Now send an image!'
}
}else if(received_message.attachments){
let attachment_url=received_message.attachments[0].payload.url;
response = {
"attachment":{
"type": "template",
"payload":{
"template_type":"generic",
"elements": [{
"title": "Is this the right picture?",
"subtitle": "Tap a button to answer.",
"image_url": attachment_url,
"buttons": [
{
"type": "postback",
"title": "Yes!",
"payload":"yes",
},
{
"type": "postback",
"title": "No!",
"payload": "no",
}
],
}]
}
}
}
}
callSendAPI(sender.psid, response);
}
function handlePostback(sender_psid, received_postback){
let response;
let payload=received_postback.payload;
if(payload==='yes'){
response = {"text": "Thanks!"}
}else if (payload==="no"){
response ={"text": "Oops, try sending another image."}
}
callSendAPI(sender_psid, response);
}
function callSendAPI(sender_psid, response){
let request_body={
"recipient": {
"id": sender_psid
},
"message": response
}
request({
"uri":"",
"qs":{"access_token": PAGE_ACCESS_TOKEN},
"method": "POST",
"json": request_body
}, (err, res, body)=>{
if(!err){
console.log('message sent!')
}else {
console.error("Unable to send message:" + err);
}
});
}
There's a problem in the POST router. The 'webhook_event' is declared inside a foreach block inside a conditional block so its scope is internal to that block. To solve this you shall rewrite your code to match the scopes. This is the wrong router (i added some comments =
app.post('/webhook', (req, res) => {
let body=req.body;
// webhook_event == null -> true
if(body.object === 'page'){
body.entry.forEach(function(entry){
//Gets the body of the webhook
let webhook_event=entry.messaging[0]; // webhook_event declared // webhook_event == null -> false
console.log(webhook_event);
//Gets the sender PSID
let sender_psid=webhook_event.sender.id;
console.log('Sender PSID: ' + sender_psid);
});
res.status(200).send('EVENT_RECEIVED');
if(webhook_event.message){ // ReferenceError cause is not defined
handleMessage(sender_psid, webhook_event.message);
}else if(webhook_event.postback){ // ReferenceError cause is not defined
handlePostback(sender_psid, webhook_event.postback);
}
}else{
res.sendStatus(404);
}
});