我正在创建一个 slack 应用程序,该应用程序向频道和特定用户发布民意调查和测验。我正在使用 chat.postMessage() 。我在模态中使用了 conversations_select 类型输入字段来选择频道/用户。当我从该列表中选择一个频道并将其值传递给 chat.postMessage() 时,它已成功发布民意调查/测验。但是,当我从列表中选择用户并将 userID 作为通道字段传递给 chat.postMessage() 方法时,它不起作用。我该怎么办。
//测验主屏幕UI
app.action("quizstart", async ({ ack, body, context, client }) => {
// Acknowledge the button request
ack();
try {
const result = await app.client.views.update({
token: context.botToken,
// Pass the view_id
view_id: body.view.id,
// View payload with updated blocks
view: {
type: "modal",
// View identifier
callback_id: "quiz_view",
title: {
type: "plain_text",
text: "Quiz"
},
close: {
type: "plain_text",
text: "Cancel",
emoji: true
},
blocks: [
{
dispatch_action: true,
type: "input",
element: {
type: "plain_text_input",
dispatch_action_config: {
trigger_actions_on: ["on_character_entered"]
},
action_id: "question_input_field" //ooy
},
label: {
type: "plain_text",
text: "Question",
emoji: true
}
},
{
dispatch_action: true,
type: "input",
element: {
type: "plain_text_input",
dispatch_action_config: {
trigger_actions_on: ["on_character_entered"]
},
action_id: "plain_text_input_action"
},
label: {
type: "plain_text",
text: "Choice A",
emoji: true
},
optional: true
},
{
dispatch_action: true,
type: "input",
element: {
type: "plain_text_input",
dispatch_action_config: {
trigger_actions_on: ["on_character_entered"]
},
action_id: "plain_text_input_action"
},
label: {
type: "plain_text",
text: "Choice B",
emoji: true
},
optional: true
},
{
dispatch_action: true,
type: "input",
element: {
type: "plain_text_input",
dispatch_action_config: {
trigger_actions_on: ["on_character_entered"]
},
action_id: "plain_text_input_action"
},
label: {
type: "plain_text",
text: "Choice C",
emoji: true
},
optional: true
},
{
dispatch_action: true,
type: "input",
element: {
type: "plain_text_input",
dispatch_action_config: {
trigger_actions_on: ["on_character_entered"]
},
action_id: "plain_text_input_action"
},
label: {
type: "plain_text",
text: "Choice D",
emoji: true
},
optional: true
},
{
dispatch_action: true,
type: "input",
element: {
type: "plain_text_input",
dispatch_action_config: {
trigger_actions_on: ["on_character_entered"]
},
action_id: "plain_text_input_action"
},
label: {
type: "plain_text",
text: "Choice E",
emoji: true
},
optional: true
},
{
dispatch_action: true,
type: "input",
element: {
type: "plain_text_input",
dispatch_action_config: {
trigger_actions_on: ["on_character_entered"]
},
action_id: "correct_answer_action"
},
label: {
type: "plain_text",
text: "Correct Answer",
emoji: true
},
optional: false
},
{
dispatch_action: true,
type: "input",
element: {
type: "plain_text_input",
dispatch_action_config: {
trigger_actions_on: ["on_character_entered"]
},
action_id: "quizname_action"
},
label: {
type: "plain_text",
text: "Quiz name",
emoji: true
},
optional: false
},
{
type: "section",
text: {
type: "mrkdwn",
text:
"Here you can configure your *WebMobi Poll*. (<https://google.com|learn more>)"
}
},
{
type: "divider"
},
{
type: "section",
text: {
type: "mrkdwn",
text: "*Audience*"
}
},
{
type: "actions",
elements: [
{
type: "conversations_select",
placeholder: {
type: "plain_text",
text: "conversations", //730
emoji: true
},
action_id: "select_channel_menu"
}
]
},
{
type: "section",
text: {
type: "mrkdwn",
text: "*Advanced Settings*"
}
},
{
type: "section",
text: {
type: "mrkdwn",
text: ":clipboard: *Anonymus*\nChoose to send poll as anonymous"
},
accessory: {
type: "static_select",
placeholder: {
type: "plain_text",
text: "Select",
emoji: true
},
options: [
{
text: {
type: "plain_text",
text: "True",
emoji: true
},
value: "value-anonymus1"
},
{
text: {
type: "plain_text",
text: "False",
emoji: true
},
value: "value-anonymus2"
}
]
}
},
],
submit: {
type: "plain_text",
text: "Submit"
}
}
});
console.log(result);
} catch (error) {
console.error(error);
}
});
//app.view for view submission
// main question UI
app.view("quiz_view", async ({ ack, body, view, context }) => {
await ack();
console.log("inside quiz view app.view " + view.id);
const val1 = view["state"]["values"];
const user1 = body["user"]["id"];
const user2 = body["user"]["name"];
var quiz_id = await makeQuizPostRequest(val1, user1, user2);
publishMessageQuiz(val1, user1, user2, quiz_id,body);
console.log(quiz_id);
});
async function makeQuizPostRequest(val1, user1, user2) {
const x = val1;
console.log(x);
const ques = Object.values(x)[0].question_input_field.value;
console.log("before ques console");
console.log("ques " + ques);
const opt1 = Object.values(x)[1].plain_text_input_action.value;
console.log("opt1 " +opt1);
const opt2 = Object.values(x)[2].plain_text_input_action.value;
console.log("opt2 " +opt2);
const opt3 = Object.values(x)[3].plain_text_input_action.value;
console.log("opt3 " +opt3);
const opt4 = Object.values(x)[4].plain_text_input_action.value;
console.log("opt4 " +opt4);
const opt5 = Object.values(x)[5].plain_text_input_action.value;
console.log("opt5 " +opt5);
const correctAns= Object.values(x)[6].correct_answer_action.value;
console.log("correctAns " + correctAns);
const quizname = Object.values(x)[7].quizname_action.value;
console.log(" quizname " + quizname);
const channel = Object.values(x)[8].select_channel_menu.selected_conversation;
var options = new Array(7);
if (opt1 != null) {
console.log(opt1);
options.push(opt1);
}
if (opt2 != null) {
console.log(opt2);
options.push(opt2);
}
if (opt3 != null) {
console.log(opt3);
options.push(opt3);
}
if (opt4 != null) {
console.log(opt4);
options.push(opt4);
}
if (opt5 != null) {
console.log(opt5);
options.push(opt5);
}
options.shift();
options.shift();
options.shift();
options.shift();
options.shift();
var options1 = options.toString();
options1 = "[" + options1 + "]";
let payload =
{
"app_id": "XXXXXXXXXXXXXXXXXXXXXXXX",
"created_by": user2,
"customtheme": false,
"quizarray": [
{
"question": ques,
"options": options1,
"correctanswer": correctAns,
"isActive": true
}
],
"quiz_name": quizname
}
let res = await axios.post(
"https://api_to_create_quiz",
payload
);
let data = res.data;
console.log(data.responseString);
var quiz_id = res.data.data.quiz_id;
console.log(quiz_id);
return quiz_id;
}
//publishMessage for quiz
async function publishMessageQuiz(val1, user1, user2, quiz_id, body) {
console.log("publishmessage_entry Quiz");
console.log("quiz_id", quiz_id);
let x = val1;
// console.log(x);
let voteList = [];
let ques = Object.values(x)[0].question_input_field.value;
let opt1 = Object.values(x)[1].plain_text_input_action.value;
let opt2 = Object.values(x)[2].plain_text_input_action.value;
let opt3 = Object.values(x)[3].plain_text_input_action.value;
let opt4 = Object.values(x)[4].plain_text_input_action.value;
let opt5 = Object.values(x)[5].plain_text_input_action.value;
let correctAns=Object.values(x)[6].correct_answer_action.value;
let quizname=Object.values(x)[7].quizname_action.value;
const channel = Object.values(x)[8].select_channel_menu.selected_conversation;
let options = new Array(5);
if (opt1 != null) {
options.push(opt1);
}
if (opt2 != null) {
options.push(opt2);
}
if (opt3 != null) {
options.push(opt3);
}
if (opt4 != null) {
options.push(opt4);
}
if (opt5 != null) {
options.push(opt5);
}
options.shift();
options.shift();
options.shift();
options.shift();
options.shift();
let options1 = options.toString();
let optionList = [];
let optionButtons = [];
let optionGraph = [];
let arr_options = options1.split(",");
arr_options.forEach(function(main, index) {
// console.log(main);
let val_id = "value" + index;
// console.log(val_id);
let act_id = "option" + index + "_" + quiz_id;
console.log(act_id);
optionButtons.push(act_id);
console.log(optionButtons);
let voteObj = {
type: "button",
text: {
type: "plain_text",
emoji: true,
text: main
},
value: val_id,
action_id: act_id
};
optionList.push(voteObj);
});
try {
// Call the chat.postMessage method using the built-in WebClient
let result = await app.client.chat.postMessage({
// The token you used to initialize your app
token: process.env.SLACK_BOT_TOKEN,
signingSecret: process.env.SLACK_SIGNING_SECRET,
channel:channel,
text: "WebMobi Quiz",
blocks: [
{
type: "section",
text: {
type: "mrkdwn",
text: "Quiz question from WebMobi poll"
}
},
{
type: "section",
text: {
type: "mrkdwn",
text: "*" + ques + "*"
}
},
{
type: "actions",
elements: [
...optionList
]
},
{
type: "divider"
},
{
type: "context",
elements: [
{
type: "mrkdwn",
text:
"hello (Sender: " +
user2 +
" | :unlock: *Responses*: Non-Anonymous/Anonymous | :clock930: *Status*: "
}
]
},
{
type: "context",
elements: [
{
type: "mrkdwn",
text:
":smile: Get insight of your polls from <https://google.com|WebMobi dashboard>"
}
]
}
]
});
optionButtons.forEach(el => {
app.action(el, async ({ ack, body, client }) => {
// Acknowledge the button request
await ack();
// console.log(el);
console.log("bodymessage", body);
// console.log("bodymessage", client);
// console.log("my user id", body.user.id);
let person = body.user.name;
let time = body.message.ts;
console.log("message Body", body);
// console.log("This is my Email Id",emailId);
// let result = await postPollApi(poll_id);
let action = body.actions[0].action_id;
let optionValue = "";
let optionResults = [];
let optionGraphResults = [];
let pollAnswer = "";
optionList.forEach(els => {
let opitonObj = {};
let optionGraphResultsObj = {};
let ok = els.text.text;
if (els.action_id == action) {
optionValue = els.value;
opitonObj = {
type: "button",
text: {
type: "plain_text",
emoji: true,
text: els.text.text
},
value: action,
style: "primary"
};
optionResults.push(opitonObj);
const red = "█████████████████████████";
optionGraphResultsObj = {
type: "section",
text: {
type: "mrkdwn",
text: els.text.text + ": `" + red + "` | 100% (1)"
} //option1 + ": `" + graph + "` " + percent + "% (" + noofvotes + ")"
};
pollAnswer = els.text.text;
} else {
opitonObj = {
type: "button",
text: {
type: "plain_text",
emoji: true,
text: els.text.text
},
value: action
};
optionResults.push(opitonObj);
const white = "█ ";
optionGraphResultsObj = {
type: "section",
text: {
type: "mrkdwn",
text: els.text.text + ": `" + white + "` | 0% (0)"
//els.text.text+"`█ ` | 0% (0)"
}
};
}
optionGraphResults.push(optionGraphResultsObj);
});
console.log("action id", action);
let email = await getUserEmail(client, body.user.id);
console.log("webmobi email id", email);
let user_webmobi_id = await getuserId(email);
console.log("webmobi user id", user_webmobi_id);
// let poll_result = await submitPoll(pollAnswer, poll_id,user_webmobi_id);
// console.log("submited poll",poll_result);
try {
console.log("364 quiz");
// Call views.update with the built-in client
const result = await client.chat.update({
token: process.env.SLACK_BOT_TOKEN,
ts: time,
response_url: body.response_url,
response_type: "ephemeral",
channel: channel,
text: "Updated quiz results",
//as_user:true,
blocks: [
{
type: "section",
text: {
type: "mrkdwn",
text: `Quiz question from WebMobi poll`
}
},
{
type: "section",
text: {
type: "mrkdwn",
text: ques
}
},
{
type: "actions",
elements: [...optionResults]
},
// ...optionGraph
...optionGraphResults,
{
type: "divider"
},
{
type: "context",
elements: [
{
type: "mrkdwn",
text:
"hello (Sender: user | :unlock: *Responses*: Non-Anonymous/Anonymous | :clock930: *Status*: "
}
]
},
{
type: "context",
elements: [
{
type: "mrkdwn",
text:
":smile: Get insight of your polls from <https://google.com|WebMobi dashboard>"
}
]
}
]
});
console.log(result);
console.log("chatpostmessage");
} catch (error) {
console.error(error);
}
});
});
// Print result, which includes information about the message (like TS)
console.log(result);
} catch (error) {
console.error(error);
}
}