-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcreate-bot.ts
160 lines (140 loc) · 3.8 KB
/
create-bot.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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
import Telegraf, { TelegrafOptions, Middleware, ContextMessageUpdate } from 'telegraf'
import { WaitingStates } from './common'
import { initEnvironment } from './environment'
export interface IBotSettings extends TelegrafOptions {
/**
* The environment name of the token variable.
* `BOT_TOKEN` by default.
*
* @type {string}
* @memberof IBotSettings
*/
tokenEnvName?: string
/**
* The name of the class function for catching errors.
* `onError` by default.
*
* When a function is found in this name inside of the class, the function
* will be used as error logger/handler.
*
* @type {string}
* @memberof IBotSettings
*/
catchFunction?: string
/**
* The name of the class function for help command.
* `help` by default.
*
* When the user sends `/help` command, this function will be executed.
* Or, the function marked with `@help` decorator will be executed.
*
* @type {string}
* @memberof IBotSettings
*/
helpFunction?: string
/**
* The name of the start function.
* `start` by default.
*
* When the user sends `/start` command, this function will be executed.
* Or, the function marked with `@start` decorator will be executed.
*
* @type {string}
* @memberof IBotSettings
*/
startFunction?: string
/**
* The name of the function for callback_query.
* `onCallbackQuery` by default.
*
* When user reacts to a menu or unassigned reaction, it will be executed
* automatically with the content of the callback_query.
*
* @type {string}
* @memberof IBotSettings
*/
callbackQueryFunction?: string
/**
* Indicates whether the callback query handler function of the bot should
* not be called if the query is handled internally such as inline menu
* queries.
*
* `true` by default.
*
* @type {boolean}
* @memberof IBotSettings
*/
skipHandledCallbackQueries?: boolean
/**
* Indicates that whether the status/action of the bot should be updated
* automatically when sending a photo, video or audio.
*
* `true` by default.
*
* @type {boolean}
* @memberof IBotSettings
*/
autoUpdateStatus?: boolean
/**
* Integrates another middlewares to the bot.
*
* @param {...Middleware<ContextMessageUpdate>[]} middlewares Middlewares.
* @memberof IBot
*/
use?: Middleware<ContextMessageUpdate> | Middleware<ContextMessageUpdate>[]
/**
* Indicates whether to print the stack trace or not when an error occurs.
*
* This will be ignored if the catch function exists in the bot class.
*
* `true` by default
*
* @type {boolean}
* @memberof IBotSettings
*/
printStackTrace?: boolean
/**
* The token of the bot or a getter function for token.
*
* @memberof IBotSettings
*/
token?: string | Func<string>
}
function _createBot(token: string, opts: IBotSettings) {
const bot = new Telegraf(token, opts)
bot.use((ctx, next) => {
if (!ctx.message) { next!(); return }
if (!ctx.from) { next!(); return }
WaitingStates.resolveFor(ctx.from.id, ctx.message)
next!()
})
return bot
}
/**
* Creates a bot with given parameters.
*
* @export
* @param {IBotSettings} opts Options for the bot.
* @returns The Telegraf instance.
*
*/
export function createBot(opts: IBotSettings) {
initEnvironment()
let { token, tokenEnvName = 'BOT_TOKEN' } = opts
if (typeof token === 'function') {
const result = token()
if (typeof result === 'string') {
token = result
}
}
else if (typeof token === 'string') {
return _createBot(token, opts)
}
if (typeof token !== 'string') {
token = process.env[ tokenEnvName ] as string
}
if (typeof token !== 'string') {
throw new Error(`The type of the "token" was not string: "${typeof token}"`)
}
return _createBot(token, opts)
}