Intelligence, Advanced Charting & Trading Platform
test(ops): add opt-in single-message Bale verification with dry run and redacted responses
davoodmehraban89 committed
Oct 15, 2026 at 19:29 UTC
80c6f3935f0d62dd67884c4b8c1ab77f3dcd7f6c
1 file changed
+86
scripts/diagnostics/bale-one-message-test.mjs
new
+86
@@ -0,0 +1,86 @@
1
+/**
2
+ * Owner-operated ONE-message Bale diagnostic. No execution unless --send.
3
+ * Keep BALE_BOT_TOKEN exclusively in a local shell secret environment; never
4
+ * paste to chat, terminal arguments, public GitHub logs or CI.
5
+ *
6
+ * --dry-run : no network (default)
7
+ * --discover: identify ONE consenting chat by a nonce-bearing /start message
8
+ * --send : explicitly send ONE message, never auto-retry
9
+ */
10
+import {pathToFileURL} from 'node:url';
11
+
12
+const HOST='https://tapi.bale.ai';
13
+const MODE_SET=new Set(['--dry-run','--discover','--send']);
14
+export function tokenOK(value) {
15
+ return typeof value==='string'&&/^[0-9]{4,20}:[A-Za-z0-9_-]{12,180}$/.test(value);
16
+}
17
+export function chatOK(value){
18
+ return typeof value==='string'&&/^-?[0-9]{1,17}$/.test(value)&&value!=='0';
19
+}
20
+export function safeSummary(result,httpStatus,expectedChat){
21
+ if(!result||result.ok!==true||httpStatus<200||httpStatus>=300) {
22
+ return {status:'FAILED',httpStatus:Number(httpStatus)||null,reason:'bale_api_error_redacted'};
23
+ }
24
+ const got=result.result?.chat?.id;
25
+ const msg=result.result?.message_id;
26
+ if(!Number.isSafeInteger(msg)||String(got)!==expectedChat)
27
+ return {status:'FAILED',httpStatus,reason:'chat_or_message_id_mismatch'};
28
+ return {status:'Bale_API_ACCEPTED_NOT_READ_CONFIRMED',httpStatus,messageId:msg};
29
+}
30
+export async function postMethod(method,body,{token,transport=fetch}){
31
+ if(!tokenOK(token)||!['getUpdates','sendMessage'].includes(method))
32
+ throw Error('Invalid Bale request inputs');
33
+ const response=await transport(HOST+'/bot'+token+'/'+method,{
34
+ method:'POST',
35
+ headers:{'content-type':'application/json'},
36
+ body:JSON.stringify(body),
37
+ signal:AbortSignal.timeout(12000),
38
+ redirect:'error'
39
+ });
40
+ const status=response.status;
41
+ let data;
42
+ try{data=await response.json()}catch{data=null}
43
+ // Do not log original URL, token, response raw body, incoming message text.
44
+ return {status,data};
45
+}
46
+export async function singleTest({mode='--dry-run',env=process.env,transport=fetch}={}){
47
+ if(!MODE_SET.has(mode))throw Error('Unknown mode');
48
+ if(mode==='--dry-run')
49
+ return {status:'NOT_SENT',required:['BALE_BOT_TOKEN','BALE_CHAT_ID','BALE_TEST_CONSENT=I_OWN_THIS_CHAT'],
50
+ docs:'https://docs.bale.ai/'};
51
+ const token=env.BALE_BOT_TOKEN;
52
+ if(!tokenOK(token))throw Error('BALE_BOT_TOKEN missing or malformed; do not print token');
53
+ if(mode==='--discover'){
54
+ const nonce=env.BALE_DISCOVERY_NONCE;
55
+ if(typeof nonce!=='string'||!/^[-_A-Za-z0-9]{8,64}$/.test(nonce))
56
+ throw Error('BALE_DISCOVERY_NONCE must be a random 8-64 character value');
57
+ const r=await postMethod('getUpdates',{limit:100,timeout:0},{token,transport});
58
+ if(r.status!==200||r.data?.ok!==true)
59
+ return {status:'FAILED',reason:'getUpdates_not_available_or_webhook_active',httpStatus:r.status};
60
+ const matches=(r.data.result||[]).filter(u=>{
61
+ const text=u?.message?.text||'';
62
+ return text===('/start '+nonce)||text===('CHARTNOVA_TEST_'+nonce);
63
+ }).map(u=>String(u.message.chat.id));
64
+ const unique=[...new Set(matches)];
65
+ return unique.length===1?{status:'CHAT_FOUND',chatId:unique[0]}:
66
+ {status:'NOT_VERIFIED',reason:'need_one_unambiguous_user_opt_in_message'};
67
+ }
68
+ if(env.BALE_TEST_CONSENT!=='I_OWN_THIS_CHAT')
69
+ throw Error('Explicit BALE_TEST_CONSENT required');
70
+ const chatId=env.BALE_CHAT_ID;
71
+ if(!chatOK(chatId))throw Error('BALE_CHAT_ID missing or malformed');
72
+ const text='[ChartNova] پیام آزمایشی کانال بله. این یک آزمون ارتباطی است؛ هشدار قیمت یا توصیه مالی نیست.';
73
+ const r=await postMethod('sendMessage',{chat_id:chatId,text},{token,transport});
74
+ return safeSummary(r.data,r.status,chatId);
75
+}
76
+if(process.argv[1]&&pathToFileURL(process.argv[1]).href===import.meta.url){
77
+ try{
78
+ const mode=process.argv[2]||'--dry-run';
79
+ const result=await singleTest({mode});
80
+ console.log(JSON.stringify(result));
81
+ if(['FAILED','NOT_VERIFIED'].includes(result.status))process.exitCode=1;
82
+ }catch(e){
83
+ console.error(JSON.stringify({status:'FAILED',reason:String(e.message).slice(0,130)}));
84
+ process.exitCode=1;
85
+ }
86
+}