-
Notifications
You must be signed in to change notification settings - Fork 0
Webhook Integration
Garot Conklin edited this page Dec 8, 2024
·
1 revision
This document details the webhook integration between GitHub and the fleXRPL Discord Bot, including setup, configuration, and event handling.
/webhook setup channel:#github-events secret:your-webhook-secret- Navigate to repository settings
- Select "Webhooks"
- Add webhook:
Payload URL: https://your-bot-domain.com/webhook Content type: application/json Secret: your-webhook-secret - Select events:
- Repository events
- Issues
- Pull requests
- Discussions
- Stars
# Example webhook secret validation
import hmac
import hashlib
def verify_signature(payload_body, secret, signature):
expected = hmac.new(
secret.encode('utf-8'),
payload_body,
hashlib.sha256
).hexdigest()
return hmac.compare_digest(f"sha256={expected}", signature)- Rotate webhook secrets regularly
- Use environment variables
- Implement rate limiting
- Validate payload signatures
| Event Type | Description | Default Channel |
|---|---|---|
| push | Repository pushes | #github-activity |
| pull_request | PR activities | #pull-requests |
| issues | Issue activities | #issues |
| discussions | Discussion activities | #discussions |
{
"event": "pull_request",
"action": "opened",
"repository": {
"full_name": "fleXRPL/flexrpl-discord-bot"
},
"pull_request": {
"title": "Feature Implementation",
"user": {
"login": "username"
}
}
}🔵 New Pull Request in {repository}
Title: {title}
Author: {author}
{url}
📝 New Issue in {repository}
Title: {title}
Author: {author}
{url}
# Example custom formatter
async def format_message(event_type, payload):
templates = {
'pull_request': '🔵 PR: {title}',
'issues': '📝 Issue: {title}',
'push': '⬆️ Push: {commits} commits'
}
return templates[event_type].format(**payload)| Error | Cause | Resolution |
|---|---|---|
| 401 | Invalid secret | Verify webhook secret |
| 404 | Wrong endpoint | Check webhook URL |
| 429 | Rate limited | Implement backoff |
async def process_webhook(payload, retries=3):
for attempt in range(retries):
try:
return await process_event(payload)
except Exception as e:
if attempt == retries - 1:
raise
await asyncio.sleep(2 ** attempt)- 5000 requests per hour
- Webhook delivery timeout: 10s
- Maximum payload size: 25MB
from fastapi import FastAPI, HTTPException
from fastapi.middleware.throttling import ThrottlingMiddleware
app = FastAPI()
app.add_middleware(
ThrottlingMiddleware,
rate_limit=100,
time_window=60
)- Webhook delivery rate
- Processing success rate
- Response time
- Error frequency
@app.get("/health")
async def health_check():
return {
"status": "healthy",
"webhook_processor": "running",
"last_event": last_event_timestamp
}-
Use ngrok for local webhook testing:
ngrok http 8000
-
Configure test webhook:
Payload URL: https://your-ngrok-url/webhook
# Test payload
test_payload = {
"event": "pull_request",
"action": "opened",
"repository": {
"full_name": "test/repo"
}
}DISCORD_WEBHOOK_SECRET=your-secret
GITHUB_WEBHOOK_SECRET=your-github-secret
DISCORD_BOT_TOKEN=your-bot-token[build]
builder = "nixpacks"
watchPatterns = ["src/**"]
[deploy]
startCommand = "uvicorn main:app --host 0.0.0.0 --port $PORT"
healthcheckPath = "/health"
healthcheckTimeout = 5-
Webhook Not Delivering
- Verify URL is correct
- Check secret matches
- Confirm events are selected
-
Message Not Appearing
- Check channel permissions
- Verify bot is online
- Review event configuration
-
Rate Limiting
- Implement exponential backoff
- Monitor usage metrics
- Optimize payload processing
This documentation is maintained by the fleXRP team.