Unable to get attributes value from inbound MMS "messageAdded" event's payload - twilio

const manager = Manager.getInstance()
manager.chatClient.on('messageAdded', payload => {
console.log('payload', payload) // it is correct
console.log('payload state attributes', payload.state.attributes)
})
Attributes are correct on chrome's console log
{
"body": "",
"media": null,
"attributes": {
"media": "https://api.twilio.com/2010-04-01/Accounts/ACfba1ba518fcc2321c11142f4041efa23/Messages/MM762e5ab98a2adde2970303db9ab6f4ba/Media/ME77ada464c9d50d6441501abf7bbd45c3",
"mediaType": "image/jpeg"
},
}
But with payload.state.attributes, the attributes changed to:
{
"proxied": true
[[Prototype]]: Object
}

Finally, figured it out after reading the Twilio flex "source code" from "node_module". If reason of "attributes" included from "updateReasons", then we can get the { media, mediaType } from "message.state.attributes".
I cannot find this mentioned anywhere from Twilio's documents.
manager.chatClient.on('messageUpdated', ({ message, updateReasons }) => {
console.log('message', message)
console.log('updateReasons', updateReasons)
})

Related

GUPSHUP send template message with attachment

I am trying to send a template message via gupshup using their unified single sendmessage API endpoint https://api.gupshup.io/sm/api/v1/msg
We have a template message approved with the relevant wording in it, and specified that there should be an attachment. When we try and send the attachment it fails with the following message, (1005) Message sending failed as user is inactive for session message and template did not match
Within the message element which is encoded JSON I have tried the a couple of options, all of which reject with the same message as above.
innerJson = { "isHSM": "true", "type": "file", "url": fileURL + location, "filename": filename, "text": message }
innerJson = { "isHSM": "true", "type": "file", "url": fileURL + location, "filename": filename, "caption": message }
innerJson = { "type": "file", "url": fileURL + location, "filename": filename, "text": message }
innerJson = { "type": "file", "url": fileURL + location, "filename": filename, "caption": message }
Can't find any explicit examples in the gupshup documentation of how to do this.
I send you a link with the correct endpoint for sending templates.
Also here's an example of how to use it in node.js:
const sendTemplateWhatsapp = (templateId, phone, media, callbackFunc) => {
innerJson = { "type":"file","url":"your_url" }
const params = new URLSearchParams()
params.append('channel', 'whatsapp');
params.append('source', waba_number);
params.append('destination', phone);
params.append('src.name', waba_name);
params.append('template', templateId);
params.append('message', JSON.stringify(innerJson));
const config = {
headers: {
'apikey': 'your_apikey',
'Content-Type': 'application/x-www-form-urlencoded'
}
}
axios.post('http://api.gupshup.io/sm/api/v1/template/msg', params, config)
.then((result) => {
console.log('Template sent to whatsapp');
callbackFunc(result.data);
})
.catch((err) => {
console.log(err);
callbackFunc(err);
})
}
Hope this helps

I am trying to add forwarding rule to the user's mailbox but it is not working asexpected

