create-gramstax 0.7.0 → 0.8.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/src/templates/package.json +1 -1
- package/dist/src/templates/src/env.ts +4 -4
- package/dist/src/templates/src/{utils → helpers}/log.ts +1 -1
- package/dist/src/templates/src/{utils → helpers}/time.ts +1 -1
- package/dist/src/templates/src/index.ts +16 -14
- package/dist/src/templates/src/pages/blank.ts +9 -0
- package/dist/src/templates/src/pages/general-status.ts +2 -2
- package/dist/src/templates/src/pages/start.ts +3 -3
- package/dist/src/templates/src/pages/sum-number.ts +17 -8
- package/dist/src/templates/src/utils/array.ts +24 -0
- package/package.json +1 -1
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
export
|
|
2
|
-
TELEGRAM_BOT_TOKEN
|
|
3
|
-
TELEGRAM_BOT_DEPLOY
|
|
4
|
-
TELEGRAM_CACHE_SESSION
|
|
1
|
+
export class Env {
|
|
2
|
+
static TELEGRAM_BOT_TOKEN = process.env.TELEGRAM_BOT_TOKEN as string
|
|
3
|
+
static TELEGRAM_BOT_DEPLOY = process.env.TELEGRAM_BOT_DEPLOY as string
|
|
4
|
+
static TELEGRAM_CACHE_SESSION = process.env.TELEGRAM_CACHE_SESSION as string
|
|
5
5
|
}
|
|
@@ -1,2 +1,2 @@
|
|
|
1
1
|
import {log} from "gramstax"
|
|
2
|
-
export {log}
|
|
2
|
+
export {log as LogHe}
|
|
@@ -1,20 +1,22 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
1
|
+
import {Env} from "./env"
|
|
2
|
+
import {LogHe} from "./helpers/log"
|
|
3
3
|
import {BotCore} from "./core/bot"
|
|
4
4
|
import {CacheExternal} from "gramstax"
|
|
5
5
|
|
|
6
|
-
|
|
6
|
+
class Index {
|
|
7
|
+
static async run() {
|
|
8
|
+
LogHe.info(`Bot started..`)
|
|
7
9
|
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
}
|
|
17
|
-
}
|
|
10
|
+
new BotCore({
|
|
11
|
+
log: LogHe,
|
|
12
|
+
token: Env.TELEGRAM_BOT_TOKEN,
|
|
13
|
+
deploy: Env.TELEGRAM_BOT_DEPLOY,
|
|
14
|
+
cacheSession: new CacheExternal(Env.TELEGRAM_CACHE_SESSION, 4 * 60 * 60 * 1000, `s`), // 4 hours
|
|
15
|
+
optionsPage: {
|
|
16
|
+
shortCallbackData: true
|
|
17
|
+
}
|
|
18
|
+
})
|
|
19
|
+
}
|
|
18
20
|
}
|
|
19
21
|
|
|
20
|
-
run()
|
|
22
|
+
Index.run()
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import {LogHe} from "~/helpers/log"
|
|
2
2
|
import {PageBase} from "~/base/page"
|
|
3
3
|
import {StartPage} from "./start"
|
|
4
4
|
|
|
@@ -15,7 +15,7 @@ export class GeneralStatusPage extends PageBase {
|
|
|
15
15
|
}
|
|
16
16
|
|
|
17
17
|
async toError(funcName: string, error: any, edit = true) {
|
|
18
|
-
|
|
18
|
+
LogHe.errorMake(error, this.constructorName, funcName)
|
|
19
19
|
|
|
20
20
|
await this.clearSession()
|
|
21
21
|
const kb = this.kb()
|
|
@@ -15,9 +15,9 @@ export class StartPage extends PageBase {
|
|
|
15
15
|
async show(edit = true) {
|
|
16
16
|
await this.clearSession()
|
|
17
17
|
const kb = this.inlineKeyboardT([RandomNumberPage, SumNumberPage, HelpPage], `2`)
|
|
18
|
-
const
|
|
19
|
-
if (edit) await this.edit(kb,
|
|
20
|
-
else await this.reply(kb,
|
|
18
|
+
const da = {userName: this.userName}
|
|
19
|
+
if (edit) await this.edit(kb, da)
|
|
20
|
+
else await this.reply(kb, da)
|
|
21
21
|
}
|
|
22
22
|
|
|
23
23
|
static template = `
|
|
@@ -22,17 +22,23 @@ export class SumNumberPage extends PageBase {
|
|
|
22
22
|
if (step === `left`) {
|
|
23
23
|
if (!Number.isFinite(value)) {
|
|
24
24
|
await this.sessionFreeText({step: `left`})
|
|
25
|
-
|
|
25
|
+
const bi = BASE.pleft
|
|
26
|
+
const kb = this.kb(bi)
|
|
27
|
+
return await this.reply(kb, {}, bi)
|
|
26
28
|
}
|
|
27
29
|
|
|
28
30
|
await this.sessionFreeText({step: `right`, left: value})
|
|
29
|
-
|
|
31
|
+
const bi = BASE.pright
|
|
32
|
+
const kb = this.kb(bi)
|
|
33
|
+
return await this.reply(kb, {}, bi)
|
|
30
34
|
}
|
|
31
35
|
|
|
32
36
|
if (step === `right`) {
|
|
33
37
|
if (!Number.isFinite(value)) {
|
|
34
38
|
await this.sessionFreeText({step: `right`, left})
|
|
35
|
-
|
|
39
|
+
const bi = BASE.pright
|
|
40
|
+
const kb = this.kb(bi)
|
|
41
|
+
return await this.reply(kb, {}, bi)
|
|
36
42
|
}
|
|
37
43
|
|
|
38
44
|
if (!Number.isFinite(left ?? NaN)) {
|
|
@@ -40,19 +46,22 @@ export class SumNumberPage extends PageBase {
|
|
|
40
46
|
return await this.first(false)
|
|
41
47
|
}
|
|
42
48
|
|
|
43
|
-
const result = left! + value
|
|
44
49
|
await this.clearSession()
|
|
45
|
-
|
|
50
|
+
const bi = BASE.result
|
|
51
|
+
const kb = this.kb(bi)
|
|
52
|
+
const da = {result: left! + value}
|
|
53
|
+
await this.reply(kb, da, bi)
|
|
46
54
|
}
|
|
47
55
|
}
|
|
48
56
|
|
|
49
57
|
private async first(edit: boolean) {
|
|
50
58
|
await this.sessionFreeText({step: `left`})
|
|
51
59
|
const {BASE} = SumNumberPage
|
|
52
|
-
const
|
|
60
|
+
const bi = BASE.pleft
|
|
61
|
+
const kb = this.kb(bi)
|
|
53
62
|
|
|
54
|
-
if (edit) await this.edit(kb, {},
|
|
55
|
-
else await this.reply(kb, {},
|
|
63
|
+
if (edit) await this.edit(kb, {}, bi)
|
|
64
|
+
else await this.reply(kb, {}, bi)
|
|
56
65
|
}
|
|
57
66
|
|
|
58
67
|
private kb(baseId: string) {
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
export class ArrayUt {
|
|
2
|
+
/**
|
|
3
|
+
* 1. Mengambil satu elemen secara acak
|
|
4
|
+
*/
|
|
5
|
+
static random(array: any[]) {
|
|
6
|
+
if (!Array.isArray(array) || array.length === 0) return undefined
|
|
7
|
+
const randomIndex = Math.floor(Math.random() * array.length)
|
|
8
|
+
return array[randomIndex]
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* 2. Mengacak urutan array (Fisher-Yates Shuffle)
|
|
13
|
+
* Menghasilkan array baru agar tidak merubah array asli (immutable)
|
|
14
|
+
*/
|
|
15
|
+
static shuffle(array: any[]) {
|
|
16
|
+
if (!Array.isArray(array)) return []
|
|
17
|
+
const newArray = [...array] // Clone array
|
|
18
|
+
for (let i = newArray.length - 1; i > 0; i--) {
|
|
19
|
+
const j = Math.floor(Math.random() * (i + 1))
|
|
20
|
+
;[newArray[i], newArray[j]] = [newArray[j], newArray[i]]
|
|
21
|
+
}
|
|
22
|
+
return newArray
|
|
23
|
+
}
|
|
24
|
+
}
|