Why is my slack app posting only ephemeral messages? [duplicate] - ruby-on-rails

Using Serverless and NodeJS, I have a Slack command set up like:
/myCommand doStuff
When I type /myCommand doStuff, the Slack output does this:
/myCommand doStuff
The content of the actual response I want shown goes here.
What I want to do is only have this:
The content of the actual response I want shown goes here.
without the /myCommand doStuff getting echoed.
How do I prevent that from happening?
Update - adding some code
Here's the actual command:
module.exports = () => {
return new Promise(function(fulfill) {
fulfill({
response_type: 'in_channel',
text: 'some testing text
});
});
};
Here's the handler:
module.exports.handler = (event, context, callback) => {
var response = {
statusCode: 200,
body: JSON.stringify(myCommand()),
};
callback(null, response);
};

When you are replying with
"response_type": "in_channel"
the reply is visible to all users in a channel and it will always copy the command back into the channel. This can not be turned off.
When you are replying with
"response_type": "ephemeral"
it is only visible to the user and the command will not be copied back. That is also the default, so you must be using in_channel in your script.
See here for the official documentation on that topic.

Related

Slack API: Send a message verbatim (escaping asterisks, colons)

I'm using the Slack JS SDK's chat.postMessage call to post messages to a Slack channel. I'm forwarded text from another source, and I'd like for that text to be posted verbatim.
But I can't figure out how to get Slack to not interpret *, _, and :.
I've tried passing in mrkdwn: false and parse: 'full', but the messages always seem to come out the same:
const slackClient = new slack.WebClient(slackApiToken);
await slackClient.chat.postMessage({
channel: targetChannel,
text: '*hello* _bye_ :smile:',
});
await slackClient.chat.postMessage({
channel: targetChannel,
text: '*hello* _bye_ :smile:',
parse: 'full',
});
await slackClient.chat.postMessage({
channel: targetChannel,
text: '*hello* _bye_ :smile:',
mkrdwn: false,
});
What I want to see in the Slack app:
*hello* _bye_ :smile:
What I see in the Slack app (for all three chat.postMessage calls):
hello bye 😄
Is there a way to get Slack to not interpret those characters as being special?
Note: I do not want to use a inline code or code block formatting for my message. I just want it to appear in plain text.
Instead of using text parameter, you can use blocks parameter to send your 'plain text'
https://api.slack.com/methods/chat.postMessage#blocks_and_attachments
Example:
await slackClient.chat.postMessage({
channel: targetChannel,
blocks : [{
type: 'section',
text: {
type: 'plain_text',
text: '*This* is a _plain_ text section block. :smile:',
emoji: false
}
}]
});
Looks like this behaviour is a known bug with chat.postMessage. I'd recommend reporting this to feedback#slack.com so that the team can get the bug prioritized.

Twilio Flex - Send Call To IVR After Reject

