Mattermost cannot open dialog - mattermost

I am not sure what I am doing wrong, but I cant seem to open the dialog, but I am getting a {status: 'OK'} response...
Mattermost Version: 5.13.2
The post from the slash command is ephemeral
My slash command is working fine, and so is the interactive button (this is what should trigger the dialog to open).
Here is my code for the dialog to trigger. This req param is being passed from the action from clicking a button.
module.exports.addEmail = (req) => {
let body = req.body;
let data = JSON.stringify({
trigger_id : body.trigger_id,
url : resolve(config.MM_ACTION_URL, '/mattermost/dialog'),
dialog : {
title : 'Email',
callback_id : 'add_email',
elements : [
{
display_name : 'Email',
name : 'email',
type : 'text',
subtype : 'email',
placeholder : 'placeholder#example.com'
}
]
}
});
mmRequest('post', 'actions/dialogs/open', data)
.then((res) => {
console.log(res);
})
.catch((error) => console.log(error));
};
On the server side, this is my code to add a little more context to my workflow.
app.post('/mattermost/actions', (req, res) => {
// console.log(req.body);
let action = req.body.context.action;
if (action === 'add_email') {
dialog.addEmail(req);
}
});
app.post('/mattermost/dialog', (req, res) => {
console.log(req.body);
res.sendStatus(200);
});
And the function that is making the API call to the mattermost server is
module.exports.mmRequest = (method, url, data = {}) => {
let c = {
method : method,
baseURL : resolve(config.MATTERMOST_URL, 'api/v4/'),
url : url,
data : data,
headers : {
Authorization : `Bearer ${config.MM_ACCESS_TOKEN}`
}
};
return new Promise((resolve, reject) => {
Axios(c)
.then((res) => {
resolve(res.data);
})
.catch((error) => reject(error.response));
});
};
In the console.log(res), I get a status OK message, but the dialog doesnt open.. What am I doing wrong?

Related

How to log HTTP response header value for all cypress requests?

One of my ideas would be to overwrite the request command, but I don't know how to handle the response object.
A snippet I already have:
Cypress.Commands.overwrite(
'request',
(
originalFn: Cypress.CommandOriginalFn<'request'>,
options: Partial<Cypress.RequestOptions>
): void | Cypress.Chainable<Cypress.Response<unknown>> => {
return originalFn(options);
}
);
My other idea would be to intercept all requests, but there are already interceptors added and you can not have two for one request.
beforeEach(() => {
cy.intercept(
{
url: '*/**',
},
req => {
// tried with 'after:response' too
req.on('response', res => {
cy.log(`${res.headers['x-custom-header']}`);
});
}
);
});
Is there any other way to log a custom header value for all request?
My final working solution was to add this code to /support/index.ts
beforeEach(() => {
cy.intercept({ url: '*', middleware: true }, req => {
req.on('after:response', (res => {
const customHeaderKey = 'x-custom-header';
const customHeaderValue = res.headers[customHeaderKey];
if (customHeaderValue) {
const message = JSON.stringify({ [customHeaderKey]: customHeaderValue });
Cypress.log({ message }).finish();
}
}));
});
});

How do you make a post request via Fetch in Nativescript?

I have a server.js file. With a post request (/login). It looks like this:
require('dotenv').config();
const express = require('express');
const mysql = require('mysql')
const app = express();
const PORT = process.env.PORT || 3000
app.listen(PORT, console.log(`Server started on port ${PORT}`))
app.post('/login', (req, res) => {
console.log("login")
console.log(req);
})
I also have a function in NativeScript that is supposed to make a post request (where the fetch is) when a button is pressed. It looks like this:
export function onLoginButtonTap() {
console.log("login button tapped")
const frame = Frame.getFrameById("mainFrame");
// TODO: user authentication
var userEmail = `${bindingContext.get('email')}`;
var userPassword = `${bindingContext.get('password')}`;
// make sure fields are filled out
if (userPassword === "" || userEmail === "") {
alert({
title: "Login Error",
message: "One or more fields is empty. Please fill in every field.",
okButtonText: "OK",
})
} else {
// TODO: post request (send user input to backend for verification)
console.log("here1")
const data = {userEmail, userPassword};
const options = {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify(data)
}
fetch("http://localhost:3000/login", options);
// if user is in database change page to home
frame.navigate("pages/home/home-page");
}
// navigates to home page without requiring login info (for testing)
frame.navigate("pages/home/home-page");
}
The post request doesn't work. I would like the console.logs in the server file to print the request. I think the problem is how I wrote the post request? Currently nothing happens.
Fetch return a promise, you need to resolve it to actually make the POST request. Try the following block of code.
fetch('http://localhost:3000/login', options)
.then((response) => response.json())
.then((result) => {
console.log('Success:', result);
})
.catch((error) => {
console.error('Error:', error);
});

