print out array without stringify to Slack - node.js

i'm trying to return an array that i'm creating.
the problem is that I don't want to use stringify since it returns brackets.
Been trying different methods but non really work, and Im not sure if its Slacks problem, or if its in my code =/
This is what I have.
let elements = [];
for (let i in response.options) {
let opt = response.options[i];
elements.push(
{
"actions" : [
{
"name": opt.label,
"text": opt.label,
"type": "button",
"value": opt.value.input.text
}]
}
);
}
payload = {
"text": "Would you like to book?",
attachments: [
{
"text": "Choose",
"fallback": "You are unable to choose a game",
"callback_id": "wopr_game",
"color": "#3AA3E3",
"attachment_type": "default",
"actions": elements
}
]
};
console.log(payload)
my console returns this.
{ text: 'Would you like to book?',
attachments:
[ { text: 'Choose',
fallback: 'You are unable to choose a game',
callback_id: 'wopr_game',
color: '#3AA3E3',
attachment_type: 'default',
actions: [Array] } ] }
and whit stringify
{ text: 'Would you like to book?',
attachments:
[ { text: 'Choose',
fallback: 'You are unable to choose a game',
callback_id: 'wopr_game',
color: '#3AA3E3',
attachment_type: 'default',
actions: '{"actions":{"name":"Book transport","text":"Book transport","type":"button","value":"transport"}},{"actions":{"name":"Book hotel","text":"Book hotel","type":"button","value":"hotel"}},{"actions":{"name":"Travel Policy","text":"Travel Policy","type":"button","value":"Travel Policy"}}' } ] }
Any ideas on how i can return just the text so slack renders the buttons?
Thanks in advance!
Update :
If i add a hardcoded solution to the payload, the slack buttons return correct.
payload = {
attachments: [
{
title: 'Do you want to interact with my buttons?',
callback_id: '123',
attachment_type: 'default',
actions: [
{
"name": "yes",
"text": "Yes",
"value": "yes",
"type": "button",
},
{
"name": "no",
"text": "No",
"value": "no",
"type": "button",
}
]
}
]
};

So you want to set the "elements" property of the payload to just the values in the "actions.text" properties of the objects inside the array? Sounds like a job for Array.map :) When setting elements on the payload, try this instead:
"elements": elements.map(el => el.actions.text)

Related

How to add a option with checkbox in Google Chat aPI

I'm using the Google Chat API to send daily updates. I want to have a UI like this:
Like this, I want the UI should appear,
https://developers.google.com/hangouts/chat/reference/message-formats/cards I'm referring this sheet to generate those UIs.
Checkbox is not an available card message.
You can use button widget in a card message. But there are only 2 types of buttons that you can use, its either an image button or a text button.
Buttons
a widget may contain one or more buttons.
buttons in the same widget will be laid out horizontally.
buttons in a different widget will be laid out vertically.
there are two types of buttons: ImageButton and TextButton.
A button can specify a URL that will be opened when a user clicks on it, or an action to be handled by the bot's CARD_CLICKED event handler, as shown in Creating interactive cards.
An ImageButton may specify either a built-in icon or a custom image URL.
You can create card message using spaces.messages.create.
Sample Message Request Body: (card message)
In this example, there are 3 widgets created having 1 button in each widget so that the buttons will be aligned vertically.
{
"cards": [
{
"header": {
"title": "ChatBot",
"imageUrl": "https://www.gstatic.com/images/icons/material/system/1x/face_black_24dp.png",
},
"sections": [
{
"widgets": [
{
"buttons": [
{
"textButton": {
"text": "I have a bike",
"onClick": {
"action": {
"actionMethodName": "optionClick",
"parameters": [
{
"key": "mode",
"value": "bike"
}
]
}
}
}
}
]
},
{
"buttons": [
{
"textButton": {
"text": "I have a car",
"onClick": {
"action": {
"actionMethodName": "optionClick",
"parameters": [
{
"key": "mode",
"value": "car"
}
]
}
}
}
}
]
},
{
"buttons": [
{
"textButton": {
"text": "I have a boat",
"onClick": {
"action": {
"actionMethodName": "optionClick",
"parameters": [
{
"key": "mode",
"value": "boat"
}
]
}
}
}
}
]
}
]
}
]
}
]
}
OUTPUT:

Use "response_action" to respond to view_submission when using botkit

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.

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

Unable to get telegram inline buttons to work

I was working with MS Bot framework and wanted to display some inline buttons on telegram after going through the documentation and some related questions i wrote the following but after session.send(temp) I'm not getting any buttons on the channel.
var temp = {
"type": "Message",
"from": { "channelID":"telegram", "address": session.message.from.id},
"to": { "channelID":"telegram", "address": session.message.to.id},
"conversationId": session.message.conversationId,
"channelData": {
"method": "editMessageReplyMarkup",
"parameters": {
"message_id": session.message.id,
"reply_markup": {
"inline_keyboard": [
[{"text": "Show me more options", "callback_data": "next"}],
[{"text": "Start a new search", "callback_data": "quit"}]
]
}
}
}
};
session.send(temp);
Buttons are supported natively in BotFramework so you don't need to use ChannelData. (See Docs)
"buttons":
[
{
"type": "imBack",
"title": "Show me more options",
"value": "next"
},
{
"type": "imBack",
"title": "Start a new search",
"value": "quit"
}
]
If you did want to achieve this through channelData, you must stringify the value of the reply_markup field, e.g.:
"reply_markup": JSON.stringify({
"inline_keyboard": [
[{"text": "Show me more options", "callback_data": "next"}],
[{"text": "Start a new search", "callback_data": "quit"}]
]
})

Resources