Using this plugin as a reference, I have Flex configured to be able to send a call to a Twilio Studio IVR, after an agent has accepted a call.
I'd like to be able to send an incoming call back to Studio when an agent rejects a call (i.e. as soon as they click the reject button). I'm trying to do this by adding a listener to the plugin's init method:
flex.Actions.addListener("afterRejectTask", async (payload, abortFunction) => {
let url: string = payload.task.attributes.transferToIvrUrl;
let menu: string = 'hangup';
await request(url, { CallSid: payload.sid, menu });
});
See here for the full context -- I'm pretty much using that exact code, with the addition of this listener.
I'm getting this error message, and the call is not transferred anywhere.
twilio-flex.unbundled-react.min.js:1574 Error on afterRejectTask: SyntaxError: Unexpected token < in JSON at position 0
Here's additional context from the console, if that's helpful:
Additional info:
The url being requested is a Twilio function, which successfully returns a response like this:
<Response>
<Enqueue workflowSid="WWcc1a650e4175089538d754a6c2e15a98">
<Task>{"transferToIvrUrl": "https://my-twilio-function-service.twil.io/studio-flex-transfer-helper"}</Task>
</Enqueue>
</Response>
Any advice would be appreciated.
Ah, ok, so looking at that plugin I found the example Twilio Function that works with it. From what I can tell, this function is intended to be used in two places, either in Studio to transfer the call to Flex (though I'm not sure it's needed for that) or from Flex to transfer the call back to Studio. The thing that triggers the different response is whether you pass an argument called transferToIVRMenu with the request.
Your current request is not passing that argument, you currently have:
await request(url, { CallSid: payload.sid, menu });
which looks similar to the original plugin's request:
await request(transferToIvrUrl, { CallSid: call_sid, transferToIVRMenu });
The difference is in the second property in the object. When you just pass the name of the variable in an object, it expands to call the property the same name as the variable and set the value to the value within the variable. So the original request expands out to:
await request(transferToIvrUrl, { CallSid: call_sid, transferToIVRMenu: transferToIVRMenu });
but your request only expands to:
await request(url, { CallSid: payload.sid, menu: menu });
So you are passing a parameter called menu not transferToIVRMenu and that triggers the Function on the back end to return TwiML and not to update the call.
To fix this, you can update your plugin code to send the transferToIVRMenu parameter, like:
flex.Actions.addListener("afterRejectTask", async (payload, abortFunction) => {
let url: string = payload.task.attributes.transferToIvrUrl;
let menu: string = 'hangup';
await request(url, { CallSid: payload.sid, transferToIVRMenu: menu });
});

Twilio Studio: Forward SMS conversation log to email