Issue in uploading files with react native with axios

I am trying to upload files to server in react-native application with axios as http handler.
My code stands as:
let promises = [];
const body = new FormData();
console.log(body);
body.append('device_name', this.state.deviceInfo.DeviceName);
body.append('device_id', this.state.deviceInfo.DeviceID);
body.append('device_ip', this.state.deviceInfo.DeviceIP);
body.append('device_os', this.state.deviceInfo.DeviceOS);
body.append('upload_type', 'user');
body.append('user_name', user.Email);
body.append('file1', {
uri: this.state.newImageUrl.uri,
name: 'test.jpg',
type: 'image/jpg',
});
promises.push(
apiUploadDocs(body)
.then(res => {
profileImageName = res[0].NewFileName;
})
.catch(err => {
console.log('this error', err);
}),
);
My apiUploadDocs is as :
export const apiUploadDocs = body => {
return new Promise((resolve, reject) => {
axios
.post(ApiRoutes.uploadDocs, body,{headers:{'content-Type': `multipart/form-data`}})
.then(res => {
console.log('upload success');
console.log(res);
})
.catch(err => {
console.log('upload error', err);
if (err.response) {
}
reject(Constant.network.networkError);
});
});
};
Every assigned variable has correct values upon logging and the api is working good when I try to upload from Postman.
But this snippet here results in an error which is undefined when logged.
I have tried trimming the 'file://' from the uri, as suggested by some answers here in stackoverflow.
I cant figure it out. Can you help me finding whats wrong here??
PS: The body when logged is:
{
"_parts":[
[
"device_name",
"sdk_gphone_x86"
],
[
"device_id",
"xxxxxxxxxxxxx"
],
[
"device_ip",
"10.0.2.xx"
],
[
"device_os",
"goldfish_x86"
],
[
"upload_type",
"user"
],
[
"user_name",
"xxxxx#gmail.com"
],
[
"file1",
[
"Object"
]
]
]
}
if it is of any reference.
I've found a link to uploading image in react-native.
https://aboutreact.com/file-uploading-in-react-native/
This might be of some help to you.
let uploadImage = async () => {
//Check if any file is selected or not
if (singleFile != null) {
//If file selected then create FormData
const fileToUpload = singleFile;
const data = new FormData();
data.append('name', 'Image Upload');
data.append('file_attachment', fileToUpload);
let res = await fetch(
'http://localhost//webservice/user/uploadImage',
{
method: 'post',
body: data,
headers: {
'Content-Type': 'multipart/form-data; ',
},
}
);
let responseJson = await res.json();
if (responseJson.status == 1) {
alert('Upload Successful');
}
} else {
//if no file selected the show alert
alert('Please Select File first');
}
};

async / await is not waiting for the response

