|
1 | | -import {App, UsersSelectAction} from '@slack/bolt'; |
| 1 | +import {App, MessageShortcut, UsersSelectAction} from '@slack/bolt'; |
2 | 2 | import {WebClient} from '@slack/web-api'; |
3 | 3 | import {Actions, Header, Input, Message, Modal, Section, TextInput, UserSelect} from 'slack-block-builder'; |
4 | 4 | import {getInfo} from './util/sheets'; |
@@ -38,6 +38,14 @@ app.action('info-select', async ({ack, payload, client, respond}) => { |
38 | 38 | await respond(message); |
39 | 39 | }); |
40 | 40 |
|
| 41 | +app.shortcut('info-shortcut', async ({ack, payload, client, shortcut}) => { |
| 42 | + await ack(); |
| 43 | + const user = (shortcut as MessageShortcut).message.user; |
| 44 | + if (!user) return; |
| 45 | + const modal = await infoModal(client, user); |
| 46 | + await client.views.open({trigger_id: payload.trigger_id, view: modal}); |
| 47 | +}); |
| 48 | + |
41 | 49 | async function infoResponse(client: WebClient, id: string) { |
42 | 50 | const res = await client.users.info({ token, user: id }); |
43 | 51 | if (!res.user?.real_name) return Message() |
@@ -74,6 +82,39 @@ async function infoResponse(client: WebClient, id: string) { |
74 | 82 | .buildToObject() |
75 | 83 | } |
76 | 84 |
|
| 85 | +// TODO: this can probably be abstracted further with the function above; |
| 86 | +// a common function might return only the block kit blocks passed to `.blocks(...)`, |
| 87 | +// but because the message has a dropdown and the modal does not this wouldn't quite work. |
| 88 | +async function infoModal(client: WebClient, id: string) { |
| 89 | + const res = await client.users.info({ token, user: id }); |
| 90 | + if (!res.user?.real_name) return Modal({title: 'SEC contact info'}) |
| 91 | + .blocks( |
| 92 | + Header({text: 'There was an error fetching your name.'}), |
| 93 | + Section({text: 'If this issue persists, please message <@U03GQC0A9MJ>.'}) |
| 94 | + ) |
| 95 | + .buildToObject() |
| 96 | + |
| 97 | + const info = await getInfo(res.user.real_name); |
| 98 | + if (!info) return Modal({title: 'SEC contact info'}) |
| 99 | + .blocks( |
| 100 | + Header({text: `${res.user.real_name} was not found on the contacts spreadsheet.`}), |
| 101 | + Section({text: 'If this is a mistake, please message <@U03GQC0A9MJ>.'}) |
| 102 | + ) |
| 103 | + .buildToObject() |
| 104 | + |
| 105 | + const [lastName, firstName, position, email, cell, home] = info; |
| 106 | + const fields = [`*Email:*\n${email}`]; |
| 107 | + if (cell) fields.push(`*Cell phone:*\n${parseCellPhone(cell)}`); |
| 108 | + if (home) fields.push(`*Home phone:*\n${parseCellPhone(home)}`); |
| 109 | + |
| 110 | + return Modal({title: 'SEC contact info'}) |
| 111 | + .blocks( |
| 112 | + Header({text: `Contact info for ${capitalize(firstName)} ${capitalize(lastName)} (${position})`}), |
| 113 | + Section().fields(fields) |
| 114 | + ) |
| 115 | + .buildToObject() |
| 116 | +} |
| 117 | + |
77 | 118 | // /help |
78 | 119 | // Sends info about other commands. |
79 | 120 | app.command('/help', async ({ack, respond}) => { |
|
0 commit comments