-
Notifications
You must be signed in to change notification settings - Fork 4
/
index.ts
116 lines (114 loc) · 3.47 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
import { PatternMatcher, compilePattern } from './PatternMatcher'
const smsMatcherProviders: { [provider: string]: SMSMatcher[] } = {
'TrueMoney Wallet': [
// == TrueMoney ==
match(
'TrueMoney',
'ชำระเงิน {{amount:number}}บ. ให้ {{to}} คงเหลือ {{balance:number}}บ. ({{transactionId}})',
{ type: 'pay' }
),
match(
'TrueMoney',
'You have paid {{amount:number}} Baht using {{via}}. Your balance is {{balance:number}} Baht (transaction {{transactionId}})',
{ type: 'pay' }
)
],
KBANK: [
match(
'KBank',
'{{date:kbank_date}} {{time:time}} {{from}} transferred {{amount:number}} Baht to {{to}} Outstanding Balance {{balance:number}} Baht.',
{ type: 'transfer' }
),
match(
'KBank',
'{{date:kbank_date}} {{time:time}} {{from}} transferred {{amount:number}} Baht to {{to}}',
{ type: 'transfer' }
),
match(
'KBank',
'{{date:kbank_date}} {{time:time}} {{from}} Withdrawal{{amount:number}} Outstanding Balance{{balance:number}} Baht.',
{ type: 'withdraw' }
),
match(
'KBank',
'{{date:kbank_date}} {{time:time}} {{from}} Withdrawal{{amount:number}} Baht.',
{ type: 'withdraw' }
),
match(
'KBank',
'{{date:kbank_date}} {{time:time}} {{to}} Deposit{{amount:number}} Outstanding Balance{{balance:number}} Baht.',
{ type: 'deposit' }
),
match(
'KBank',
'{{date:kbank_date}} {{time:time}} {{to}} Deposit{{amount:number}} Baht.',
{ type: 'deposit' }
),
match(
'KBank',
'{{date:kbank_date}} {{time:time}} {{from}} credited {{amount:number}} Baht to {{to}} Outstanding Balance {{balance:number}} Baht.',
{ type: 'credit' }
),
match(
'KBank',
'{{date:kbank_date}} {{time:time}} {{from}} credited {{amount:number}} Baht to {{to}}',
{ type: 'credit' }
),
match(
'KBank',
'{{date:kbank_date}} {{time:time}} {{to}} received {{amount:number}} Baht from {{from}} Outstanding Balance {{balance:number}} Baht.',
{ type: 'receive' }
),
match(
'KBank',
'{{date:kbank_date}} {{time:time}} {{to}} received {{amount:number}} Baht from {{from}}',
{ type: 'receive' }
),
match(
'KBank',
'{{date:kbank_date}} {{time:time}} {{subject}} Outstanding Balance {{balance:number}} Baht.',
{ type: 'info' }
),
match(
'KBank',
'ชำระ {{amount:number}}บ บัตร {{from}}@{{to}} {{time:time}}น เหลือ {{balance:number}}บ',
{ type: 'pay' }
),
match(
'KBank',
'คืนเงิน {{amount:number}}บ บัตร {{to}}@{{from}} {{time:time}}น เหลือ {{balance:number}}บ',
{ type: 'refund' }
)
]
}
export function parseSMS(text: string, sender?: string) {
for (const provider of Object.keys(smsMatcherProviders)) {
for (const smsMatcher of smsMatcherProviders[provider]) {
if (sender && sender !== smsMatcher.from) {
continue
}
const result = smsMatcher.patternMatcher.execute(text)
if (!result) {
continue
}
return {
provider,
...smsMatcher.extra,
...result
}
}
}
return null
}
type SMSMatcher = {
from: string
patternMatcher: PatternMatcher
extra: any
}
function match(from: string, pattern: string, extra: any): SMSMatcher {
return {
from,
patternMatcher: compilePattern(pattern),
extra
}
}