I am requesting an api using POST method. but after calling the RemoteRequest function using await. It's not waiting for the response return. It directly executes the remaining code in the register.js by this im getting the status = undefind in the console.log.
register.js
const DeviceUniqueId = DeviceInfo.getUniqueId();
const requestBody = { phone: phone, username: username };
const status = await RemoteRequest(URLs.APP_REGISTER,
'POST', DeviceUniqueId, requestBody);
console.log('status====>', status);
this.setState({
loading : false
});
remotereuqest.js
export const RemoteRequest = async (url, method, DeviceUniqueId, requestbody) => {
console.log(url, method, DeviceUniqueId, requestbody);
NetInfo.fetch().then((state) => {
if (state.isConnected) {
fetch(url, {
method : method,
headers : {
Accept : 'application/json',
'Content-Type' : 'application/json',
DEVICEID : DeviceUniqueId,
'Accept-Charset' : 'utf-8'
},
body : JSON.stringify(requestbody)
})
.then((response) => {
console.log('reponse=====>', response);
return response.json();
})
.then((responseData) => {
console.log(responseData);
if (responseData.status == 'OK' && responseData.code == 200) {
return responseData.code;
}
return null;
})
.catch((error) => {
console.log(error);
if (error.message == 'Network request failed') {
showMessage({
floating : true,
message : 'Connection error',
description : 'you have no Internet Connection',
type : 'alert',
backgroundColor : 'red'
});
return null; //503
}
else {
showMessage({
floating : true,
message : 'Internal Server Error',
description : 'please try again after some time',
type : 'alert',
backgroundColor : 'red'
});
throw error;
return null;
}
})
.done();
}
else {
showMessage({
floating : true,
message : 'Connection error',
description : 'you have no Internet Connection',
type : 'alert',
backgroundColor : 'red'
});
return null; //503
}
});
};
You need to await for all promises within the function, otherwise they still get executed asynchronously. Something like this:
await NetInfo.fetch().then( async (state) => {
if (state.isConnected) {
await fetch(url, {
...
}
}
});
When you use .then() the code after it gets executed immediately, so instead you should await the responses and then do the work without .then()-s.
const state = await NetInfo.fetch();
if (state.isConnected) {
const response = fetch(url, ...);
console.log(response);
...
}

Firebase Cloud Functions Call : error : Object message : "Bad Request" status : "INVALID_ARGUMENT"

first of all i am working with react-native
i wanted to use Custom Claims on my project since it seems to fit the role distribution i expect to use on my app.
after setting my app following the documentation i succeed on creating some functions
but, here's the thing, when i try to call a function by fetching the endpoint i always get this error :
in the console
error
:
Object
message
:
"Bad Request"
status
:
"INVALID_ARGUMENT"
in firebase console
addAdminRoleTest Request body is missing data. { email: 'dev#test.com' }
i couldn't find any answer to that except that i send wrong information from my fetch but i don't understand why.
i even tried to simplify my function only to get the data i sent but i had the exact same error
find below my cloud function & the calling method :
functions/index.js
exports.addAdminRole = functions.https.onCall((data, context) => {
// get user
return admin.auth().getUserByEmail(data.email).then(user => {
// if not already (admin)
if(user.customClaims && (user.customClaims).admin === true) {
return;
}
// add custom claim (admin)
return admin.auth().setCustomUserClaims(user.uid, {
admin: true
});
}).then(() => {
return {
message: `Bravo : ${data.email} fait partie de l'équipe Admins`
}
}).catch(err => {
return err;
});
});
simplified function :
exports.addAdminRoleTest = functions.https.onCall(data => {
console.log("parse data : "+JSON.parse(data));
return (
JSON.parse(data)
);
});
adminScreen.js
function httpAddAdminRole() {
const initRequest = {
method: 'POST',
headers: {
Accept: 'application/json',
'Content-Type': 'application/json'
},
body:JSON.stringify({
email: 'dev#test.com'
})
}
console.log(initRequest);
return fetch('https://us-central1-my-project.cloudfunctions.net/addAdminRole', initRequest)
.catch(err => console.log(err))
.then(res => res.json())
.then(parsedRes => {
console.log(parsedRes);
});
}
in the end this was mostly json knowledge that missed me
my body should have data included
here's the answer i came to :
functions/index.js
exports.addAdminRole = functions.https.onCall((data, context) => {
const dataParsed = JSON.parse(data);
// get user
return admin.auth().getUserByEmail(dataParsed.email).then(user => {
// if not already (admin)
if(user.customClaims && (user.customClaims).admin === true) {
console.log(dataParsed.email + " is already an Admin");
return;
}
// add custom claim (admin)
return admin.auth().setCustomUserClaims(user.uid, {
admin: true
});
}).then(() => {
return {
message: `Bravo : ${dataParsed.email} is now an Admin`
}
}).catch(err => {
return err;
});
});
adminScreen.js
function httpAddAdminRole(mail) {
const initRequest = {
method: 'POST',
headers: {
Accept: 'application/json',
'Content-Type': 'application/json'
},
body:JSON.stringify({
data:JSON.stringify({
email: mail
})
})
}
console.log(initRequest);
return fetch('https://us-central1-my-project.cloudfunctions.net/addAdminRole', initRequest)
.catch(err => console.log(err))
.then(res => res.json())
.then(parsedRes => {
console.log(parsedRes);
});
}