| 1 | import type { WebhookFormat } from './index.js'; |
| 2 | import { summaryOf } |
| 3 | |
| 4 | /** Slack incoming webhooks: https://docs.slack.dev/messaging/sending-messages-using-incoming-webhooks */ |
| 5 | export const slack: |
| 6 | matches: (url) |
| 7 | url.hostname === |
| 8 | // Slack honours username and icon_url only for webhooks made before Slack apps; an app's webhook posts as the app. |
| 9 | render: (message, |
| 10 | const summary = |
| 11 | const quoted = |
| 12 | ? [ |
| 13 | ...message.commits. |
| 14 | (commit) => |
| 15 | `<${commit.url |
| 16 | ), |
| 17 | ...(message.moreCommits |
| 18 | ? [`and ${message |
| 19 | : []), |
| 20 | ].join('\n' |
| 21 | : message.excerpt |
| 22 | return { |
| 23 | username: sender.name |
| 24 | icon_url: sender.iconUrl |
| 25 | text: [ |
| 26 | `[${escape(message |
| 27 | ...(quoted ? |
| 28 | ].join('\n' |
| 29 | unfurl_links: false, |
| 30 | }; |
| 31 | }, |
| 32 | }; |
| 33 | |
| 34 | // Slack's only escapes; they also keep user text from forming `<!channel>` or a link. |
| 35 | function escape(text: |
| 36 | return text |
| 37 | .replaceAll('&', |
| 38 | .replaceAll('<', |
| 39 | .replaceAll('>', |
| 40 | } |