I am trying to make a GET request to twilio to get the data from a specific channel after that I need make some changes to it and then post it again.
I am completely new working with js I will appreciate any advice I am not using Twilio SDK
So far I make this.. but It doesn't make the post request
function modifyChannel(sid, json) {
console.log(json);
return new Promise((resolve, reject) => {
let newJson = JSON.parse(json.attributes);
newJson.task_sid = null;
json.attributes = JSON.stringify(newJson);
resolve(json);
})
}
function postChannel(sid, json) {
axios({
method: 'post',
url:`https://chat.twilio.com/v2/Services/${DEV_CREDENTIAL.programmableChatSid}/Channels/${sid}`,
auth: {
username: DEV_CREDENTIAL.account,
password: DEV_CREDENTIAL.token
},
data: {
json
}
});
}
axios({
method: 'get',
url:`https://chat.twilio.com/v2/Services/${DEV_CREDENTIAL.programmableChatSid}/Channels/${channel_sid}`,
auth: {
username: DEV_CREDENTIAL.account,
password: DEV_CREDENTIAL.token
}
})
.then(response => {
return modifyChannel(channel_sid, response.data);
}).then(jsonModified => { postChannel(channel_sid, jsonModified); })
.catch(err => console.log(err));
Twilio developer evangelist here.
I think the issue is that you are passing data: { json } when you make the post request. That's going to expand to: data: { json: { THE_ACTUAL_DATA }} where you just want data: { THE_ACTUAL_DATA }. So, remove the json key from there.
You can also simplify things with your data manipulation. You aren't doing anything asynchronous in your modifyChannel function, so there's no need to return a Promise.
Try the following instead:
function modifyChannel(sid, json) {
let newJson = JSON.parse(json.attributes);
newJson.task_sid = null;
json.attributes = JSON.stringify(newJson);
return json;
}
function postChannel(sid, json) {
axios({
method: "post",
url: `https://chat.twilio.com/v2/Services/${DEV_CREDENTIAL.programmableChatSid}/Channels/${sid}`,
auth: {
username: DEV_CREDENTIAL.account,
password: DEV_CREDENTIAL.token,
},
data: json,
});
}
axios({
method: "get",
url: `https://chat.twilio.com/v2/Services/${DEV_CREDENTIAL.programmableChatSid}/Channels/${channel_sid}`,
auth: {
username: DEV_CREDENTIAL.account,
password: DEV_CREDENTIAL.token,
},
})
.then((response) => {
const jsonModified = modifyChannel(channel_sid, response.data);
postChannel(channel_sid, jsonModified);
})
.catch((err) => console.log(err));