Skip to content
This repository has been archived by the owner on Mar 25, 2022. It is now read-only.

Commit

Permalink
Merge pull request #51 from trustpilot/small-updates
Browse files Browse the repository at this point in the history
Small updates
  • Loading branch information
miklosaubert authored Jan 21, 2019
2 parents d860070 + 7a31976 commit 2979fce
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 25 deletions.
18 changes: 10 additions & 8 deletions app/slackapp/feed-settings.js
Original file line number Diff line number Diff line change
Expand Up @@ -110,12 +110,8 @@ const showFeedSettings = (bot, message) => {
response_url: message.response_url, // eslint-disable-line camelcase
channel: message.channel,
};
const dialog = bot
.createDialog(
'Review settings',
JSON.stringify({ dialogType: 'feed_settings', sourceMessage }),
'Save'
)
const dialogBuilder = bot
.createDialog('Review settings', 'feed_settings', 'Save')
.addSelect('Filter by star rating', 'starFilter', starFilter, [
{ label: 'None - post all reviews', value: 'all' },
{ label: 'Only post 4 and 5-star reviews', value: 'positive' },
Expand All @@ -128,7 +124,13 @@ const showFeedSettings = (bot, message) => {
value: 'off',
},
]);
bot.replyWithDialog(message, dialog.asObject(), (err, res) => {

// Add a state property to the dialog object
const dialogData = {
state: JSON.stringify({ sourceMessage }),
...dialogBuilder.asObject(),
};
bot.replyWithDialog(message, dialogData, (err, res) => {
if (err) {
console.log(err, res);
}
Expand Down Expand Up @@ -167,7 +169,7 @@ const handleNewFeedSettings = async (bot, message, slackapp) => {
};

const handleDialogSubmission = (slackapp) => async (bot, message) => {
const { sourceMessage } = JSON.parse(message.callback_id);
const { sourceMessage } = JSON.parse(message.state);
await handleNewFeedSettings(bot, message, slackapp);
await showIntroMessage(sourceMessage, bot);
};
Expand Down
15 changes: 10 additions & 5 deletions app/slackapp/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ const setupAppHandlers = (slackapp, apiClient, enableReviewQueries) => {

slackapp.on('dialog_submission', async (bot, message) => {
bot.dialogOk();
const { dialogType } = JSON.parse(message.callback_id);
const dialogType = message.callback_id;
const dialogHandlers = {
['review_reply']: reviewReply.handleReply(apiClient),
['feed_settings']: feedSettings.handleDialogSubmission(slackapp),
Expand All @@ -154,13 +154,18 @@ const setupAppHandlers = (slackapp, apiClient, enableReviewQueries) => {
);

feeds.forEach(async ({ channelId, canReply }) => {
const message = composeReviewMessage(review, { canReply });
message.username = bot.config.bot.name; // Confusing, but such is life
message.channel = channelId;
const message = {
username: bot.config.bot.name, // Confusing, but such is life
channel: channelId,
...composeReviewMessage(review, { canReply }),
};
try {
const { ok: sentOk, ts, channel } = await bot.sendAsync(message);
if (sentOk) {
slackapp.trigger('trustpilot_review_posted', [bot, { ts, channel, reviewId: review.id }]);
slackapp.trigger('trustpilot_review_posted', [
bot,
{ businessUnitId, ts, channel, reviewId: review.id },
]);
}
} catch (e) {
if (e.message === 'account_inactive') {
Expand Down
25 changes: 13 additions & 12 deletions app/slackapp/review-reply.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,18 +20,19 @@ const removeReaction = (bot, channel, timestamp, name) => {
};

const showReplyDialog = (bot, message) => {
const dialog = bot
.createDialog(
'Reply to a review',
JSON.stringify({
dialogType: 'review_reply',
originalTs: message.message_ts,
reviewId: message.callback_id,
}),
'Send'
)
const dialogBuilder = bot
.createDialog('Reply to a review', 'review_reply', 'Send')
.addTextarea('Your reply', 'reply');
bot.replyWithDialog(message, dialog.asObject(), (err, res) => {

// Add a state property to the dialog object
const dialogData = {
state: JSON.stringify({
originalTs: message.message_ts,
reviewId: message.callback_id,
}),
...dialogBuilder.asObject(),
};
bot.replyWithDialog(message, dialogData, (err, res) => {
if (err) {
console.log(err, res);
}
Expand Down Expand Up @@ -67,7 +68,7 @@ const confirmReply = async (bot, message, originalTs) => {
};

const handleReply = (apiClient) => async (bot, message) => {
const { originalTs, reviewId } = JSON.parse(message.callback_id);
const { originalTs, reviewId } = JSON.parse(message.state);
try {
await apiClient.replyToReview({
reviewId,
Expand Down

0 comments on commit 2979fce

Please sign in to comment.