Skip to content

Commit

Permalink
Add wait support
Browse files Browse the repository at this point in the history
  • Loading branch information
tsickert committed Apr 5, 2024
2 parents 5a06f51 + bbd23c3 commit 94d1bb3
Show file tree
Hide file tree
Showing 7 changed files with 50 additions and 16 deletions.
8 changes: 8 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -89,3 +89,11 @@ jobs:
content: Threaded message
avatar-url: https://cdn.discordapp.com/avatars/742807869023322232/dd41912939ffccbea0276f70688fa0ec.webp?size=256
thread-id: ${{ secrets.TEST_THREAD_ID }}
- name: Discord Webhook Action Confirm Message Sent
uses: ./
with:
webhook-url: ${{ secrets.WEBHOOK_URL }}
username: Bill
content: Discord will confirm this was sent successfully before replying.
avatar-url: https://cdn.discordapp.com/avatars/742807869023322232/dd41912939ffccbea0276f70688fa0ec.webp?size=256
wait: true
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ Want to know more about Discord Webhooks? Check out the [intro](https://support.
| thread-id | `false` | ID of the thread you want the webhook to send the message into (will automatically unarchive threads) |
| thread-name | `false` | Name of the thread you want the webhook to create |
| flags | `false` | Message flags |
| wait | `false` | Whether Discord should confirm the message was successfully sent before responding to the request (boolean) |
| username | `false` | The username that should appear to send the message. Note: username will have the "bot" badge next to their name |
| avatar-url | `false` | URL for the avatar that should appear with the message |
| tts | `false` | Whether the message is text-to-speech |
Expand Down
3 changes: 3 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ inputs:
flags:
description: 'Message flags'
required: false
wait:
description: 'Whether Discord should wait for confirmation of message send before responding to the webhook request'
required: false
username:
description: 'The username that should appear to send the message. Note: username will have the "bot" badge next to their name.'
required: false
Expand Down
26 changes: 18 additions & 8 deletions dist/index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/index.js.map

Large diffs are not rendered by default.

23 changes: 16 additions & 7 deletions lib/discord/webhook.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,20 +23,29 @@ async function handleResponse(response: TypedResponse<unknown>): Promise<void> {
export async function executeWebhook(
webhookUrl: string,
threadId: string,
filename: string,
filePath: string,
threadName: string,
flags: string,
wait: boolean,
payload: unknown): Promise<void>{

if (threadId !== '') {
webhookUrl = `${webhookUrl}?thread_id=${threadId}`
}

if (filename !== '' || threadName !== '' || flags !== '') {
if (wait) {
if (webhookUrl.includes('?')) {
webhookUrl = `${webhookUrl}&wait=true`
} else {
webhookUrl = `${webhookUrl}?wait=true`
}
}

if (filePath !== '' || threadName !== '' || flags !== '') {
const formData = new FormData()
if (filename !== '') {
const actualFilename = path.basename(filename);
formData.append('upload-file', await blob(createReadStream(filename)), actualFilename)
if (filePath !== '') {
const fileName = path.basename(filePath);
formData.append('upload-file', await blob(createReadStream(filePath)), fileName)
formData.append('payload_json', JSON.stringify(payload))
}
if (threadName !== '') {
Expand All @@ -56,13 +65,13 @@ export async function executeWebhook(
})

if (response.status !== 200) {
if (filename !== '') {
if (filePath !== '') {
core.error(`failed to upload file: ${response.statusText}`)
}
if (threadName !== '') {
core.error(`failed to create thread: ${threadName}`)
}
} else if (filename !== '') {
} else if (filePath !== '') {
core.info(
`successfully uploaded file with status code: ${response.status}`
)
Expand Down
3 changes: 3 additions & 0 deletions src/action.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ const FILENAME = 'filename'
const THREAD_ID = 'thread-id'
const THREAD_NAME = 'thread-name'
const FLAGS = 'flags'
const WAIT = 'wait'

const TOP_LEVEL_WEBHOOK_KEYS = [CONTENT, USERNAME, AVATAR_URL]
const EMBED_KEYS = [TITLE, DESCRIPTION, TIMESTAMP, COLOR, URL]
Expand Down Expand Up @@ -123,6 +124,7 @@ async function run(): Promise<void> {
const threadId = core.getInput(THREAD_ID)
const threadName = core.getInput(THREAD_NAME)
const flags = core.getInput(FLAGS)
const wait = core.getBooleanInput(WAIT)
const payload = createPayload()
try {
core.info('Running discord webhook action...')
Expand All @@ -132,6 +134,7 @@ async function run(): Promise<void> {
filename,
threadName,
flags,
wait,
payload
)
} catch (error) {
Expand Down

0 comments on commit 94d1bb3

Please sign in to comment.