create-gramstax 0.3.21 → 0.4.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/base/page.ts +3 -3
- package/dist/src/templates/src/{components → blocks}/keyboard.ts +4 -4
- package/dist/src/templates/src/core/bot.ts +6 -7
- package/dist/src/templates/src/env.ts +3 -3
- package/dist/src/templates/src/guards/user.ts +7 -3
- package/dist/src/templates/src/index.ts +3 -3
- package/dist/src/templates/src/{data → labels}/keyboard.ts +2 -2
- package/dist/src/templates/src/pages/general-status.ts +64 -0
- package/dist/src/templates/src/pages/help.ts +8 -11
- package/dist/src/templates/src/pages/input-text.ts +5 -5
- package/dist/src/templates/src/pages/start.ts +7 -10
- package/dist/src/templates/src/pages/username-notfound.ts +5 -8
- package/package.json +1 -1
- package/dist/src/templates/src/pages/general-error-input-notfound.ts +0 -45
- package/dist/src/templates/src/pages/general-error.ts +0 -53
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import {KeyboardBlock} from "~/blocks/keyboard"
|
|
2
2
|
import {CtxCore} from "~/core/ctx"
|
|
3
3
|
|
|
4
4
|
export abstract class PageBase extends CtxCore {
|
|
5
|
-
static
|
|
6
|
-
static
|
|
5
|
+
static kbk = KeyboardBlock
|
|
6
|
+
static kbl = KeyboardBlock.label
|
|
7
7
|
}
|
|
@@ -1,8 +1,8 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
1
|
+
import {BlockKeyboard} from "gramstax"
|
|
2
|
+
import {KeyboardLabel} from "~/labels/keyboard"
|
|
3
3
|
|
|
4
|
-
export class
|
|
5
|
-
static
|
|
4
|
+
export class KeyboardBlock extends BlockKeyboard {
|
|
5
|
+
static label = KeyboardLabel
|
|
6
6
|
static home(lang?: string, type?: string) {
|
|
7
7
|
return this._build(`home`, lang, type)
|
|
8
8
|
}
|
|
@@ -1,11 +1,10 @@
|
|
|
1
1
|
import {Gramstax} from "gramstax"
|
|
2
2
|
import {UserGuard} from "~/guards/user"
|
|
3
|
-
import {
|
|
4
|
-
import {GeneralErrorInputNotFoundPage} from "~/pages/general-error-input-notfound"
|
|
3
|
+
import {GeneralStatusPage} from "~/pages/general-status"
|
|
5
4
|
import type {CtxCore} from "./ctx"
|
|
6
5
|
|
|
7
6
|
export class BotCore extends Gramstax {
|
|
8
|
-
async hookBeforeRoute(ctx: CtxCore)
|
|
7
|
+
async hookBeforeRoute(ctx: CtxCore) {
|
|
9
8
|
const result = await new UserGuard(ctx).ensureValid(true)
|
|
10
9
|
if (result === null) {
|
|
11
10
|
return false
|
|
@@ -13,11 +12,11 @@ export class BotCore extends Gramstax {
|
|
|
13
12
|
return true
|
|
14
13
|
}
|
|
15
14
|
|
|
16
|
-
async hookErrorPage(ctx: CtxCore, listenerName: string, error: any, isEdit: boolean)
|
|
17
|
-
await new
|
|
15
|
+
async hookErrorPage(ctx: CtxCore, listenerName: string, error: any, isEdit: boolean) {
|
|
16
|
+
await new GeneralStatusPage(ctx).toError(`${listenerName}:hookErrorPage`, error, isEdit)
|
|
18
17
|
}
|
|
19
18
|
|
|
20
|
-
async hookErrorInputNotFoundPage(ctx: CtxCore)
|
|
21
|
-
await new
|
|
19
|
+
async hookErrorInputNotFoundPage(ctx: CtxCore) {
|
|
20
|
+
await new GeneralStatusPage(ctx).toErrorInputNotFound()
|
|
22
21
|
}
|
|
23
22
|
}
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
export const env = {
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
2
|
+
TELEGRAM_BOT_TOKEN: process.env.TELEGRAM_BOT_TOKEN as string,
|
|
3
|
+
TELEGRAM_BOT_DEPLOY: process.env.TELEGRAM_BOT_DEPLOY as string,
|
|
4
|
+
TELEGRAM_CACHE_SESSION: process.env.TELEGRAM_CACHE_SESSION as string
|
|
5
5
|
}
|
|
@@ -1,8 +1,13 @@
|
|
|
1
1
|
import {GuardBase} from "~/base/guard"
|
|
2
|
-
import {
|
|
2
|
+
import {GeneralStatusPage} from "~/pages/general-status"
|
|
3
3
|
import {UserNameNotFoundPage} from "~/pages/username-notfound"
|
|
4
4
|
|
|
5
5
|
export class UserGuard extends GuardBase {
|
|
6
|
+
async _catch(err: any, func: any, isEdit: boolean) {
|
|
7
|
+
await new GeneralStatusPage(this).toError(func, err, isEdit)
|
|
8
|
+
return null
|
|
9
|
+
}
|
|
10
|
+
|
|
6
11
|
async ensureValid(isEdit: boolean) {
|
|
7
12
|
try {
|
|
8
13
|
if (!this.userName || this.userName.length == 0) {
|
|
@@ -11,8 +16,7 @@ export class UserGuard extends GuardBase {
|
|
|
11
16
|
}
|
|
12
17
|
return {status: true}
|
|
13
18
|
} catch (err) {
|
|
14
|
-
await
|
|
15
|
-
return null
|
|
19
|
+
return await this._catch(err, `ensureValid`, isEdit)
|
|
16
20
|
}
|
|
17
21
|
}
|
|
18
22
|
}
|
|
@@ -6,9 +6,9 @@ import {CacheExternal} from "gramstax"
|
|
|
6
6
|
logUt.success(`Bot started..`)
|
|
7
7
|
|
|
8
8
|
new BotCore({
|
|
9
|
-
token: env.
|
|
10
|
-
deploy: env.
|
|
11
|
-
cacheSession: new CacheExternal(env.
|
|
9
|
+
token: env.TELEGRAM_BOT_TOKEN,
|
|
10
|
+
deploy: env.TELEGRAM_BOT_DEPLOY,
|
|
11
|
+
cacheSession: new CacheExternal(env.TELEGRAM_CACHE_SESSION, `session`, 24 * 60 * 60 * 1000),
|
|
12
12
|
optionsPage: {
|
|
13
13
|
shortCallbackData: true
|
|
14
14
|
}
|
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import {LabelKeyboard} from "gramstax"
|
|
2
2
|
|
|
3
|
-
export class
|
|
3
|
+
export class KeyboardLabel extends LabelKeyboard {
|
|
4
4
|
static home(lang?: string) {
|
|
5
5
|
return this._build(lang, false, {
|
|
6
6
|
default: `Home`,
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
import {log} from "gramstax"
|
|
2
|
+
import {PageBase} from "~/base/page"
|
|
3
|
+
import {StartPage} from "./start"
|
|
4
|
+
|
|
5
|
+
export class GeneralStatusPage extends PageBase {
|
|
6
|
+
constructorName?: string
|
|
7
|
+
constructor(p: any) {
|
|
8
|
+
super(p)
|
|
9
|
+
this.constructorName = p?.constructor?.name
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
kb() {
|
|
13
|
+
return this.inlineKeyboard((k, a) => {
|
|
14
|
+
return k.text(a[0]!, StartPage.data.callback())
|
|
15
|
+
})
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
async toError(funcName: string, error: any, isUpdate = true) {
|
|
19
|
+
log.errorMake(error, this.constructorName, funcName)
|
|
20
|
+
|
|
21
|
+
await this.sessionClear()
|
|
22
|
+
const kb = this.kb()
|
|
23
|
+
const id = `error`
|
|
24
|
+
if (isUpdate === true) {
|
|
25
|
+
await this.edit(kb, {}, id)
|
|
26
|
+
} else {
|
|
27
|
+
await this.reply(kb, {}, id)
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
async toErrorInputNotFound() {
|
|
32
|
+
await this.reply(this.kb(), {}, `error-input-notfound`)
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
static template = `
|
|
36
|
+
<base>
|
|
37
|
+
${this.kbk.home()}
|
|
38
|
+
</base>
|
|
39
|
+
|
|
40
|
+
<base id="error">
|
|
41
|
+
<message>
|
|
42
|
+
<b>❌ Error</b>
|
|
43
|
+
|
|
44
|
+
An unexpected error occurred.
|
|
45
|
+
Please try again later.
|
|
46
|
+
</message>
|
|
47
|
+
<message lang="id">
|
|
48
|
+
<b>❌ Kesalahan</b>
|
|
49
|
+
|
|
50
|
+
Terjadi kesalahan yang tidak diharapkan.
|
|
51
|
+
Silakan coba lagi nanti.
|
|
52
|
+
</message>
|
|
53
|
+
</base>
|
|
54
|
+
|
|
55
|
+
<base id="error-input-notfound">
|
|
56
|
+
<message>
|
|
57
|
+
<b>❌ Not Found</b>
|
|
58
|
+
</message>
|
|
59
|
+
<message lang="id">
|
|
60
|
+
<b>❌ Tidak Ditemukan</b>
|
|
61
|
+
</message>
|
|
62
|
+
</base>
|
|
63
|
+
`
|
|
64
|
+
}
|
|
@@ -3,31 +3,28 @@ import {StartPage} from "./start"
|
|
|
3
3
|
|
|
4
4
|
export class HelpPage extends PageBase {
|
|
5
5
|
kb() {
|
|
6
|
-
return this.
|
|
7
|
-
return k.text(a[0]!, StartPage.data.
|
|
6
|
+
return this.inlineKeyboard((k, a) => {
|
|
7
|
+
return k.text(a[0]!, StartPage.data.callback())
|
|
8
8
|
})
|
|
9
9
|
}
|
|
10
10
|
|
|
11
|
-
async
|
|
11
|
+
async core(edit = true) {
|
|
12
12
|
const kb = this.kb()
|
|
13
|
-
if (edit)
|
|
14
|
-
|
|
15
|
-
} else {
|
|
16
|
-
await this.reply(kb)
|
|
17
|
-
}
|
|
13
|
+
if (edit) await this.edit(kb)
|
|
14
|
+
else await this.reply(kb)
|
|
18
15
|
}
|
|
19
16
|
|
|
20
17
|
async callback() {
|
|
21
|
-
await this.
|
|
18
|
+
await this.core(true)
|
|
22
19
|
}
|
|
23
20
|
|
|
24
21
|
async textCommand() {
|
|
25
|
-
await this.
|
|
22
|
+
await this.core(false)
|
|
26
23
|
}
|
|
27
24
|
|
|
28
25
|
static template = `
|
|
29
26
|
<base>
|
|
30
|
-
${this.
|
|
27
|
+
${this.kbk.back()}
|
|
31
28
|
<message><b>Help message</b></message>
|
|
32
29
|
<message lang="it"><b>Ordina aiuto</b></message>
|
|
33
30
|
<message lang="id"><b>Pesan bantuan</b></message>
|
|
@@ -4,12 +4,12 @@ import {SessionGuard} from "~/guards/session"
|
|
|
4
4
|
|
|
5
5
|
export class InputTextPage extends PageBase {
|
|
6
6
|
static data = this.buildData({
|
|
7
|
-
|
|
7
|
+
intentTextCommand: `input_text`
|
|
8
8
|
})
|
|
9
9
|
|
|
10
10
|
kb(baseId?: string) {
|
|
11
|
-
return this.
|
|
12
|
-
return k.text(a[0]!, StartPage.data.
|
|
11
|
+
return this.inlineKeyboard((k, a) => {
|
|
12
|
+
return k.text(a[0]!, StartPage.data.callback()), {baseId}
|
|
13
13
|
})
|
|
14
14
|
}
|
|
15
15
|
|
|
@@ -44,13 +44,13 @@ export class InputTextPage extends PageBase {
|
|
|
44
44
|
|
|
45
45
|
static template = `
|
|
46
46
|
<base>
|
|
47
|
-
${this.
|
|
47
|
+
${this.kbk.cancel()}
|
|
48
48
|
<message>Please input text you want view</message>
|
|
49
49
|
<message lang="it">Inserisci il testo che vuoi vedere</message>
|
|
50
50
|
<message lang="id">Tolong masukan text yang ingin anda liat</message>
|
|
51
51
|
</base>
|
|
52
52
|
<base id="result">
|
|
53
|
-
${this.
|
|
53
|
+
${this.kbk.home()}
|
|
54
54
|
<message>
|
|
55
55
|
Result: {{res}}
|
|
56
56
|
|
|
@@ -3,27 +3,24 @@ import {PageBase} from "~/base/page"
|
|
|
3
3
|
|
|
4
4
|
export class StartPage extends PageBase {
|
|
5
5
|
kb() {
|
|
6
|
-
return this.
|
|
7
|
-
return k.text(a[0]!, HelpPage.data.
|
|
6
|
+
return this.inlineKeyboard((k, a) => {
|
|
7
|
+
return k.text(a[0]!, HelpPage.data.callback())
|
|
8
8
|
})
|
|
9
9
|
}
|
|
10
10
|
|
|
11
|
-
async
|
|
11
|
+
async core(edit = true) {
|
|
12
12
|
const kb = this.kb()
|
|
13
13
|
const data = {userName: this.userName}
|
|
14
|
-
if (edit)
|
|
15
|
-
|
|
16
|
-
} else {
|
|
17
|
-
await this.reply(kb, data)
|
|
18
|
-
}
|
|
14
|
+
if (edit) await this.edit(kb, data)
|
|
15
|
+
else await this.reply(kb, data)
|
|
19
16
|
}
|
|
20
17
|
|
|
21
18
|
async callback() {
|
|
22
|
-
await this.
|
|
19
|
+
await this.core(true)
|
|
23
20
|
}
|
|
24
21
|
|
|
25
22
|
async textCommand() {
|
|
26
|
-
await this.
|
|
23
|
+
await this.core(false)
|
|
27
24
|
}
|
|
28
25
|
|
|
29
26
|
static template = `
|
|
@@ -3,23 +3,20 @@ import {PageBase} from "~/base/page"
|
|
|
3
3
|
|
|
4
4
|
export class UserNameNotFoundPage extends PageBase {
|
|
5
5
|
kb() {
|
|
6
|
-
return this.
|
|
7
|
-
return k.text(a[0]!, HelpPage.data.
|
|
6
|
+
return this.inlineKeyboard((k, a) => {
|
|
7
|
+
return k.text(a[0]!, HelpPage.data.callback())
|
|
8
8
|
})
|
|
9
9
|
}
|
|
10
10
|
|
|
11
11
|
async transition(edit = true) {
|
|
12
12
|
const kb = this.kb()
|
|
13
|
-
if (edit)
|
|
14
|
-
|
|
15
|
-
} else {
|
|
16
|
-
await this.reply(kb)
|
|
17
|
-
}
|
|
13
|
+
if (edit) await this.edit(kb)
|
|
14
|
+
else await this.reply(kb)
|
|
18
15
|
}
|
|
19
16
|
|
|
20
17
|
static template = `
|
|
21
18
|
<base>
|
|
22
|
-
${this.
|
|
19
|
+
${this.kbk.home()}
|
|
23
20
|
<message>Please set the username first before using the bot.</message>
|
|
24
21
|
<message lang="it">Prima di utilizzare il bot, imposta prima il nome utente.</message>
|
|
25
22
|
<message lang="id">Tolong set username dulu sebelum memakai bot</message>
|
package/package.json
CHANGED
|
@@ -1,45 +0,0 @@
|
|
|
1
|
-
import {PageBase} from "~/base/page"
|
|
2
|
-
import {StartPage} from "./start"
|
|
3
|
-
|
|
4
|
-
export class GeneralErrorInputNotFoundPage extends PageBase {
|
|
5
|
-
kb() {
|
|
6
|
-
return this.ikb((k, a) => {
|
|
7
|
-
return k.text(a[0]!, StartPage.data.callbackData())
|
|
8
|
-
})
|
|
9
|
-
}
|
|
10
|
-
|
|
11
|
-
async transition(session?: boolean) {
|
|
12
|
-
if (session || this.session.method) {
|
|
13
|
-
await this.reply(this.kb(), undefined, `session-error`)
|
|
14
|
-
await this.sessionClear()
|
|
15
|
-
} else {
|
|
16
|
-
await this.reply(this.kb())
|
|
17
|
-
}
|
|
18
|
-
}
|
|
19
|
-
|
|
20
|
-
static template = `
|
|
21
|
-
<base>
|
|
22
|
-
${this.kbc.home()}
|
|
23
|
-
<message>
|
|
24
|
-
<b>❌ Not Found</b>
|
|
25
|
-
</message>
|
|
26
|
-
<message lang="id">
|
|
27
|
-
<b>❌ Tidak Ditemukan</b>
|
|
28
|
-
</message>
|
|
29
|
-
</base>
|
|
30
|
-
<base id="session-error">
|
|
31
|
-
<message>
|
|
32
|
-
<b>❌ Session Error</b>
|
|
33
|
-
|
|
34
|
-
An error occurred while processing your request.
|
|
35
|
-
Your session has an error.
|
|
36
|
-
</message>
|
|
37
|
-
<message lang="id">
|
|
38
|
-
<b>❌ Kesalahan Sesi</b>
|
|
39
|
-
|
|
40
|
-
Terjadi kesalahan saat memproses permintaan.
|
|
41
|
-
Sesi Anda bermasalh.
|
|
42
|
-
</message>
|
|
43
|
-
</base>
|
|
44
|
-
`
|
|
45
|
-
}
|
|
@@ -1,53 +0,0 @@
|
|
|
1
|
-
import {logUt} from "~/utils/log"
|
|
2
|
-
import {PageBase} from "~/base/page"
|
|
3
|
-
import {StartPage} from "./start"
|
|
4
|
-
|
|
5
|
-
export class GeneralErrorPage extends PageBase {
|
|
6
|
-
constructorName?: string
|
|
7
|
-
constructor(p: any) {
|
|
8
|
-
super(p)
|
|
9
|
-
this.constructorName = p?.constructor?.name
|
|
10
|
-
}
|
|
11
|
-
|
|
12
|
-
kb() {
|
|
13
|
-
return this.ikb((k, a) => {
|
|
14
|
-
return k.text(a[0]!, StartPage.data.callbackData())
|
|
15
|
-
})
|
|
16
|
-
}
|
|
17
|
-
|
|
18
|
-
async transition(funcName: string, error: unknown, isUpdate = true) {
|
|
19
|
-
logUt.errorMake(error, this.constructorName, funcName)
|
|
20
|
-
await this.sessionClear()
|
|
21
|
-
|
|
22
|
-
const kb = this.kb()
|
|
23
|
-
if (isUpdate === true) {
|
|
24
|
-
await this.edit(kb)
|
|
25
|
-
} else {
|
|
26
|
-
await this.reply(kb)
|
|
27
|
-
}
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
static template = `
|
|
31
|
-
<base>
|
|
32
|
-
${this.kbc.home()}
|
|
33
|
-
<message>
|
|
34
|
-
<b>❌ Error</b>
|
|
35
|
-
|
|
36
|
-
An unexpected error occurred.
|
|
37
|
-
Please try again later.
|
|
38
|
-
</message>
|
|
39
|
-
<message lang="it">
|
|
40
|
-
<b>❌ Errore</b>
|
|
41
|
-
|
|
42
|
-
Si è verificato un errore imprevisto.
|
|
43
|
-
Riprova più tardi.
|
|
44
|
-
</message>
|
|
45
|
-
<message lang="id">
|
|
46
|
-
<b>❌ Kesalahan</b>
|
|
47
|
-
|
|
48
|
-
Terjadi kesalahan yang tidak diharapkan.
|
|
49
|
-
Silakan coba lagi nanti.
|
|
50
|
-
</message>
|
|
51
|
-
</base>
|
|
52
|
-
`
|
|
53
|
-
}
|