I'm using SMS studio do have a quick chat bot conversation with inbound SMS messages, and would like to forward the conversation log to email after it's complete. I've written a function that uses the SendGrid API to forward SMSes to email. It works independently - ie, if I configure the phone number to run the function immediately as a text comes in, it will email that single SMS input.
However, I'd like to add the function to the end of of Twilio Studio flow, so that it emails the entire log of the conversation to me, once it's over. Once I append the function to the end of the studio flow, it stops working, and I get a failure notice.
Here's the code in the function:
const got = require('got');
exports.handler = function(context, event, callback)
{
const requestBody = {
personalizations: [{ to: [{ email: context.TO_EMAIL_ADDRESS }] }],
from: { email: context.FROM_EMAIL_ADDRESS },
subject: `New SMS message from: ${event.From}`,
content: [
{
type: 'text/plain',
value: event.Body
}
]
}
got.post('https://api.sendgrid.com/v3/mail/send', {
headers: {
Authorization: `Bearer ${context.SENDGRID_API_KEY}`,
'Content-Type': 'application/json'
},
body: JSON.stringify(requestBody)
})
.then(response => {
let twiml = new Twilio.twiml.MessagingResponse();
callback(null, twiml);
})
.catch(err => {
callback(err);
});
};`
Here's the error the debugger returns, if I make this function the last step in a Twilio studio flow:
Error - 81017
Error on Twilio Function response
There was an error in the response back from a Twilio Function attached to the Studio flow.
Possible Causes
Your Function timed out before responding
Your Function returned an error response
Possible Solutions
Your Function must contain a callback.
Make sure you place the Function callback callback(err, response) is placed correctly in your Function code.
If you are using a JavaScript promise, make sure the callback is called in both success and catch blocks.
Your Function responded with an error.
I'm having a hard time figuring out what the error is. Does anyone have any tips?
Thanks!
Jarod from Twilio. I actually wrote an app very similar to this. Code looks good to me. Often when people encounter an error of this nature it is coming from the SendGrid promise. Here are two tips:
You can actually log errors and view responses from the function if you leave the function open while testing it. The logs will be at the bottom. These usually have more information.
Check that you are using an outbound email address that has been whitelisted on your SendGrid account Whitelabel - Sendgrid
Make sure you have added your environment variables to your Functions config. Twilio Functions Configuration - Console
Hope that helps! If not feel free to email support#twilio.com with more questions.

"message" : "Internal server error" issue with Lambda/API Gateway and iOS

I've set up a lambda function and created some GET and POST methods inside the API Gateway which seem to work fine when testing them inside the web application.
I am then trying to call the functions inside an iOS application which is set up using the mobile hub. The functions also work inside the testing facility via the mobile hub perfectly fine, however when I actually test the functions inside the app I get:
"message" : "Internal server error"
I know the error is not much to work from, but I can't figure out a way to get a more detailed error description.
Any ideas?
This may happen because your Lambda function is not set to return a HTTP status code.
Changing from
exports.handler = (event, context, callback) => {
callback(null, 'Hello from Lambda');
};
to
exports.handler = (event, context, callback) => {
callback(null, { statusCode: 200, body: 'Hello from Lambda' });
};
Should fix the issue.
The JSON.stringify() solved my issue. The response.body needs to be in String format and not as JSON. I hope this helps.
exports.sendRes = (body, status = 200) => {
var response = {
statusCode: status,
headers: {
"Content-Type": "application/json"
},
body: JSON.stringify(body)
};
return response;
};
I had the same issue with the following code:
exports.handler = async event => {
console.log("hello world");
return {
statusCode: 200,
body: event
};
};
To fix all I had to do was JSON.stringify() the body.
exports.handler = async event => {
console.log("hello world");
return {
statusCode: 200,
body: JSON.stringify(event), // <-- here
};
};
I had this problem until I click in "Deploy API" under the "Actions" button.
The other possible reason could be the payload/request/response limits on API Gateway (10MB) and/or Lambda (6MB)
None of the above answers worked for me. I was having a permission issue. Below is how I solved it.
Context
This is my lambda function:
exports.handler = function(event, context, callback) {
callback(null, {
statusCode: '200',
body: JSON.stringify({ 'message': 'hello world' }),
headers: {
'Content-Type': 'application/json',
},
});
};
I used terraform to provision api gateway and lambda. I used the example code provided by this blog post.
Diagnosis
In the lambda console I ran a test event on my lambda. As my lambda was super basic I used the hello world test template, named, and saved it. The test return success.
I checked cloudwatch logs, but couldn't find anything of use. I'm new to AWS so wasn't sure if I had to set anything up.
In the api gateway console I ran a test event. I just added Content-Type:application/json to the headers of the event and ran the test. For whatever weird reason the test results returned on the right side of the browser so had to scroll to the right to see them.
I got this result: Execution failed due to configuration error: Invalid permissions on Lambda function
SOLUTION
I checked the basic terraform example for api gateway and lambda integration here and noticed I was missing the aws_lambda_permission resource. This is needed to give permission to api gateway to invoke the lambda function.
For those that aren't using terraform here is a link to the aws docs on how to create the appropriate permissions.
please try to
Give execute lambda permission API Gateway
tick checkbox : Use Lambda Proxy integration
Handle null pointer for query string, headers & body.
I solved the issue by adding "isBase64Encoded": False/True to my lambda response
results = {
"statusCode": 200,
"headers": {"Content-Type": "application/json"},
"body": json.dumps(res),
"isBase64Encoded": False
}
In my case, the issue resolved while adding the integration Response and redeploying API

Slack slash command response_type ephemeral are sent to the entire channel

Ephemeral messages are supposed to be only visible to the user that issued the command. However, in my experience, it's sent to the entire channel.
Am I missing anything?
According to Slack Slash commands documentation, the only attribute needed is to set response_type to ephemeral in the response of Node.js app.
The code in my app looks like this:
var t = {
"response_type": "ephemeral",
"text": "How to use /please"
}
request({
uri: uri,
headers: {
'content-type': 'application/json',
},
method: 'POST',
body: JSON.stringify(t)
}, function (error, response, body) {
return res.status(200).end();
});
Ephemeral messages are only supported in slash commands currently. From your screen capture it looks like you're sending your payload to an incoming webhook. Unfortunately, I can't find the wiki that backs this up, but here's another post with a similar answer: Slack API "attachments" not showing

Resources