Skip to content

Commit 7922f1b

Browse files
committed
feat: support custom HTTP headers per command
- Allow defining custom headers directly inside routes.json for individual commands. - Unlock seamless integrations with third-party APIs (OpenAI, Gantry, GitHub) requiring specific authentication keys like Authorization or X-Token. - Preserve global X-DashCord-Token fallback to ensure backwards compatibility with existing setups. - Update documentation and example config with custom header usage.
1 parent dc562a7 commit 7922f1b

3 files changed

Lines changed: 32 additions & 1 deletion

File tree

README.md

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -224,6 +224,28 @@ The `body_template` can be **any valid JSON structure** (deeply nested objects,
224224
* `{{attachment_text}}`: The raw UTF-8 text of the file (great for `.txt` or `.json` uploads).
225225
* `{{attachment.filename}}`: The original name of the uploaded file.
226226

227+
### 5. Custom HTTP Headers (Optional)
228+
229+
By default, DashCord secures your webhooks using the `X-DashCord-Token` header globally defined in your `.env`. However, if you want to bypass your automation tool and point DashCord *directly* at a third-party API (like OpenAI, Gantry, or GitHub), you can define custom HTTP headers per-command.
230+
231+
```json
232+
"commands": {
233+
"chat": {
234+
"endpoint": "https://api.openai.com/v1/chat/completions",
235+
"method": "POST",
236+
"headers": {
237+
"Authorization": "Bearer sk-proj-YourApiKeyHere",
238+
"OpenAI-Organization": "org-123456"
239+
},
240+
"body_template": {
241+
"model": "gpt-4",
242+
"messages": [{"role": "user", "content": "{{raw}}"}]
243+
}
244+
}
245+
}
246+
```
247+
*Note: Any custom headers defined in `routes.json` will override the global default token if they share the same key. If omitted, the bot falls back to using the global `.env` secret.*
248+
227249
---
228250

229251
## 📦 Default Webhook Payload

bot.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -668,9 +668,15 @@ def post_to_webhook(command: str, payload: dict) -> dict:
668668
out_json = _render_body_template(body_template, payload)
669669

670670
headers = {"Content-Type": "application/json"}
671+
671672
if DASHCORD_SHARED_SECRET:
672673
headers["X-DashCord-Token"] = DASHCORD_SHARED_SECRET
673674

675+
custom_headers = cfg.get("headers")
676+
if isinstance(custom_headers, dict):
677+
for h_key, h_val in custom_headers.items():
678+
headers[h_key] = str(h_val)
679+
674680
def parse_response(r: requests.Response) -> dict:
675681
_dbg("WEBHOOK POST cmd=%s status=%s", command, r.status_code)
676682

@@ -1182,7 +1188,6 @@ async def on_message(message: discord.Message):
11821188
await _add_reaction_safe(message, COMMAND_REACTION_SUCCESS)
11831189
else:
11841190
await _add_reaction_safe(message, COMMAND_REACTION_FAIL)
1185-
# ------------------------------
11861191

11871192
await send_reply(message.channel, data)
11881193
except Exception as e:

routes.json.example

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,10 @@
3333
"allowed_users": [987654321098765432],
3434
"accept_attachments": true,
3535
"allow_without_command": true,
36+
"headers": {
37+
"Authorization": "Bearer YOUR_CUSTOM_TOKEN_HERE",
38+
"X-Custom-Flag": "true"
39+
},
3640
"body_template": {
3741
"user_data": {
3842
"id": "{{discord.user_id}}",

0 commit comments

Comments
 (0)