Use "response_action" to respond to view_submission when using botkit - node.js

I have a simple modal with view pushed using:
const result = await bot.api.views.open({
trigger_id: message.trigger_id,
view: {
"type": "modal",
"submit": {
"type": "plain_text",
"text": "Submit",
"emoji": true
},
"title": {
"type": "plain_text",
"text": "Request",
"emoji": true
},
"blocks": [
{
"type": "input",
"block_id" : "accblock",
"element": {
"type": "plain_text_input",
"action_id": "account_name",
"multiline": false
},
"label": {
"type": "plain_text",
"text": "Account Name",
"emoji": true
}
}
]
}
});
I need to add error to the block if certain value is entered on view_submission. I understand I should send response with the following JSON:
{
response_action: 'errors',
errors: {
"ticket-desc": 'I will never accept a value, you are doomed!'
}
}
But, how do I send it? I've tried using bot.httpBody(). Does this JSON need to be included as the view object? Any help is appreciated.

bot.httpBody() is the method you need.
Make sure that the key to the errors dictionary matches the block_id of the element you are providing an error message for.
In your case this will work:
bot.httpBody({
response_action: 'errors',
errors: {
accblock: 'Your account name is invalid or in use.'
}
})
You do not need to JSON.stringify the response message and indeed the process fails if you do.

Related

Slack modal returning nothing, saying "We had some trouble connecting. Try again?"

The problem is, that upon filling in the modal, my client doesn't do anything and instead returns a "We had some trouble connecting. Try again?"
app.command("/bugreport", async ({ command, ack, say}) => {
try {
await ack();
app.client.dialog.open(
{
"trigger_id": command.trigger_id,
"dialog": {
"callback_id": "bug-report",
"title": "Make an issue",
"submit_label": "Report",
"notify_on_cancel": true,
"elements": [
{
"type": "text",
"label": "Your Name",
"name": "username"
},
{
"type": "select",
"label": "Issue Severity",
"name": "severity",
"options": [
{
"label": "Minor",
"value": "minor"
},
{
"label": "Moderate",
"value": "moderate"
},
{
"label": "Urgent",
"value": "urgent"
},
{
"label": "Critical",
"value": "critical"
}
]
},
{
"label": "Description",
"name": "description",
"type": "textarea",
"hint": "Describe the bug in this section. Be thorough and explain the exact cause of the bug"
},
{
"label": "Bug timeline",
"name": "timeline",
"type": "text",
"placeholder": "When did this issue first appear"
},
{
"label": "Expected Behaviour",
"name": "excpectedBehaviour",
"type": "textarea",
"hint": "Detail what you expected the code to do. Just what it was meant to do."
},
{
"label": "Actual Behaviour",
"name": "actualBehaviour",
"type": "textarea",
"hint": "What actually happened, what was different."
},
{
"label": "Steps to reproduce",
"name": "steps",
"type": "textarea",
"placeholder": "1. [First Step]...\n 2. [Second Step]... \n 3. [And so on]... \n"
},
{
"label": "Additional Information",
"name": "additionalInformation",
"type": "textarea",
"hint": "Anything extra to include."
}
]
}
}
)
} catch (error) {
console.log("err")
console.error(error);
}
});
app.event("dialog_submission", async ({ event, context }) => {
// Extract the values from the submission payload
const { username, severity, description, timeline, expectedBehaviour, actualBehaviour, steps, additionalInformation } = event.submission;
if (event.callback_id === "bug-report") {
// Use the Octokit library to create an issue in the repository
try {
const issue = await octokit.issues.create({
owner: "my-github-username",
repo: currentRepository,
title: `${severity} issue reported by ${username}`,
body: `Description: ${description}
Timeline: ${timeline}
Expected Behaviour: ${expectedBehaviour}
Actual Behaviour: ${actualBehaviour}
Steps to reproduce: ${steps}
Additional information: ${additionalInformation}`
});
console.log(`Created issue ${issue.data.number}`);
} catch (error) {
console.error(`Failed to create issue: ${error.message}`);
}
// Use the respond() function to send a response to the user
try {
await app.client.chat.postMessage({
token: context.botToken,
channel: event.channel.id,
text: "Thank you for your bug report! We will review it and take appropriate action."
});
} catch (error) {
console.error(`Failed to send response: ${error.message}`);
}
}else{
console.log("what")
}
});
My code runs fine, just doesn't return anything when you press submit in the modal, and returns an error to the user. I've tried fixing it and scouring for fixes but I don't know what it is. I am using slack in socket mode.

How can I add unfurl_links in Slack message

In the Slack message format example block below, how can I add unfurl_link option?
{
"type": "section",
"text": {
"type": "mrkdwn",
"text": "New Paid Time Off request from <example.com|Fred Enriquez>\n\n<https://example.com|View request>"
}
}
I'm guessing the snippet you posted is part of the blocks parameter, try adding the unfurl_links parameter with a value of false (outside the blocks):
{
"token": "your-slack-token",
"channel": "#test-channel",
"unfurl_links": false,
"blocks": [{
"type": "section",
"text": {
"type": "mrkdwn",
"text": "New Paid Time Off request from <example.com|Fred Enriquez>\n\n<https://example.com|View request>"
}
}]
}

Slack webhook doesn't accept a JSON payload with a block that has an empty string