I am using graph API to add message rule which is forward a mail from user's inbox. Rule is getting added but mails are not forwarding to specified id.
Here are some details:
var data = {
"displayName": "From partner",
"sequence": 1,
"isEnabled": true,
"conditions": {
"isAutomaticForward": true
},
"actions": {
"forwardTo": [
{
"emailAddress": {
"name": "recipient name ",
"address": "email address"
}
}
],
"stopProcessingRules": true
}
}
axios.post("https://graph.microsoft.com/v1.0/users/{userId}/mailFolders/inbox/messageRules", data,
{
headers: {
"Authorization": "Bearer " + access_token
}
}
)
.then(response => {
console.log(response.data)
})
.catch(err => {
console.log(err.response)
})
Response is as expected but mails are not forwarding.
I tried the above payload, steps and it works for me!!
(1) Create a new rule using Graph API
POST https://graph.microsoft.com/v1.0/me/mailFolders/inbox/messageRules
Content-type: application/json
{
"displayName": "From partner",
"sequence": 2,
"isAutomaticForward": true,
"actions": {
"forwardTo": [
{
"emailAddress": {
"name": "Alex Wilbur",
"address": "AlexW#contoso.onmicrosoft.com"
}
}
],
"stopProcessingRules": true
}
}
(2) Test whether the rule is working or not.
Result: It's working as expected
(3) Check that the above rule shows or not (as part of Outlook.office.com or Outlook UI's rule section)
Adding a snapshot that i captured from Outlook.office.com, mailbox settings!!

Send a Whatsapp/SMS message with info obtained from a user with Autopilot

I've made a simple bot that gives information and collects some data from the users that use it. For example a user can ask for an appoinment and give his cellphone. What I'm trying to do is send a WA or a SMS message to a third number with the information that the bot collected. This is a sample of the Task code for the collect part
"collect": {
"name": "schedule_appt",
"questions": [
{
{
"question": "Let us your phone number and an expert will get in touch with you",
"name": "appt_phone_number",
"type": "Twilio.PHONE_NUMBER"
}
]
Now, I want to send a WA or SMS message with the value of "Twilio.PHONE_NUMBER" to another number. Is it possible to do with the options that twilio give in the bot creator or should I create some custom code for this situation? If so, which would be the best way to do it? A PHP app, or something Web developed?
I'm kinda lost so any advice would be appreciated.
Thanks!
You can do something like this:
{
"actions": [
{
"collect": {
"name": "schedule_appt",
"questions": [
{
"question": "Let us your phone number and an expert will get in touch with you",
"name": "appt_phone_number",
"type": "Twilio.PHONE_NUMBER"
}
],
"on_complete": {
"redirect": {
"method": "POST",
"uri": "https://xyz.twil.io/sostub"
}
}
}
}
]
}
Twilio Function (destination for URL:https://xyz.twil.io/sostub match to your unique Twilio Function domain)
exports.handler = async function(context, event, callback) {
let twilioClient = context.getTwilioClient();
let memory = JSON.parse(event.Memory);
console.log("User Identifier: "+ event.UserIdentifier);
console.log("Task: "+ event.CurrentTask);
console.log(event.CurrentInput);
let message = "Your Message Has been Sent!";
let responseObject = {
"actions": [
{
"say": message
}]
};
let sendSMS = () => {
try {
let result = twilioClient.messages
.create({
body: `Appointment Scheduled, Mobile Phone ${event.CurrentInput}`,
to: '+14075551212',
from: '+15095551212',
});
return result;
} catch(e) {
console.log(e);
callback(e);
}
};
let result = await sendSMS();
callback(null, responseObject);
};

Jira API: Add Comment Using Edit Endpoint

Jira has a an /edit endpoint which can be used to add a comment. There is an example in their documentation that suggests this input body to accomplish this:
{
"update": {
"comment": [
{
"add": {
"body": "It is time to finish this task"
}
}
]
}
}
I create the exact same input in my Java code:
private String createEditBody() {
JsonNodeFactory jsonNodeFactory = JsonNodeFactory.instance;
ObjectNode payload = jsonNodeFactory.objectNode();
ObjectNode update = payload.putObject("update");
ArrayNode comments = update.putArray("comment");
ObjectNode add = comments.addObject();
ObjectNode commentBody = add.putObject("add");
commentBody.put("body", "this is a test");
return payload.toString();
}
but when I send this PUT request I get an error saying that the "Operation value must be of type Atlassian Document Format"!
Checking the ADF format it says that "version", "type" and "content" are required for this format. So although their documentation example doesn't seem to be ADF format, I'm trying to guess the format and change it. Here's what I accomplished after modifying my code:
{
"update": {
"comment": [
{
"add": {
"version": 1,
"type": "paragraph",
"content": [
{
"body": "this is a test"
}
]
}
}
]
}
}
the add operation seems to be an ADF but now I get 500 (internal server error). Can you help me find the issue?
Note that the above example from Atlassian documentation is for "Jira Server Platform" but the instance I'm working with is "Jira Cloud Platform" although I think the behaviour should be the same for this endpoint.
after tinkering with the input body, I was able to form the right request body! This will work:
{
"update": {
"comment": [
{
"add": {
"body": {
"version": 1,
"type": "doc",
"content": [
{
"type": "paragraph",
"content": [
{
"type": "text",
"text": "this is a test"
}
]
}
]
}
}
}
]
}
}
The annoying things that I learned along the way:
Jira's documentation is WRONG!! Sending the request in their example will fail!!
after making a few changes, I was able to get 204 from the endpoint while still comment was not being posted! And I guessed that the format is not correct and kept digging! But don't know why Jira returns 204 when it fails!!!

Twilio autopilot redirect not working properly

Im having a problem in getting Autopilot redirect to work
After redirecting to my POST url it stops saying anything.
Below is my code:
{
"actions": [
{
"collect": {
"name": "password_reset_collect",
"questions": [
{
"question": "I will perform password reset to your account. Do you wish to continue?",
"name": "continue",
"type": "Twilio.YES_NO"
}
],
"on_complete": {
"redirect": "https://88fb4b1a.ngrok.io/Voice/Post"
}
}
},
{
"remember": {
"action_query": "password_reset"
}
}
]
}
and in my POST url it returns JSON result using return Content() as follows
var response = "{\"actions\": [{\"say\": {\"speech\": \"Thank you! Have a good day\" }},{\"listen\": true }]}";
return Content(response, "application/json");
result:
{
"actions": [
{
"say": {
"speech": "Thank you! Have a good day"
}
},
{
"listen": true
}
]
}
It never say what I specified in the on JSON and the call end
What am I doing wrong?
Twilio developer evangelist here.
I'd try putting Remember before Collect, as mentioned on the Actions docs page.

Resources