worker: Telegram notice about every purchase (notify task)
A separate worker task, apart from invoicing: each paid checkout is
announced once to the operator's Telegram chat ('Páros csomag, 6 000 Ft',
marked when it was a test payment), without the buyer's data. Outages
and rate limits are retried; a wrong token or chat is logged, not
retried. Needs TELEGRAM_BOT_TOKEN (.env.secrets) and TELEGRAM_CHAT_ID.
Also fixes the worker test that still expected the old sign-up e-mail
subject (CI failed on it since 7d6a6c3).
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_012vLHzh5qUhFtmJhzkcTbXL
This commit is contained in:
@@ -0,0 +1,4 @@
|
|||||||
|
-- When the operator's Telegram notice about a paid checkout went out (the
|
||||||
|
-- worker's `notify` task). Earlier purchases count as notified.
|
||||||
|
ALTER TABLE stripe_checkouts ADD COLUMN notified_at timestamptz;
|
||||||
|
UPDATE stripe_checkouts SET notified_at = now() WHERE completed_at IS NOT NULL;
|
||||||
@@ -28,3 +28,7 @@ TAG=latest
|
|||||||
# Beszel monitoring agent (deploy/README.md): the hub's public key.
|
# Beszel monitoring agent (deploy/README.md): the hub's public key.
|
||||||
# COMPOSE_PROFILES=monitoring
|
# COMPOSE_PROFILES=monitoring
|
||||||
# BESZEL_KEY=ssh-ed25519 AAAA...
|
# BESZEL_KEY=ssh-ed25519 AAAA...
|
||||||
|
|
||||||
|
# Purchase notices to Telegram (worker notify task); the bot token
|
||||||
|
# (TELEGRAM_BOT_TOKEN) goes to .env.secrets.
|
||||||
|
# TELEGRAM_CHAT_ID=123456789
|
||||||
|
|||||||
@@ -88,5 +88,6 @@ A Google OAuth kliensnél a `https://sorsfonalak.hu` címet fel kell venni az en
|
|||||||
- A worker két konténerben fut, ugyanabból az image-ből: a `worker` küldi az e-maileket és takarít (`WORKER_TASKS=email,cleanup`, ide kerülnek a további háttérfeladatok is), a `worker-ai` írja az elemzéseket és a válaszokat (`WORKER_TASKS=analyses`, gyors és lassú sáv). A sávok mérete a szerver `.env` fájljában állítható (`QUICK_CONCURRENCY`, `ANALYSIS_CONCURRENCY`).
|
- A worker két konténerben fut, ugyanabból az image-ből: a `worker` küldi az e-maileket és takarít (`WORKER_TASKS=email,cleanup`, ide kerülnek a további háttérfeladatok is), a `worker-ai` írja az elemzéseket és a válaszokat (`WORKER_TASKS=analyses`, gyors és lassú sáv). A sávok mérete a szerver `.env` fájljában állítható (`QUICK_CONCURRENCY`, `ANALYSIS_CONCURRENCY`).
|
||||||
- Visszaállás korábbi verzióra: `.env`-ben `TAG=<régebbi hash>`, utána `docker compose up -d`
|
- Visszaállás korábbi verzióra: `.env`-ben `TAG=<régebbi hash>`, utána `docker compose up -d`
|
||||||
- Mentés: naponta egy `pg_dump` a `/opt/sorsfonalak/backups` mappába, 14 napig megmarad. Az otthoni TrueNAS naponta lehúzza (Rsync Task, Pull, SSH) a `nasbackup` felhasználóval, aki csak ezt a mappát olvashatja (`authorized_keys`: `command="/usr/bin/rrsync -ro /opt/sorsfonalak/backups",restrict`); a NAS-on a régi mentések is megmaradnak.
|
- Mentés: naponta egy `pg_dump` a `/opt/sorsfonalak/backups` mappába, 14 napig megmarad. Az otthoni TrueNAS naponta lehúzza (Rsync Task, Pull, SSH) a `nasbackup` felhasználóval, aki csak ezt a mappát olvashatja (`authorized_keys`: `command="/usr/bin/rrsync -ro /opt/sorsfonalak/backups",restrict`); a NAS-on a régi mentések is megmaradnak.
|
||||||
|
- Vásárlási értesítő: a `worker` `notify` feladata minden kifizetett vásárlásról egyszer szól Telegramon („🛒 Vásárlás: Páros csomag, 6 000 Ft”, a vevő adatai nélkül), a számlázástól függetlenül. Beállítás: `TELEGRAM_BOT_TOKEN` a `.env.secrets`-ben, `TELEGRAM_CHAT_ID` a `.env`-ben; ha bármelyik hiányzik, nincs értesítés.
|
||||||
- Monitorozás: a `beszel-agent` (Beszel, CPU/memória/lemez/konténerek) a NAS-on futó Beszel hubnak ad adatot, Tailscale-en át. Csak a szerver Tailscale-címén figyel (`100.69.226.118:45876`), az internet felé nem nyit portot; a hub kapcsolódik hozzá. Bekapcsolás a `.env`-ben: `COMPOSE_PROFILES=monitoring` és `BESZEL_KEY=<a hub nyilvános kulcsa>`, utána `docker compose up -d beszel-agent`. A hubban a rendszer címe `100.69.226.118`, port `45876`.
|
- Monitorozás: a `beszel-agent` (Beszel, CPU/memória/lemez/konténerek) a NAS-on futó Beszel hubnak ad adatot, Tailscale-en át. Csak a szerver Tailscale-címén figyel (`100.69.226.118:45876`), az internet felé nem nyit portot; a hub kapcsolódik hozzá. Bekapcsolás a `.env`-ben: `COMPOSE_PROFILES=monitoring` és `BESZEL_KEY=<a hub nyilvános kulcsa>`, utána `docker compose up -d beszel-agent`. A hubban a rendszer címe `100.69.226.118`, port `45876`.
|
||||||
- Visszatöltés: `gunzip -c backups/sorsfonalak-<dátum>.sql.gz | docker compose exec -T postgres psql -U sorsfonalak sorsfonalak`
|
- Visszatöltés: `gunzip -c backups/sorsfonalak-<dátum>.sql.gz | docker compose exec -T postgres psql -U sorsfonalak sorsfonalak`
|
||||||
|
|||||||
@@ -129,7 +129,9 @@ services:
|
|||||||
image: ${REGISTRY_PREFIX:-registry.sagifer.com/sagifer/sorsfonalak}-worker:${TAG:-latest}
|
image: ${REGISTRY_PREFIX:-registry.sagifer.com/sagifer/sorsfonalak}-worker:${TAG:-latest}
|
||||||
environment:
|
environment:
|
||||||
DATABASE_URL: postgres://sorsfonalak:${POSTGRES_PASSWORD}@postgres:5432/sorsfonalak
|
DATABASE_URL: postgres://sorsfonalak:${POSTGRES_PASSWORD}@postgres:5432/sorsfonalak
|
||||||
WORKER_TASKS: email,cleanup,invoices
|
WORKER_TASKS: email,cleanup,invoices,notify
|
||||||
|
# Purchase notices to Telegram; the bot token (TELEGRAM_BOT_TOKEN) is in .env.secrets.
|
||||||
|
TELEGRAM_CHAT_ID: ${TELEGRAM_CHAT_ID:-}
|
||||||
# SMTP_URL or the SES credentials come from .env.secrets (docs/levelkuldes.md);
|
# SMTP_URL or the SES credentials come from .env.secrets (docs/levelkuldes.md);
|
||||||
# listing them here with an empty default would override that file.
|
# listing them here with an empty default would override that file.
|
||||||
SES_REGION: ${SES_REGION:-eu-central-1}
|
SES_REGION: ${SES_REGION:-eu-central-1}
|
||||||
|
|||||||
@@ -90,6 +90,8 @@ services:
|
|||||||
SMTP_URL: ${SMTP_URL:-}
|
SMTP_URL: ${SMTP_URL:-}
|
||||||
MAIL_FROM: ${MAIL_FROM:-Sorsfonalak <noreply@sorsfonalak.local>}
|
MAIL_FROM: ${MAIL_FROM:-Sorsfonalak <noreply@sorsfonalak.local>}
|
||||||
PUBLIC_BASE_URL: ${PUBLIC_BASE_URL:-http://localhost:3001}
|
PUBLIC_BASE_URL: ${PUBLIC_BASE_URL:-http://localhost:3001}
|
||||||
|
# Purchase notices to Telegram (TELEGRAM_BOT_TOKEN in .env.secrets).
|
||||||
|
TELEGRAM_CHAT_ID: ${TELEGRAM_CHAT_ID:-}
|
||||||
# OPENROUTER_API_KEY (not in git); without it analyses stay pending.
|
# OPENROUTER_API_KEY (not in git); without it analyses stay pending.
|
||||||
env_file:
|
env_file:
|
||||||
- path: .env.secrets
|
- path: .env.secrets
|
||||||
|
|||||||
@@ -16,11 +16,13 @@ pub struct Tasks {
|
|||||||
pub analyses: bool,
|
pub analyses: bool,
|
||||||
/// Issuing the invoices of the payments in Billingo.
|
/// Issuing the invoices of the payments in Billingo.
|
||||||
pub invoices: bool,
|
pub invoices: bool,
|
||||||
|
/// Telegram notices about purchases.
|
||||||
|
pub notify: bool,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Tasks {
|
impl Tasks {
|
||||||
pub const ALL: Tasks = Tasks { email: true, cleanup: true, analyses: true, invoices: true };
|
pub const ALL: Tasks = Tasks { email: true, cleanup: true, analyses: true, invoices: true, notify: true };
|
||||||
const NONE: Tasks = Tasks { email: false, cleanup: false, analyses: false, invoices: false };
|
const NONE: Tasks = Tasks { email: false, cleanup: false, analyses: false, invoices: false, notify: false };
|
||||||
|
|
||||||
pub fn parse(raw: &str) -> anyhow::Result<Self> {
|
pub fn parse(raw: &str) -> anyhow::Result<Self> {
|
||||||
let mut tasks = Self::NONE;
|
let mut tasks = Self::NONE;
|
||||||
@@ -30,7 +32,10 @@ impl Tasks {
|
|||||||
"cleanup" => tasks.cleanup = true,
|
"cleanup" => tasks.cleanup = true,
|
||||||
"analyses" => tasks.analyses = true,
|
"analyses" => tasks.analyses = true,
|
||||||
"invoices" => tasks.invoices = true,
|
"invoices" => tasks.invoices = true,
|
||||||
other => anyhow::bail!("unknown task '{other}' in WORKER_TASKS (email, cleanup, analyses, invoices)"),
|
"notify" => tasks.notify = true,
|
||||||
|
other => {
|
||||||
|
anyhow::bail!("unknown task '{other}' in WORKER_TASKS (email, cleanup, analyses, invoices, notify)")
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
anyhow::ensure!(tasks != Self::NONE, "WORKER_TASKS names no task");
|
anyhow::ensure!(tasks != Self::NONE, "WORKER_TASKS names no task");
|
||||||
@@ -79,6 +84,12 @@ pub struct Config {
|
|||||||
/// `MAX_INVOICE_ATTEMPTS` (default 10, about a day with the backoff):
|
/// `MAX_INVOICE_ATTEMPTS` (default 10, about a day with the backoff):
|
||||||
/// then the invoice is marked failed for an admin.
|
/// then the invoice is marked failed for an admin.
|
||||||
pub max_invoice_attempts: i32,
|
pub max_invoice_attempts: i32,
|
||||||
|
/// `TELEGRAM_BOT_TOKEN` (secret) and `TELEGRAM_CHAT_ID`: purchase notices
|
||||||
|
/// to this chat; unset: none.
|
||||||
|
pub telegram_bot_token: Option<String>,
|
||||||
|
pub telegram_chat_id: Option<String>,
|
||||||
|
/// `TELEGRAM_API_URL` (default `https://api.telegram.org`).
|
||||||
|
pub telegram_api_url: String,
|
||||||
}
|
}
|
||||||
|
|
||||||
fn var(name: &str) -> Option<String> {
|
fn var(name: &str) -> Option<String> {
|
||||||
@@ -129,6 +140,9 @@ impl Config {
|
|||||||
.transpose()
|
.transpose()
|
||||||
.context("invalid BILLINGO_BLOCK_ID")?,
|
.context("invalid BILLINGO_BLOCK_ID")?,
|
||||||
max_invoice_attempts: parsed("MAX_INVOICE_ATTEMPTS", 10)?,
|
max_invoice_attempts: parsed("MAX_INVOICE_ATTEMPTS", 10)?,
|
||||||
|
telegram_bot_token: var("TELEGRAM_BOT_TOKEN"),
|
||||||
|
telegram_chat_id: var("TELEGRAM_CHAT_ID"),
|
||||||
|
telegram_api_url: var("TELEGRAM_API_URL").unwrap_or_else(|| crate::telegram::DEFAULT_API_URL.into()),
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -20,7 +20,9 @@ mod compare;
|
|||||||
mod config;
|
mod config;
|
||||||
mod email;
|
mod email;
|
||||||
mod invoices;
|
mod invoices;
|
||||||
|
mod notify;
|
||||||
mod ses;
|
mod ses;
|
||||||
|
mod telegram;
|
||||||
mod templates {
|
mod templates {
|
||||||
pub use email_templates::*;
|
pub use email_templates::*;
|
||||||
}
|
}
|
||||||
@@ -153,6 +155,15 @@ async fn main() -> anyhow::Result<()> {
|
|||||||
}
|
}
|
||||||
None => tracing::warn!("BILLINGO_API_KEY is not set: invoices stay pending"),
|
None => tracing::warn!("BILLINGO_API_KEY is not set: invoices stay pending"),
|
||||||
}
|
}
|
||||||
|
match (&config.telegram_bot_token, &config.telegram_chat_id) {
|
||||||
|
_ if !tasks.notify => {}
|
||||||
|
(Some(token), Some(chat)) => {
|
||||||
|
tracing::info!("purchase notices enabled (Telegram)");
|
||||||
|
let telegram = telegram::Telegram::new(&config.telegram_api_url, token, chat);
|
||||||
|
jobs.spawn(notify::run(pool.clone(), telegram, config.poll_interval, stop.clone()));
|
||||||
|
}
|
||||||
|
_ => tracing::info!("TELEGRAM_BOT_TOKEN or TELEGRAM_CHAT_ID is not set: no purchase notices"),
|
||||||
|
}
|
||||||
|
|
||||||
let app = Router::new().route("/healthz", get(healthz)).with_state(pool);
|
let app = Router::new().route("/healthz", get(healthz)).with_state(pool);
|
||||||
let listener = tokio::net::TcpListener::bind(addr).await.with_context(|| format!("binding {addr}"))?;
|
let listener = tokio::net::TcpListener::bind(addr).await.with_context(|| format!("binding {addr}"))?;
|
||||||
|
|||||||
@@ -0,0 +1,176 @@
|
|||||||
|
//! The `notify` task: a Telegram notice to the operator about every paid
|
||||||
|
//! checkout ("Páros csomag, 6 000 Ft"), once. It runs on its own, apart from
|
||||||
|
//! invoicing, and carries no personal data of the buyer.
|
||||||
|
//!
|
||||||
|
//! A purchase counts as notified when Telegram took the message, or refused
|
||||||
|
//! it for good (a wrong token or chat: logged as an error); after an outage
|
||||||
|
//! or a rate limit it is tried again on the next round.
|
||||||
|
|
||||||
|
use std::time::Duration;
|
||||||
|
|
||||||
|
use sqlx::PgPool;
|
||||||
|
use tokio::sync::watch;
|
||||||
|
|
||||||
|
use crate::telegram::{Telegram, huf};
|
||||||
|
|
||||||
|
/// At most this many notices a round.
|
||||||
|
const BATCH: i64 = 20;
|
||||||
|
|
||||||
|
/// The notice of one purchase.
|
||||||
|
fn text(item: &str, price_huf: i32, live: bool) -> String {
|
||||||
|
// The invoice item without its service prefix ("Online tartalomszolgáltatás – ").
|
||||||
|
let item = item.rsplit(" – ").next().unwrap_or(item);
|
||||||
|
format!("🛒 Vásárlás: {item}, {}{}", huf(price_huf), if live { "" } else { " (teszt fizetés)" })
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Sends the notices of the purchases not notified yet; returns how many went out.
|
||||||
|
pub async fn run_once(db: &PgPool, telegram: &Telegram) -> Result<usize, sqlx::Error> {
|
||||||
|
let mut tx = db.begin().await?;
|
||||||
|
let due: Vec<(String, String, i32, bool)> = sqlx::query_as(
|
||||||
|
"SELECT c.id, coalesce(i.item_name, c.package), coalesce(c.amount_huf, c.price_huf), c.live \
|
||||||
|
FROM stripe_checkouts c LEFT JOIN invoices i ON i.checkout_id = c.id \
|
||||||
|
WHERE c.completed_at IS NOT NULL AND c.notified_at IS NULL \
|
||||||
|
ORDER BY c.completed_at LIMIT $1 FOR UPDATE OF c SKIP LOCKED",
|
||||||
|
)
|
||||||
|
.bind(BATCH)
|
||||||
|
.fetch_all(&mut *tx)
|
||||||
|
.await?;
|
||||||
|
let mut sent = 0;
|
||||||
|
for (id, item, price, live) in due {
|
||||||
|
match telegram.send(&text(&item, price, live)).await {
|
||||||
|
Ok(()) => sent += 1,
|
||||||
|
Err(e) if e.permanent => {
|
||||||
|
tracing::error!(checkout = %id, error = %e.message, "telegram refused the purchase notice; check TELEGRAM_*");
|
||||||
|
}
|
||||||
|
Err(e) => {
|
||||||
|
tracing::warn!(checkout = %id, error = %e.message, "purchase notice not sent yet");
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
sqlx::query("UPDATE stripe_checkouts SET notified_at = now() WHERE id = $1")
|
||||||
|
.bind(&id)
|
||||||
|
.execute(&mut *tx)
|
||||||
|
.await?;
|
||||||
|
}
|
||||||
|
tx.commit().await?;
|
||||||
|
Ok(sent)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub async fn run(db: PgPool, telegram: Telegram, interval: Duration, stop: watch::Receiver<bool>) {
|
||||||
|
loop {
|
||||||
|
if let Err(e) = run_once(&db, &telegram).await {
|
||||||
|
tracing::error!(error = %e, "purchase notices");
|
||||||
|
}
|
||||||
|
tokio::select! {
|
||||||
|
() = tokio::time::sleep(interval) => {}
|
||||||
|
() = crate::stopped(stop.clone()) => break,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod tests {
|
||||||
|
use std::sync::{Arc, Mutex};
|
||||||
|
|
||||||
|
use axum::extract::State;
|
||||||
|
use axum::http::StatusCode;
|
||||||
|
use axum::routing::post;
|
||||||
|
use axum::{Json, Router};
|
||||||
|
use serde_json::Value;
|
||||||
|
|
||||||
|
use super::*;
|
||||||
|
|
||||||
|
#[derive(Default)]
|
||||||
|
struct Fake {
|
||||||
|
messages: Mutex<Vec<Value>>,
|
||||||
|
/// The answer to give; `None`: 200.
|
||||||
|
status: Mutex<Option<StatusCode>>,
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn fake_telegram(fake: Arc<Fake>) -> String {
|
||||||
|
async fn send(State(f): State<Arc<Fake>>, Json(body): Json<Value>) -> StatusCode {
|
||||||
|
let status = *f.status.lock().unwrap();
|
||||||
|
if status.is_none() {
|
||||||
|
f.messages.lock().unwrap().push(body);
|
||||||
|
}
|
||||||
|
status.unwrap_or(StatusCode::OK)
|
||||||
|
}
|
||||||
|
let app = Router::new().route("/botTOKEN/sendMessage", post(send)).with_state(fake);
|
||||||
|
let listener = tokio::net::TcpListener::bind("127.0.0.1:0").await.unwrap();
|
||||||
|
let addr = listener.local_addr().unwrap();
|
||||||
|
tokio::spawn(async move { axum::serve(listener, app).await.unwrap() });
|
||||||
|
format!("http://{addr}")
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn checkout(db: &PgPool, package: &str, price: i32, live: bool, paid: bool) -> String {
|
||||||
|
let id = format!("cs_{}", uuid::Uuid::now_v7());
|
||||||
|
sqlx::query(
|
||||||
|
"INSERT INTO stripe_checkouts (id, package, price_huf, live, completed_at) \
|
||||||
|
VALUES ($1, $2, $3, $4, CASE WHEN $5 THEN now() END)",
|
||||||
|
)
|
||||||
|
.bind(&id)
|
||||||
|
.bind(package)
|
||||||
|
.bind(price)
|
||||||
|
.bind(live)
|
||||||
|
.bind(paid)
|
||||||
|
.execute(db)
|
||||||
|
.await
|
||||||
|
.unwrap();
|
||||||
|
id
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn notice_texts() {
|
||||||
|
assert_eq!(
|
||||||
|
text("Online tartalomszolgáltatás – Páros csomag", 6000, true),
|
||||||
|
"🛒 Vásárlás: Páros csomag, 6\u{a0}000\u{a0}Ft"
|
||||||
|
);
|
||||||
|
assert_eq!(
|
||||||
|
text("addon_questions", 1000, false),
|
||||||
|
"🛒 Vásárlás: addon_questions, 1\u{a0}000\u{a0}Ft (teszt fizetés)"
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[sqlx::test(migrator = "db::MIGRATOR")]
|
||||||
|
async fn each_purchase_is_announced_once(db: PgPool) {
|
||||||
|
let fake = Arc::new(Fake::default());
|
||||||
|
let telegram = Telegram::new(&fake_telegram(fake.clone()).await, "TOKEN", "42");
|
||||||
|
let paid = checkout(&db, "couple", 6000, false, true).await;
|
||||||
|
sqlx::query(
|
||||||
|
"INSERT INTO invoices (id, checkout_id, buyer_name, buyer_country, buyer_post_code, buyer_city, \
|
||||||
|
buyer_address, buyer_tax_type, item_name, gross_huf, net_huf, vat_huf) \
|
||||||
|
VALUES ($1, $2, 'Anna', 'HU', '6720', 'Szeged', 'Fő utca 1.', 'NO_TAX_NUMBER', \
|
||||||
|
'Online tartalomszolgáltatás – Páros csomag', 6000, 6000, 0)",
|
||||||
|
)
|
||||||
|
.bind(uuid::Uuid::now_v7())
|
||||||
|
.bind(&paid)
|
||||||
|
.execute(&db)
|
||||||
|
.await
|
||||||
|
.unwrap();
|
||||||
|
checkout(&db, "single", 2000, true, false).await; // not paid (yet)
|
||||||
|
|
||||||
|
// An outage: nothing is lost, it comes on the next round.
|
||||||
|
*fake.status.lock().unwrap() = Some(StatusCode::BAD_GATEWAY);
|
||||||
|
assert_eq!(run_once(&db, &telegram).await.unwrap(), 0);
|
||||||
|
*fake.status.lock().unwrap() = None;
|
||||||
|
assert_eq!(run_once(&db, &telegram).await.unwrap(), 1);
|
||||||
|
assert_eq!(run_once(&db, &telegram).await.unwrap(), 0);
|
||||||
|
let messages = fake.messages.lock().unwrap().clone();
|
||||||
|
assert_eq!(messages.len(), 1);
|
||||||
|
assert_eq!(messages[0]["chat_id"], "42");
|
||||||
|
assert_eq!(messages[0]["text"], "🛒 Vásárlás: Páros csomag, 6\u{a0}000\u{a0}Ft (teszt fizetés)");
|
||||||
|
assert!(!messages[0]["text"].as_str().unwrap().contains("Anna"), "no personal data");
|
||||||
|
|
||||||
|
// A wrong chat is not retried for ever.
|
||||||
|
checkout(&db, "addon_questions", 1000, true, true).await;
|
||||||
|
*fake.status.lock().unwrap() = Some(StatusCode::BAD_REQUEST);
|
||||||
|
assert_eq!(run_once(&db, &telegram).await.unwrap(), 0);
|
||||||
|
let left: i64 = sqlx::query_scalar(
|
||||||
|
"SELECT count(*) FROM stripe_checkouts WHERE completed_at IS NOT NULL AND notified_at IS NULL",
|
||||||
|
)
|
||||||
|
.fetch_one(&db)
|
||||||
|
.await
|
||||||
|
.unwrap();
|
||||||
|
assert_eq!(left, 0);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,72 @@
|
|||||||
|
//! Short notices to the operator's Telegram chat through a bot (Bot API
|
||||||
|
//! `sendMessage`).
|
||||||
|
|
||||||
|
use std::time::Duration;
|
||||||
|
|
||||||
|
use serde_json::json;
|
||||||
|
|
||||||
|
pub const DEFAULT_API_URL: &str = "https://api.telegram.org";
|
||||||
|
|
||||||
|
pub struct Telegram {
|
||||||
|
http: reqwest::Client,
|
||||||
|
api: String,
|
||||||
|
token: String,
|
||||||
|
chat_id: String,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Telegram {
|
||||||
|
pub fn new(api: &str, token: &str, chat_id: &str) -> Self {
|
||||||
|
let http = reqwest::Client::builder().timeout(Duration::from_secs(10)).build().expect("http client");
|
||||||
|
Self { http, api: api.trim_end_matches('/').to_owned(), token: token.to_owned(), chat_id: chat_id.to_owned() }
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Sends `text` as a plain message.
|
||||||
|
pub async fn send(&self, text: &str) -> Result<(), SendError> {
|
||||||
|
let url = format!("{}/bot{}/sendMessage", self.api, self.token);
|
||||||
|
let body = json!({ "chat_id": self.chat_id, "text": text, "disable_web_page_preview": true });
|
||||||
|
match self.http.post(&url).json(&body).send().await {
|
||||||
|
Ok(res) if res.status().is_success() => Ok(()),
|
||||||
|
Ok(res) => {
|
||||||
|
let status = res.status();
|
||||||
|
let detail: String = res.text().await.unwrap_or_default().chars().take(200).collect();
|
||||||
|
// A wrong token or chat stays wrong; rate limits and outages pass.
|
||||||
|
let permanent = status.is_client_error() && status != reqwest::StatusCode::TOO_MANY_REQUESTS;
|
||||||
|
Err(SendError { permanent, message: format!("{status}: {detail}") })
|
||||||
|
}
|
||||||
|
// The URL holds the bot token: keep it out of the message.
|
||||||
|
Err(e) => Err(SendError { permanent: false, message: e.without_url().to_string() }),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Debug)]
|
||||||
|
pub struct SendError {
|
||||||
|
/// Retrying will not help (e.g. a wrong token or chat ID).
|
||||||
|
pub permanent: bool,
|
||||||
|
pub message: String,
|
||||||
|
}
|
||||||
|
|
||||||
|
/// "6 000 Ft"
|
||||||
|
pub fn huf(amount: i32) -> String {
|
||||||
|
let digits = amount.unsigned_abs().to_string();
|
||||||
|
let mut out = String::new();
|
||||||
|
for (i, c) in digits.chars().enumerate() {
|
||||||
|
if i > 0 && (digits.len() - i).is_multiple_of(3) {
|
||||||
|
out.push('\u{a0}');
|
||||||
|
}
|
||||||
|
out.push(c);
|
||||||
|
}
|
||||||
|
format!("{}{out}\u{a0}Ft", if amount < 0 { "-" } else { "" })
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod tests {
|
||||||
|
use super::huf;
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn forints() {
|
||||||
|
assert_eq!(huf(990), "990\u{a0}Ft");
|
||||||
|
assert_eq!(huf(6000), "6\u{a0}000\u{a0}Ft");
|
||||||
|
assert_eq!(huf(1234567), "1\u{a0}234\u{a0}567\u{a0}Ft");
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -99,7 +99,7 @@ async fn delivers_retries_and_gives_up(db: PgPool) {
|
|||||||
let d = delivery(db.clone(), vec![("flaky@example.com", false), ("rejected@example.com", true)]);
|
let d = delivery(db.clone(), vec![("flaky@example.com", false), ("rejected@example.com", true)]);
|
||||||
|
|
||||||
assert_eq!(d.deliver_due().await.unwrap(), 4);
|
assert_eq!(d.deliver_due().await.unwrap(), 4);
|
||||||
assert_eq!(sent(&d), vec![("anna@example.com".into(), "Erősítsd meg az e-mail-címed".into())]);
|
assert_eq!(sent(&d), vec![("anna@example.com".into(), "Add meg a jelszavad".into())]);
|
||||||
assert_eq!(state(&db, ok).await, (1, true, false, None, false));
|
assert_eq!(state(&db, ok).await, (1, true, false, None, false));
|
||||||
// Temporary failure: retried later, not at once.
|
// Temporary failure: retried later, not at once.
|
||||||
assert_eq!(state(&db, flaky).await, (1, false, false, Some("test failure".into()), true));
|
assert_eq!(state(&db, flaky).await, (1, false, false, Some("test failure".into()), true));
|
||||||
@@ -228,12 +228,13 @@ async fn cleanup_removes_only_expired_data(db: PgPool) {
|
|||||||
fn worker_tasks_are_chosen_by_name() {
|
fn worker_tasks_are_chosen_by_name() {
|
||||||
use crate::config::Tasks;
|
use crate::config::Tasks;
|
||||||
let mail = Tasks::parse("email, cleanup").unwrap();
|
let mail = Tasks::parse("email, cleanup").unwrap();
|
||||||
assert_eq!(mail, Tasks { email: true, cleanup: true, analyses: false, invoices: false });
|
assert_eq!(mail, Tasks { email: true, cleanup: true, analyses: false, invoices: false, notify: false });
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
Tasks::parse("analyses").unwrap(),
|
Tasks::parse("analyses").unwrap(),
|
||||||
Tasks { email: false, cleanup: false, analyses: true, invoices: false }
|
Tasks { email: false, cleanup: false, analyses: true, invoices: false, notify: false }
|
||||||
);
|
);
|
||||||
assert!(Tasks::parse("email,invoices").unwrap().invoices);
|
assert!(Tasks::parse("email,invoices").unwrap().invoices);
|
||||||
|
assert!(Tasks::parse("email,cleanup,invoices,notify").unwrap().notify);
|
||||||
assert!(Tasks::parse("emails").is_err());
|
assert!(Tasks::parse("emails").is_err());
|
||||||
assert!(Tasks::parse(" , ").is_err());
|
assert!(Tasks::parse(" , ").is_err());
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user