OperateRecipes

Recipes

A cron scan, a Discord relay and a daily brief.

Three complete shapes you can copy. Each is small enough to run as a cron job or a

serverless function.

1. A nightly scan

Submit a saved sketch, poll until it finishes, keep the top ten.

import os, time, requests

API = "https://dejabars.com/api/v1/app"
HEAD = {"X-API-Key": os.environ["DEJABARS_KEY"]}

pattern = [0.42, 0.51, 0.47, 0.63, 0.58]
xs = [0.0, 0.2, 0.4, 0.6, 0.8]

task = requests.post(
    f"{API}/search-task",
    headers={**HEAD, "Idempotency-Key": time.strftime("nightly-%Y-%m-%d")},
    json={
        "pattern": pattern,
        "xs": xs,
        "timeframe": "1h",
        "period_days": 30,
        "search_mode": "geo",
        "max_distance": 0.35,
    },
    timeout=30,
).json()

while True:
    status = requests.get(f"{API}/task-status/{task['task_id']}", headers=HEAD).json()
    if status["status"] in ("completed", "failed", "error"):
        break
    time.sleep(3)

result = requests.get(f"{API}/task-result/{task['task_id']}", headers=HEAD).json()
top = sorted(result["results"], key=lambda row: row["distance"])[:10]
for row in top:
    print(row["symbol"], row["distance"])

The Idempotency-Key makes a retry safe: the same date returns the same task.

2. A webhook that posts to Discord

Receive the signed alert, verify it, then post a short message. This runs on your server,

not on ours.

import hashlib, hmac, os, time, requests
from flask import Flask, request, abort

app = Flask(__name__)
SECRET = os.environ["DEJABARS_WEBHOOK_SECRET"].encode()
DISCORD = os.environ["DISCORD_WEBHOOK_URL"]

@app.post("/hooks/dejabars")
def receive():
    raw = request.get_data(as_text=True)
    timestamp = request.headers.get("X-DejaBars-Timestamp", "")
    signature = request.headers.get("X-DejaBars-Signature", "")

    if abs(time.time() - int(timestamp or 0)) > 300:
        abort(400)
    expected = "sha256=" + hmac.new(
        SECRET, f"{timestamp}.{raw}".encode(), hashlib.sha256
    ).hexdigest()
    if not hmac.compare_digest(expected, signature):
        abort(403)

    alert = request.get_json()
    requests.post(DISCORD, json={
        "content": f"{alert['title']}\n{alert['link']}"
    }, timeout=10)
    return "", 204

Answer within a few seconds. Do the Discord call in a background task if it is slow.

3. A daily brief from a finished report

Read a report, take the strongest matches, and write one message.

import os, requests

API = "https://dejabars.com/api/v1/app"
HEAD = {"X-API-Key": os.environ["DEJABARS_KEY"]}

result = requests.get(f"{API}/task-result/{os.environ['TASK_ID']}", headers=HEAD).json()
rows = sorted(result["results"], key=lambda row: row["distance"])[:5]

lines = ["Pattern matches today:"]
for row in rows:
    lines.append(f"- {row['symbol']} · distance {row['distance']:.3f} · p_win {row['p_win']:.2f}")

print("\n".join(lines))

4. Fail closed

  • If the quote is 402, skip the run and report it; do not retry.
  • If a webhook signature fails, return 403 and log the delivery id.
  • If the search never completes, alert your own operator. Silence is not a result.
Verify a call

Open the interactive reference and run the operation with your key.

Open the playground →