I am trying to set up an integration between Zendesk and Slack, where Zendesk sends new notifications to Slack using Slack's Incoming Webhooks. The issue is that under certain circumstances, some Zendesk fields would be empty strings, which are flagged by slack as an invalid_payload. I would love to be able to send the message with an empty string, or at least have a fallback string if the zendesk field is empty.
I have confirmed that populating the empty string, without making any other changes to the payload, results in a successful integration, so it's definitely the empty string blocking the message from being delivered. I have also found this SO thread, but this doesn't seem to work for me. I haven't been able to find any documentation from slack regarding empty strings in the JSON payload either.
So I guess my question is whether or not it's possible to send empty strings in a JSON payload for Slack webhooks at all, and if not, is there a workaround to account for the possibility of an empty string? As I mentioned, this is a Zendesk integration so I don't have the ability to write a script to check for an empty string because it's all happening within Zendesk's dashboard (at least as far as I'm aware).
Here's the overall JSON object that I am trying to send from Zendesk to Slack. The Organization field is the one that has the potential to be blank:
{
"blocks": [
{
"type": "divider"
},
{
"type": "header",
"text": {
"type": "plain_text",
"text": "Ticket Title/Subject"
}
},
{
"type": "context",
"elements": [
{
"type": "mrkdwn",
"text": "*Ticket #_000_*"
}
]
},
{
"type": "context",
"elements": [
{
"type": "mrkdwn",
"text": "*Status: _New_*"
}
]
},
{
"type": "section",
"fields": [
{
"type": "mrkdwn",
"text": "*Requester*"
},
{
"type": "mrkdwn",
"text": "*Organization*"
},
{
"type": "plain_text",
"text": "Client Name"
},
{
"type": "plain_text",
"text": "Organization Name" //Possible empty string
}
]
},
{
"type": "section",
"text": {
"type": "plain_text",
"text": "Message Body Text Here"
}
},
{
"type": "divider"
}
]
}

Unable to post message via the Slack API - getting error 'no_text'

Why am I getting an error 'no_text' ?
The Json below is valid and is taken from the slack documentation example.
In bot.postMessage(channel, '', params) if I populate the second parameter (i.e. replacing ' ' with 'some_text) it prints 'some_text' but without the attachment.
bot.postMessage(channel, 'some_text', params) --> works but the attachment doesn't show up.
const element = {
"text": "Would you like to play a game?",
"response_type": "in_channel",
"attachments": [
{
"text": "Choose a game to play",
"fallback": "If you could read this message, you'd be choosing something fun to do right now.",
"color": "#3AA3E3",
"attachment_type": "default",
"callback_id": "game_selection",
"actions": [
{
"name": "games_list",
"text": "Pick a game...",
"type": "select",
"options": [
{
"text": "Hearts",
"value": "hearts"
},
{
"text": "Global Thermonuclear War",
"value": "war"
}
]
}
]
}
]
}
console.log('JSON.stringify(element): '+JSON.stringify(element));
params = {
icon_emoji: ':r2:',
attachments: JSON.stringify(element)
}
bot.postMessage(channel, '', params).always(function (data) {...}
The issue arises from the lack of a text field in the parameters that is passed to bot.PostMessage. Your params should be
params = {
icon_emoji: ':r2:',
text: "Would you like to play a game?",
response_type: "in_channel",
attachments: element
}
and the element now should start from the actual attachment
const element = [
{
"text": "Choose a game to play",
"fallback": "If you could read this message, you'd be choosing something fun to do right now.",
"color": "#3AA3E3",
"attachment_type": "default",
"callback_id": "game_selection",
"actions": [
{
"name": "games_list",
"text": "Pick a game...",
"type": "select",
"options": [
{
"text": "Hearts",
"value": "hearts"
},
{
"text": "Global Thermonuclear War",
"value": "war"
}
]
}
]
}
]
I met the same issue and found the solution.
The problem is that if only attachments field is added into the payload, it will report no_text error. But if text field is added, slack message will only show the text content.
The solution:
When we want to display attachments, we need to add a basic blocks field instead of text field. Something like
{
"blocks": [
{
"type": "section",
"text": {
"type": "mrkdwn",
"text": "bar"
}
}
],
"attachments": [
{
"color": "#FF0000",
"blocks": [
{
"type": "section",
"text": {
"type": "mrkdwn",
"text": "foo"
}
}
]
}
]
}
If putting the above payload into the Slack build kit, it will be a misleading. That's also why I stuck with the issue.
I would recommend to use chat.postMessage test to debug the payload. It will work like a charm.

Dialogflow Fulfillment LINE custom payload

I am creating Dialogflow fulfillment webhook and use https://github.com/dialogflow/dialogflow-fulfillment-nodejs
I want to use quick reply in LINE and use Suggestion, but it is not working as I want it like this quick reply. Then I decide to try Custom Payload, but it is not working.
My code is like this
function testQuickReplyHandler(agent) {
let payload = {
"messages": [
{
"type": "text",
"text": "Select your favorite food category or send me your location!",
"quickReply": {
"items": [
{
"type": "action",
"action": {
"type": "message",
"label": "Sushi",
"text": "Sushi"
}
},
{
"type": "action",
"action": {
"type": "message",
"label": "Tempura",
"text": "Tempura"
}
},
{
"type": "action",
"action": {
"type": "location",
"label": "Send location"
}
}
]
}
}
]};
agent.add(new Payload('LINE', payload));
}
Am I missing something? I also try removing "messages" field, but also not working
agent.add(new Payload('LINE', payload)); => agent.add(new Payload(agent.ACTIONS_ON_GOOGLE, payload));
Or any payload you like

Resources