|
| 1 | +// SPDX-License-Identifier: PMPL-1.0-or-later |
| 2 | +// Copyright (c) 2026 Jonathan D.A. Jewell <j.d.a.jewell@open.ac.uk> |
| 3 | +// |
| 4 | +// STAMP Telegram Bot. |
| 5 | +// AffineScript port of bot.ts. Compiled via --target node-cjs (affinescript#35). |
| 6 | +// |
| 7 | +// Callback handlers are registered as wasm table index references (@fn_name). |
| 8 | +// The Grammy Node-CJS shim calls back into wasm when Telegram delivers a command. |
| 9 | +// Async operations (ctx.reply, bot.api.sendMessage) are handled by the shim; |
| 10 | +// the wasm code sees them as synchronous extern fn calls. |
| 11 | + |
| 12 | +module Bot; |
| 13 | + |
| 14 | +extern type Bot; |
| 15 | +extern type Context; |
| 16 | +extern type BotInfo; |
| 17 | +extern type Db; |
| 18 | +extern fn bot_new(token: String) -> Bot; |
| 19 | +extern fn bot_command(b: Bot, command: String, handler: Int) -> Int; |
| 20 | +extern fn bot_catch(b: Bot, err_handler: Int) -> Int; |
| 21 | +extern fn bot_start(b: Bot, on_start_handler: Int) -> Int; |
| 22 | +extern fn ctx_from_id(ctx: Context) -> Option<Int>; |
| 23 | +extern fn ctx_from_username(ctx: Context) -> Option<String>; |
| 24 | +extern fn ctx_reply(ctx: Context, text: String, parse_mode: String) -> Int; |
| 25 | +extern fn botinfo_username(info: BotInfo) -> String; |
| 26 | +extern fn set_interval(handler: Int, interval_ms: Int) -> Int; |
| 27 | +extern fn db_open(path: String) -> Db; |
| 28 | +extern fn db_execute(d: Db, sql: String) -> Int; |
| 29 | +extern fn db_query_int(d: Db, sql: String, params_json: String) -> Int; |
| 30 | +extern fn db_query_one(d: Db, sql: String, params_json: String) -> String; |
| 31 | +extern fn db_query(d: Db, sql: String, params_json: String) -> String; |
| 32 | +extern fn time_ms() -> Int; |
| 33 | +extern fn random_string(n: Int) -> String; |
| 34 | +extern fn getenv(name: String) -> Option<String>; |
| 35 | +extern fn process_exit(code: Int) -> Int; |
| 36 | +extern fn log(s: String) -> Int; |
| 37 | + |
| 38 | +// __ Startup _______________________________________________________________ |
| 39 | + |
| 40 | +pub fn main() -> Int { |
| 41 | + let token = match getenv("BOT_TOKEN") { |
| 42 | + Some(t) => t, |
| 43 | + None => { |
| 44 | + log("Error: BOT_TOKEN environment variable not set"); |
| 45 | + process_exit(1); |
| 46 | + "" |
| 47 | + } |
| 48 | + }; |
| 49 | + |
| 50 | + let bot = bot_new(token); |
| 51 | + |
| 52 | + log("STAMP Telegram Bot starting..."); |
| 53 | + |
| 54 | + bot_command(bot, "start", @handle_start); |
| 55 | + bot_command(bot, "verify", @handle_verify); |
| 56 | + bot_command(bot, "unsubscribe", @handle_unsubscribe); |
| 57 | + bot_command(bot, "status", @handle_status); |
| 58 | + bot_command(bot, "help", @handle_help); |
| 59 | + |
| 60 | + set_interval(@send_demo_messages, 3600000); |
| 61 | + bot_catch(bot, @handle_error); |
| 62 | + bot_start(bot, @handle_bot_start); |
| 63 | + 0 |
| 64 | +} |
| 65 | + |
| 66 | +pub fn handle_bot_start(info: BotInfo) -> Int { |
| 67 | + log("Connected as @" ++ botinfo_username(info)); |
| 68 | + log("Polling for messages..."); |
| 69 | + 0 |
| 70 | +} |
| 71 | + |
| 72 | +pub fn handle_error(err_msg: String) -> Int { |
| 73 | + log("Bot error: " ++ err_msg); |
| 74 | + 0 |
| 75 | +} |
| 76 | + |
| 77 | +// __ DB helpers ____________________________________________________________ |
| 78 | + |
| 79 | +fn open_db() -> Db { |
| 80 | + db_open("./db/stamp-bot.db") |
| 81 | +} |
| 82 | + |
| 83 | +fn is_subscribed(db: Db, uid: Int) -> Bool { |
| 84 | + let p = "[" ++ show(uid) ++ "]"; |
| 85 | + db_query_int(db, "SELECT COUNT(*) FROM users WHERE telegram_id=? AND subscribed=1", p) > 0 |
| 86 | +} |
| 87 | + |
| 88 | +fn get_token(db: Db, uid: Int) -> String { |
| 89 | + let p = "[" ++ show(uid) ++ "]"; |
| 90 | + let row = db_query_one(db, "SELECT consent_token FROM users WHERE telegram_id=?", p); |
| 91 | + if row == "null" { "" } else { row } |
| 92 | +} |
| 93 | + |
| 94 | +fn subscribe(db: Db, uid: Int, username: String, token: String, proof: String) -> Int { |
| 95 | + let now = time_ms(); |
| 96 | + db_execute(db, "INSERT INTO users (telegram_id,username,subscribed,consent_timestamp,consent_token,consent_proof,created_at,updated_at) VALUES (" ++ show(uid) ++ ",'" ++ username ++ "',1," ++ show(now) ++ ",'" ++ token ++ "','" ++ proof ++ "'," ++ show(now) ++ "," ++ show(now) ++ ") ON CONFLICT(telegram_id) DO UPDATE SET subscribed=1,consent_timestamp=" ++ show(now) ++ ",consent_token='" ++ token ++ "',consent_proof='" ++ proof ++ "',updated_at=" ++ show(now)) |
| 97 | +} |
| 98 | + |
| 99 | +fn unsubscribe(db: Db, uid: Int) -> Int { |
| 100 | + let now = time_ms(); |
| 101 | + db_execute(db, "UPDATE users SET subscribed=0,updated_at=" ++ show(now) ++ " WHERE telegram_id=" ++ show(uid)) |
| 102 | +} |
| 103 | + |
| 104 | +// __ /start ________________________________________________________________ |
| 105 | + |
| 106 | +pub fn handle_start(ctx: Context) -> Int { |
| 107 | + let uid = match ctx_from_id(ctx) { |
| 108 | + Some(id) => id, |
| 109 | + None => { ctx_reply(ctx, "Error: Could not identify user", ""); return 0; } |
| 110 | + }; |
| 111 | + let username = match ctx_from_username(ctx) { Some(u) => u, None => "" }; |
| 112 | + let db = open_db(); |
| 113 | + |
| 114 | + if is_subscribed(db, uid) { |
| 115 | + ctx_reply(ctx, |
| 116 | + "You are already subscribed!\\n\\nUse /status or /unsubscribe.", |
| 117 | + "Markdown"); |
| 118 | + return 0; |
| 119 | + } |
| 120 | + |
| 121 | + let now = time_ms(); |
| 122 | + let token = show(uid) ++ "_" ++ show(now) ++ "_" ++ random_string(13); |
| 123 | + let proof = "{\"type\":\"consent_verification\",\"timestamp\":" ++ show(now) ++ "}"; |
| 124 | + |
| 125 | + subscribe(db, uid, username, token, proof); |
| 126 | + |
| 127 | + ctx_reply(ctx, |
| 128 | + "*Subscription Confirmed*\\n\\n" ++ |
| 129 | + "Token: " ++ string_sub(token, 0, 20) ++ "...\\n" ++ |
| 130 | + "Proof: Cryptographically signed\\n\\n" ++ |
| 131 | + "/verify - Show proof for last message\\n" ++ |
| 132 | + "/status - Show subscription status\\n" ++ |
| 133 | + "/unsubscribe - Unsubscribe", |
| 134 | + "Markdown"); |
| 135 | + 0 |
| 136 | +} |
| 137 | + |
| 138 | +// __ /verify _______________________________________________________________ |
| 139 | + |
| 140 | +pub fn handle_verify(ctx: Context) -> Int { |
| 141 | + let uid = match ctx_from_id(ctx) { |
| 142 | + Some(id) => id, |
| 143 | + None => { return 0; } |
| 144 | + }; |
| 145 | + let db = open_db(); |
| 146 | + let p = "[" ++ show(uid) ++ "]"; |
| 147 | + let msg = db_query_one(db, "SELECT subject,sent_at,proof FROM messages WHERE telegram_id=? ORDER BY sent_at DESC LIMIT 1", p); |
| 148 | + if msg == "null" { |
| 149 | + ctx_reply(ctx, "No messages to verify yet. You will receive a demo message soon!", ""); |
| 150 | + } else { |
| 151 | + ctx_reply(ctx, |
| 152 | + "*STAMP Verification Proof*\\n\\n" ++ |
| 153 | + "Last message proof:\\n```json\\n" ++ msg ++ "\\n```\\n\\n" ++ |
| 154 | + "This proof is cryptographically signed.", |
| 155 | + "Markdown"); |
| 156 | + } |
| 157 | + 0 |
| 158 | +} |
| 159 | + |
| 160 | +// __ /unsubscribe __________________________________________________________ |
| 161 | + |
| 162 | +pub fn handle_unsubscribe(ctx: Context) -> Int { |
| 163 | + let uid = match ctx_from_id(ctx) { |
| 164 | + Some(id) => id, |
| 165 | + None => { return 0; } |
| 166 | + }; |
| 167 | + let db = open_db(); |
| 168 | + if !is_subscribed(db, uid) { |
| 169 | + ctx_reply(ctx, "You are not currently subscribed. Use /start to subscribe.", ""); |
| 170 | + return 0; |
| 171 | + } |
| 172 | + unsubscribe(db, uid); |
| 173 | + ctx_reply(ctx, |
| 174 | + "*Unsubscribed Successfully*\\n\\n" ++ |
| 175 | + "You will NOT receive future messages.\\n" ++ |
| 176 | + "Use /start to re-subscribe anytime.", |
| 177 | + "Markdown"); |
| 178 | + 0 |
| 179 | +} |
| 180 | + |
| 181 | +// __ /status _______________________________________________________________ |
| 182 | + |
| 183 | +pub fn handle_status(ctx: Context) -> Int { |
| 184 | + let uid = match ctx_from_id(ctx) { |
| 185 | + Some(id) => id, |
| 186 | + None => { return 0; } |
| 187 | + }; |
| 188 | + let db = open_db(); |
| 189 | + let user = db_query_one(db, "SELECT telegram_id,subscribed,consent_token FROM users WHERE telegram_id=?", |
| 190 | + "[" ++ show(uid) ++ "]"); |
| 191 | + if user == "null" { |
| 192 | + ctx_reply(ctx, "No subscription found. Use /start to subscribe.", ""); |
| 193 | + } else { |
| 194 | + let total = db_query_int(db, "SELECT COUNT(*) FROM users", "[]"); |
| 195 | + let active = db_query_int(db, "SELECT COUNT(*) FROM users WHERE subscribed=1", "[]"); |
| 196 | + let msgs = db_query_int(db, "SELECT COUNT(*) FROM messages WHERE telegram_id=?", |
| 197 | + "[" ++ show(uid) ++ "]"); |
| 198 | + ctx_reply(ctx, |
| 199 | + "*Your STAMP Subscription*\\n\\n" ++ |
| 200 | + "Messages received: " ++ show(msgs) ++ "\\n\\n" ++ |
| 201 | + "*Bot Statistics:*\\n" ++ |
| 202 | + "Total users: " ++ show(total) ++ "\\n" ++ |
| 203 | + "Active subscriptions: " ++ show(active), |
| 204 | + "Markdown"); |
| 205 | + } |
| 206 | + 0 |
| 207 | +} |
| 208 | + |
| 209 | +// __ /help _________________________________________________________________ |
| 210 | + |
| 211 | +pub fn handle_help(ctx: Context) -> Int { |
| 212 | + ctx_reply(ctx, |
| 213 | + "*STAMP Protocol Demo Bot*\\n\\n" ++ |
| 214 | + "Demonstrates STAMP protocol verification.\\n\\n" ++ |
| 215 | + "*Commands:*\\n" ++ |
| 216 | + "/start - Subscribe to demo messages\\n" ++ |
| 217 | + "/verify - Show proof for last message\\n" ++ |
| 218 | + "/status - Show subscription details\\n" ++ |
| 219 | + "/unsubscribe - Unsubscribe (one-click, proven)\\n" ++ |
| 220 | + "/help - Show this help", |
| 221 | + "Markdown"); |
| 222 | + 0 |
| 223 | +} |
| 224 | + |
| 225 | +// __ Periodic demo messages ________________________________________________ |
| 226 | + |
| 227 | +pub fn send_demo_messages() -> Int { |
| 228 | + let db = open_db(); |
| 229 | + let users = db_query(db, "SELECT telegram_id,consent_token FROM users WHERE subscribed=1", "[]"); |
| 230 | + log("Sending demo messages to subscribed users..."); |
| 231 | + 0 |
| 232 | +} |
0 commit comments