@maxhub/max-bot-api 0.2.1
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/LICENSE +20 -0
- package/dist/api.d.ts +42 -0
- package/dist/api.js +144 -0
- package/dist/bot.d.ts +26 -0
- package/dist/bot.js +70 -0
- package/dist/composer.d.ts +22 -0
- package/dist/composer.js +130 -0
- package/dist/context.d.ts +85 -0
- package/dist/context.js +252 -0
- package/dist/core/helpers/attachments.d.ts +70 -0
- package/dist/core/helpers/attachments.js +127 -0
- package/dist/core/helpers/buttons.d.ts +8 -0
- package/dist/core/helpers/buttons.js +33 -0
- package/dist/core/helpers/keyboard.d.ts +3 -0
- package/dist/core/helpers/keyboard.js +34 -0
- package/dist/core/helpers/types.d.ts +7 -0
- package/dist/core/helpers/types.js +2 -0
- package/dist/core/helpers/upload.d.ts +47 -0
- package/dist/core/helpers/upload.js +166 -0
- package/dist/core/network/api/base-api.d.ts +14 -0
- package/dist/core/network/api/base-api.js +35 -0
- package/dist/core/network/api/client.d.ts +23 -0
- package/dist/core/network/api/client.js +72 -0
- package/dist/core/network/api/error.d.ts +11 -0
- package/dist/core/network/api/error.js +17 -0
- package/dist/core/network/api/index.d.ts +5 -0
- package/dist/core/network/api/index.js +25 -0
- package/dist/core/network/api/modules/bots/api.d.ts +7 -0
- package/dist/core/network/api/modules/bots/api.js +16 -0
- package/dist/core/network/api/modules/bots/types.d.ts +11 -0
- package/dist/core/network/api/modules/bots/types.js +2 -0
- package/dist/core/network/api/modules/chats/api.d.ts +19 -0
- package/dist/core/network/api/modules/chats/api.js +83 -0
- package/dist/core/network/api/modules/chats/types.d.ts +109 -0
- package/dist/core/network/api/modules/chats/types.js +2 -0
- package/dist/core/network/api/modules/index.d.ts +12 -0
- package/dist/core/network/api/modules/index.js +34 -0
- package/dist/core/network/api/modules/messages/api.d.ts +17 -0
- package/dist/core/network/api/modules/messages/api.js +59 -0
- package/dist/core/network/api/modules/messages/types.d.ts +70 -0
- package/dist/core/network/api/modules/messages/types.js +2 -0
- package/dist/core/network/api/modules/subscriptions/api.d.ts +6 -0
- package/dist/core/network/api/modules/subscriptions/api.js +13 -0
- package/dist/core/network/api/modules/subscriptions/types.d.ts +13 -0
- package/dist/core/network/api/modules/subscriptions/types.js +2 -0
- package/dist/core/network/api/modules/types.d.ts +118 -0
- package/dist/core/network/api/modules/types.js +19 -0
- package/dist/core/network/api/modules/uploads/api.d.ts +6 -0
- package/dist/core/network/api/modules/uploads/api.js +13 -0
- package/dist/core/network/api/modules/uploads/types.d.ts +9 -0
- package/dist/core/network/api/modules/uploads/types.js +2 -0
- package/dist/core/network/api/raw-api.d.ts +169 -0
- package/dist/core/network/api/raw-api.js +29 -0
- package/dist/core/network/api/types/attachment-request.d.ts +68 -0
- package/dist/core/network/api/types/attachment-request.js +2 -0
- package/dist/core/network/api/types/attachment.d.ts +67 -0
- package/dist/core/network/api/types/attachment.js +2 -0
- package/dist/core/network/api/types/bot.d.ts +8 -0
- package/dist/core/network/api/types/bot.js +2 -0
- package/dist/core/network/api/types/chat.d.ts +41 -0
- package/dist/core/network/api/types/chat.js +2 -0
- package/dist/core/network/api/types/common.d.ts +9 -0
- package/dist/core/network/api/types/common.js +2 -0
- package/dist/core/network/api/types/index.d.ts +11 -0
- package/dist/core/network/api/types/index.js +27 -0
- package/dist/core/network/api/types/keyboard.d.ts +30 -0
- package/dist/core/network/api/types/keyboard.js +2 -0
- package/dist/core/network/api/types/markup.d.ts +13 -0
- package/dist/core/network/api/types/markup.js +2 -0
- package/dist/core/network/api/types/message.d.ts +38 -0
- package/dist/core/network/api/types/message.js +2 -0
- package/dist/core/network/api/types/subcription.d.ts +85 -0
- package/dist/core/network/api/types/subcription.js +2 -0
- package/dist/core/network/api/types/uploads.d.ts +1 -0
- package/dist/core/network/api/types/uploads.js +2 -0
- package/dist/core/network/api/types/user.d.ts +14 -0
- package/dist/core/network/api/types/user.js +2 -0
- package/dist/core/network/polling.d.ts +11 -0
- package/dist/core/network/polling.js +51 -0
- package/dist/filters.d.ts +2 -0
- package/dist/filters.js +17 -0
- package/dist/index.d.ts +9 -0
- package/dist/index.js +45 -0
- package/dist/middleware.d.ts +9 -0
- package/dist/middleware.js +2 -0
- package/dist/types.d.ts +1 -0
- package/dist/types.js +17 -0
- package/dist/utils.d.ts +3 -0
- package/dist/utils.js +24 -0
- package/package.json +55 -0
- package/readme.md +62 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
The MIT License (MIT)
|
|
2
|
+
Copyright (c) 2024 juice development LLC
|
|
3
|
+
|
|
4
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
5
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
6
|
+
in the Software without restriction, including without limitation the rights
|
|
7
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
8
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
9
|
+
furnished to do so, subject to the following conditions:
|
|
10
|
+
|
|
11
|
+
The above copyright notice and this permission notice shall be included in all
|
|
12
|
+
copies or substantial portions of the Software.
|
|
13
|
+
|
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
15
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
16
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
17
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
18
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
19
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
20
|
+
SOFTWARE.
|
package/dist/api.d.ts
ADDED
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
import { AudioAttachment, FileAttachment, ImageAttachment, VideoAttachment } from './core/helpers/attachments';
|
|
2
|
+
import type { MaybeArray } from './core/helpers/types';
|
|
3
|
+
import { Upload } from './core/helpers/upload';
|
|
4
|
+
import type { UploadFileOptions, UploadImageOptions, UploadVideoOptions, UploadAudioOptions } from './core/helpers/upload';
|
|
5
|
+
import { GetMessagesExtra, RawApi, SenderAction } from './core/network/api';
|
|
6
|
+
import type { AnswerOnCallbackExtra, Client, DeleteMessageExtra, EditMessageExtra, SendMessageExtra, BotCommand, EditMyInfoDTO, FlattenReq, GetUpdatesDTO, UpdateType } from './core/network/api';
|
|
7
|
+
import type { EditChatExtra, GetAllChatsExtra, GetChatMembersExtra, PinMessageExtra } from './core/network/api/modules';
|
|
8
|
+
export declare class Api {
|
|
9
|
+
raw: RawApi;
|
|
10
|
+
upload: Upload;
|
|
11
|
+
constructor(client: Client);
|
|
12
|
+
getMyInfo: () => Promise<import("./core/network/api").BotInfo>;
|
|
13
|
+
editMyInfo: (extra: FlattenReq<EditMyInfoDTO>) => Promise<import("./core/network/api").BotInfo>;
|
|
14
|
+
setMyCommands: (commands: BotCommand[]) => Promise<import("./core/network/api").BotInfo>;
|
|
15
|
+
deleteMyCommands: () => Promise<import("./core/network/api").BotInfo>;
|
|
16
|
+
getAllChats: (extra?: GetAllChatsExtra) => Promise<import("./core/network/api/modules").GetAllChatsResponse>;
|
|
17
|
+
getChat: (id: number) => Promise<import("./core/network/api").Chat>;
|
|
18
|
+
getChatByLink: (link: string) => Promise<import("./core/network/api").Chat>;
|
|
19
|
+
editChatInfo: (chatId: number, extra: EditChatExtra) => Promise<import("./core/network/api").Chat>;
|
|
20
|
+
sendMessageToChat: (chatId: number, text: string, extra?: SendMessageExtra) => Promise<import("./core/network/api").Message>;
|
|
21
|
+
sendMessageToUser: (userId: number, text: string, extra?: SendMessageExtra) => Promise<import("./core/network/api").Message>;
|
|
22
|
+
getMessages: (chatId: number, { message_ids, ...extra }?: GetMessagesExtra) => Promise<import("./core/network/api").GetMessagesResponse>;
|
|
23
|
+
getMessage: (id: string) => Promise<import("./core/network/api").Message>;
|
|
24
|
+
editMessage: (messageId: string, extra?: EditMessageExtra) => Promise<import("./core/network/api").ActionResponse>;
|
|
25
|
+
deleteMessage: (messageId: string, extra?: DeleteMessageExtra) => Promise<import("./core/network/api").ActionResponse>;
|
|
26
|
+
answerOnCallback: (callbackId: string, extra?: AnswerOnCallbackExtra) => Promise<import("./core/network/api").ActionResponse>;
|
|
27
|
+
getChatMembership: (chatId: number) => Promise<import("./core/network/api").ChatMember>;
|
|
28
|
+
getChatAdmins: (chatId: number) => Promise<import("./core/network/api/modules").GetChatAdminsResponse>;
|
|
29
|
+
addChatMembers: (chatId: number, userIds: number[]) => Promise<import("./core/network/api").ActionResponse>;
|
|
30
|
+
getChatMembers: (chatId: number, { user_ids, ...extra }?: GetChatMembersExtra) => Promise<import("./core/network/api/modules").GetChatMembersResponse>;
|
|
31
|
+
removeChatMember: (chatId: number, userId: number) => Promise<import("./core/network/api").ActionResponse>;
|
|
32
|
+
getUpdates: (types?: MaybeArray<UpdateType>, extra?: Omit<FlattenReq<GetUpdatesDTO>, "types">) => Promise<import("./core/network/api").GetUpdatesResponse>;
|
|
33
|
+
getPinnedMessage: (chatId: number) => Promise<import("./core/network/api/modules").GetPinnedMessageResponse>;
|
|
34
|
+
pinMessage: (chatId: number, messageId: string, extra?: PinMessageExtra) => Promise<import("./core/network/api").ActionResponse>;
|
|
35
|
+
unpinMessage: (chatId: number) => Promise<import("./core/network/api").ActionResponse>;
|
|
36
|
+
sendAction: (chatId: number, action: SenderAction) => Promise<import("./core/network/api").ActionResponse>;
|
|
37
|
+
leaveChat: (chatId: number) => Promise<import("./core/network/api").ActionResponse>;
|
|
38
|
+
uploadImage: (options: UploadImageOptions) => Promise<ImageAttachment>;
|
|
39
|
+
uploadVideo: (options: UploadVideoOptions) => Promise<VideoAttachment>;
|
|
40
|
+
uploadAudio: (options: UploadAudioOptions) => Promise<AudioAttachment>;
|
|
41
|
+
uploadFile: (options: UploadFileOptions) => Promise<FileAttachment>;
|
|
42
|
+
}
|
package/dist/api.js
ADDED
|
@@ -0,0 +1,144 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.Api = void 0;
|
|
4
|
+
const attachments_1 = require("./core/helpers/attachments");
|
|
5
|
+
const upload_1 = require("./core/helpers/upload");
|
|
6
|
+
const api_1 = require("./core/network/api");
|
|
7
|
+
class Api {
|
|
8
|
+
constructor(client) {
|
|
9
|
+
this.getMyInfo = async () => {
|
|
10
|
+
return this.raw.bots.getMyInfo();
|
|
11
|
+
};
|
|
12
|
+
this.editMyInfo = async (extra) => {
|
|
13
|
+
return this.raw.bots.editMyInfo(extra);
|
|
14
|
+
};
|
|
15
|
+
this.setMyCommands = async (commands) => {
|
|
16
|
+
return this.editMyInfo({ commands });
|
|
17
|
+
};
|
|
18
|
+
this.deleteMyCommands = async () => {
|
|
19
|
+
return this.editMyInfo({ commands: [] });
|
|
20
|
+
};
|
|
21
|
+
this.getAllChats = async (extra = {}) => {
|
|
22
|
+
return this.raw.chats.getAll(extra);
|
|
23
|
+
};
|
|
24
|
+
this.getChat = async (id) => {
|
|
25
|
+
return this.raw.chats.getById({ chat_id: id });
|
|
26
|
+
};
|
|
27
|
+
this.getChatByLink = async (link) => {
|
|
28
|
+
return this.raw.chats.getByLink({ chat_link: link });
|
|
29
|
+
};
|
|
30
|
+
this.editChatInfo = async (chatId, extra) => {
|
|
31
|
+
return this.raw.chats.edit({ chat_id: chatId, ...extra });
|
|
32
|
+
};
|
|
33
|
+
this.sendMessageToChat = async (chatId, text, extra) => {
|
|
34
|
+
const { message } = await this.raw.messages.send({
|
|
35
|
+
chat_id: chatId,
|
|
36
|
+
text,
|
|
37
|
+
...extra,
|
|
38
|
+
});
|
|
39
|
+
return message;
|
|
40
|
+
};
|
|
41
|
+
this.sendMessageToUser = async (userId, text, extra) => {
|
|
42
|
+
const { message } = await this.raw.messages.send({
|
|
43
|
+
user_id: userId,
|
|
44
|
+
text,
|
|
45
|
+
...extra,
|
|
46
|
+
});
|
|
47
|
+
return message;
|
|
48
|
+
};
|
|
49
|
+
this.getMessages = async (chatId, { message_ids, ...extra } = {}) => {
|
|
50
|
+
return this.raw.messages.get({
|
|
51
|
+
chat_id: chatId,
|
|
52
|
+
message_ids: message_ids?.join(','),
|
|
53
|
+
...extra,
|
|
54
|
+
});
|
|
55
|
+
};
|
|
56
|
+
this.getMessage = async (id) => {
|
|
57
|
+
return this.raw.messages.getById({ message_id: id });
|
|
58
|
+
};
|
|
59
|
+
this.editMessage = async (messageId, extra) => {
|
|
60
|
+
return this.raw.messages.edit({
|
|
61
|
+
message_id: messageId,
|
|
62
|
+
...extra,
|
|
63
|
+
});
|
|
64
|
+
};
|
|
65
|
+
this.deleteMessage = async (messageId, extra) => {
|
|
66
|
+
return this.raw.messages.delete({ message_id: messageId, ...extra });
|
|
67
|
+
};
|
|
68
|
+
this.answerOnCallback = async (callbackId, extra) => {
|
|
69
|
+
return this.raw.messages.answerOnCallback({ callback_id: callbackId, ...extra });
|
|
70
|
+
};
|
|
71
|
+
this.getChatMembership = (chatId) => {
|
|
72
|
+
return this.raw.chats.getChatMembership({ chat_id: chatId });
|
|
73
|
+
};
|
|
74
|
+
this.getChatAdmins = (chatId) => {
|
|
75
|
+
return this.raw.chats.getChatAdmins({ chat_id: chatId });
|
|
76
|
+
};
|
|
77
|
+
this.addChatMembers = (chatId, userIds) => {
|
|
78
|
+
return this.raw.chats.addChatMembers({
|
|
79
|
+
chat_id: chatId,
|
|
80
|
+
user_ids: userIds,
|
|
81
|
+
});
|
|
82
|
+
};
|
|
83
|
+
this.getChatMembers = (chatId, { user_ids, ...extra } = {}) => {
|
|
84
|
+
return this.raw.chats.getChatMembers({
|
|
85
|
+
chat_id: chatId,
|
|
86
|
+
user_ids: user_ids?.join(','),
|
|
87
|
+
...extra,
|
|
88
|
+
});
|
|
89
|
+
};
|
|
90
|
+
this.removeChatMember = (chatId, userId) => {
|
|
91
|
+
return this.raw.chats.removeChatMember({
|
|
92
|
+
chat_id: chatId,
|
|
93
|
+
user_id: userId,
|
|
94
|
+
});
|
|
95
|
+
};
|
|
96
|
+
this.getUpdates = async (types = [], extra = {}) => {
|
|
97
|
+
return this.raw.subscriptions.getUpdates({
|
|
98
|
+
types: Array.isArray(types) ? types.join(',') : types,
|
|
99
|
+
...extra,
|
|
100
|
+
});
|
|
101
|
+
};
|
|
102
|
+
this.getPinnedMessage = async (chatId) => {
|
|
103
|
+
return this.raw.chats.getPinnedMessage({ chat_id: chatId });
|
|
104
|
+
};
|
|
105
|
+
this.pinMessage = async (chatId, messageId, extra) => {
|
|
106
|
+
return this.raw.chats.pinMessage({
|
|
107
|
+
chat_id: chatId,
|
|
108
|
+
message_id: messageId,
|
|
109
|
+
...extra,
|
|
110
|
+
});
|
|
111
|
+
};
|
|
112
|
+
this.unpinMessage = async (chatId) => {
|
|
113
|
+
return this.raw.chats.unpinMessage({ chat_id: chatId });
|
|
114
|
+
};
|
|
115
|
+
this.sendAction = async (chatId, action) => {
|
|
116
|
+
return this.raw.chats.sendAction({
|
|
117
|
+
chat_id: chatId,
|
|
118
|
+
action,
|
|
119
|
+
});
|
|
120
|
+
};
|
|
121
|
+
this.leaveChat = async (chatId) => {
|
|
122
|
+
return this.raw.chats.leaveChat({ chat_id: chatId });
|
|
123
|
+
};
|
|
124
|
+
this.uploadImage = async (options) => {
|
|
125
|
+
const data = await this.upload.image(options);
|
|
126
|
+
return new attachments_1.ImageAttachment(data);
|
|
127
|
+
};
|
|
128
|
+
this.uploadVideo = async (options) => {
|
|
129
|
+
const data = await this.upload.video(options);
|
|
130
|
+
return new attachments_1.VideoAttachment({ token: data.token });
|
|
131
|
+
};
|
|
132
|
+
this.uploadAudio = async (options) => {
|
|
133
|
+
const data = await this.upload.audio(options);
|
|
134
|
+
return new attachments_1.AudioAttachment({ token: data.token });
|
|
135
|
+
};
|
|
136
|
+
this.uploadFile = async (options) => {
|
|
137
|
+
const data = await this.upload.file(options);
|
|
138
|
+
return new attachments_1.FileAttachment({ token: data.token });
|
|
139
|
+
};
|
|
140
|
+
this.raw = new api_1.RawApi(client);
|
|
141
|
+
this.upload = new upload_1.Upload(this);
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
exports.Api = Api;
|
package/dist/bot.d.ts
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import { Composer } from './composer';
|
|
2
|
+
import { Context } from './context';
|
|
3
|
+
import { MaybePromise } from './core/helpers/types';
|
|
4
|
+
import { BotInfo, ClientOptions, UpdateType } from './core/network/api';
|
|
5
|
+
import { Api } from './api';
|
|
6
|
+
type BotConfig<Ctx extends Context> = {
|
|
7
|
+
clientOptions?: ClientOptions;
|
|
8
|
+
contextType: new (...args: ConstructorParameters<typeof Context>) => Ctx;
|
|
9
|
+
};
|
|
10
|
+
type LaunchOptions = {
|
|
11
|
+
allowedUpdates: UpdateType[];
|
|
12
|
+
};
|
|
13
|
+
export declare class Bot<Ctx extends Context = Context> extends Composer<Ctx> {
|
|
14
|
+
api: Api;
|
|
15
|
+
botInfo?: BotInfo;
|
|
16
|
+
private polling?;
|
|
17
|
+
private pollingIsStarted;
|
|
18
|
+
private config;
|
|
19
|
+
constructor(token: string, config?: Partial<BotConfig<Ctx>>);
|
|
20
|
+
private handleError;
|
|
21
|
+
catch(handler: (err: unknown, ctx: Ctx) => MaybePromise<void>): this;
|
|
22
|
+
start: (options?: LaunchOptions) => Promise<void>;
|
|
23
|
+
stop: () => void;
|
|
24
|
+
private handleUpdate;
|
|
25
|
+
}
|
|
26
|
+
export {};
|
package/dist/bot.js
ADDED
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.Bot = void 0;
|
|
7
|
+
const debug_1 = __importDefault(require("debug"));
|
|
8
|
+
const composer_1 = require("./composer");
|
|
9
|
+
const context_1 = require("./context");
|
|
10
|
+
const api_1 = require("./core/network/api");
|
|
11
|
+
const polling_1 = require("./core/network/polling");
|
|
12
|
+
const api_2 = require("./api");
|
|
13
|
+
const debug = (0, debug_1.default)('one-me:main');
|
|
14
|
+
const defaultConfig = {
|
|
15
|
+
contextType: context_1.Context,
|
|
16
|
+
};
|
|
17
|
+
class Bot extends composer_1.Composer {
|
|
18
|
+
constructor(token, config) {
|
|
19
|
+
super();
|
|
20
|
+
this.pollingIsStarted = false;
|
|
21
|
+
this.handleError = (err, ctx) => {
|
|
22
|
+
process.exitCode = 1;
|
|
23
|
+
console.error('Unhandled error while processing', ctx.update);
|
|
24
|
+
throw err;
|
|
25
|
+
};
|
|
26
|
+
this.start = async (options) => {
|
|
27
|
+
if (this.pollingIsStarted) {
|
|
28
|
+
debug('Long polling already running');
|
|
29
|
+
return;
|
|
30
|
+
}
|
|
31
|
+
this.pollingIsStarted = true;
|
|
32
|
+
this.botInfo ?? (this.botInfo = await this.api.getMyInfo());
|
|
33
|
+
this.polling = new polling_1.Polling(this.api, options?.allowedUpdates);
|
|
34
|
+
debug(`Starting @${this.botInfo.username}`);
|
|
35
|
+
await this.polling.loop(this.handleUpdate);
|
|
36
|
+
};
|
|
37
|
+
this.stop = () => {
|
|
38
|
+
if (!this.pollingIsStarted) {
|
|
39
|
+
debug('Long polling is not running');
|
|
40
|
+
return;
|
|
41
|
+
}
|
|
42
|
+
this.polling?.stop();
|
|
43
|
+
this.pollingIsStarted = false;
|
|
44
|
+
};
|
|
45
|
+
this.handleUpdate = async (update) => {
|
|
46
|
+
const updateId = `${update.update_type}:${update.timestamp}`;
|
|
47
|
+
debug(`Processing update ${updateId}`);
|
|
48
|
+
const UpdateContext = this.config.contextType;
|
|
49
|
+
const ctx = new UpdateContext(update, this.api, this.botInfo);
|
|
50
|
+
try {
|
|
51
|
+
await this.middleware()(ctx, () => Promise.resolve(undefined));
|
|
52
|
+
}
|
|
53
|
+
catch (err) {
|
|
54
|
+
await this.handleError(err, ctx);
|
|
55
|
+
}
|
|
56
|
+
finally {
|
|
57
|
+
debug(`Finished processing update ${updateId}`);
|
|
58
|
+
}
|
|
59
|
+
};
|
|
60
|
+
// @ts-ignore
|
|
61
|
+
this.config = { ...defaultConfig, ...config };
|
|
62
|
+
this.api = new api_2.Api((0, api_1.createClient)(token, this.config.clientOptions));
|
|
63
|
+
debug('Created `Bot` instance');
|
|
64
|
+
}
|
|
65
|
+
catch(handler) {
|
|
66
|
+
this.handleError = handler;
|
|
67
|
+
return this;
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
exports.Bot = Bot;
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import type { Guard, MaybeArray } from './core/helpers/types';
|
|
2
|
+
import type { UpdateType } from './core/network/api';
|
|
3
|
+
import type { Middleware, MiddlewareFn, MiddlewareObj, NextFn } from './middleware';
|
|
4
|
+
import { Context, type FilteredContext } from './context';
|
|
5
|
+
type Triggers = MaybeArray<string | RegExp>;
|
|
6
|
+
type UpdateFilter<Ctx extends Context> = UpdateType | Guard<Ctx['update']>;
|
|
7
|
+
export declare class Composer<Ctx extends Context> implements MiddlewareObj<Ctx> {
|
|
8
|
+
private handler;
|
|
9
|
+
constructor(...middlewares: Array<Middleware<Ctx>>);
|
|
10
|
+
middleware(): MiddlewareFn<Ctx>;
|
|
11
|
+
use(...middlewares: Array<Middleware<Ctx>>): this;
|
|
12
|
+
on<Filter extends UpdateType | Guard<Ctx['update']>>(filters: MaybeArray<Filter>, ...middlewares: Array<Middleware<FilteredContext<Ctx, Filter>>>): this;
|
|
13
|
+
command(command: Triggers, ...middlewares: Array<Middleware<FilteredContext<Ctx, 'message_created'>>>): this;
|
|
14
|
+
hears(triggers: Triggers, ...middlewares: Array<Middleware<FilteredContext<Ctx, 'message_created'>>>): this;
|
|
15
|
+
action(triggers: Triggers, ...middlewares: Array<Middleware<FilteredContext<Ctx, 'message_callback'>>>): this;
|
|
16
|
+
filter<Filter extends UpdateFilter<Ctx>>(filters: MaybeArray<Filter>, ...middlewares: Array<Middleware<FilteredContext<Ctx, Filter>>>): MiddlewareFn<Ctx>;
|
|
17
|
+
static flatten<C extends Context>(mw: Middleware<C>): MiddlewareFn<C>;
|
|
18
|
+
static concat<C extends Context>(first: MiddlewareFn<C>, andThen: MiddlewareFn<C>): MiddlewareFn<C>;
|
|
19
|
+
static pass<C extends Context>(_ctx: C, next: NextFn): Promise<void>;
|
|
20
|
+
static compose<C extends Context>(middlewares: Array<Middleware<C>>): MiddlewareFn<C>;
|
|
21
|
+
}
|
|
22
|
+
export {};
|
package/dist/composer.js
ADDED
|
@@ -0,0 +1,130 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.Composer = void 0;
|
|
4
|
+
const filters_1 = require("./filters");
|
|
5
|
+
class Composer {
|
|
6
|
+
constructor(...middlewares) {
|
|
7
|
+
this.handler = Composer.compose(middlewares);
|
|
8
|
+
}
|
|
9
|
+
middleware() {
|
|
10
|
+
return this.handler;
|
|
11
|
+
}
|
|
12
|
+
use(...middlewares) {
|
|
13
|
+
this.handler = Composer.compose([this.handler, ...middlewares]);
|
|
14
|
+
return this;
|
|
15
|
+
}
|
|
16
|
+
on(filters, ...middlewares) {
|
|
17
|
+
return this.use(this.filter(filters, ...middlewares));
|
|
18
|
+
}
|
|
19
|
+
command(command, ...middlewares) {
|
|
20
|
+
const normalizedTriggers = normalizeTriggers(command);
|
|
21
|
+
const filter = (0, filters_1.createdMessageBodyHas)('text');
|
|
22
|
+
const handler = Composer.compose(middlewares);
|
|
23
|
+
return this.use(this.filter(filter, (ctx, next) => {
|
|
24
|
+
const text = extractTextFromMessage(ctx.message, ctx.myId);
|
|
25
|
+
const cmd = text.slice(1);
|
|
26
|
+
for (const trigger of normalizedTriggers) {
|
|
27
|
+
const match = trigger(cmd);
|
|
28
|
+
if (match) {
|
|
29
|
+
ctx.match = match;
|
|
30
|
+
return handler(ctx, next);
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
return next();
|
|
34
|
+
}));
|
|
35
|
+
}
|
|
36
|
+
hears(triggers, ...middlewares) {
|
|
37
|
+
const normalizedTriggers = normalizeTriggers(triggers);
|
|
38
|
+
const filter = (0, filters_1.createdMessageBodyHas)('text');
|
|
39
|
+
const handler = Composer.compose(middlewares);
|
|
40
|
+
return this.use(this.filter(filter, (ctx, next) => {
|
|
41
|
+
const text = extractTextFromMessage(ctx.message, ctx.myId);
|
|
42
|
+
for (const trigger of normalizedTriggers) {
|
|
43
|
+
const match = trigger(text);
|
|
44
|
+
if (match) {
|
|
45
|
+
ctx.match = match;
|
|
46
|
+
return handler(ctx, next);
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
return next();
|
|
50
|
+
}));
|
|
51
|
+
}
|
|
52
|
+
action(triggers, ...middlewares) {
|
|
53
|
+
const normalizedTriggers = normalizeTriggers(triggers);
|
|
54
|
+
const handler = Composer.compose(middlewares);
|
|
55
|
+
return this.use(this.filter('message_callback', (ctx, next) => {
|
|
56
|
+
const { payload } = ctx.update.callback;
|
|
57
|
+
if (!payload)
|
|
58
|
+
return next();
|
|
59
|
+
for (const trigger of normalizedTriggers) {
|
|
60
|
+
const match = trigger(payload);
|
|
61
|
+
if (match) {
|
|
62
|
+
ctx.match = match;
|
|
63
|
+
return handler(ctx, next);
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
return next();
|
|
67
|
+
}));
|
|
68
|
+
}
|
|
69
|
+
filter(filters, ...middlewares) {
|
|
70
|
+
const handler = Composer.compose(middlewares);
|
|
71
|
+
return (ctx, next) => {
|
|
72
|
+
return ctx.has(filters) ? handler(ctx, next) : next();
|
|
73
|
+
};
|
|
74
|
+
}
|
|
75
|
+
static flatten(mw) {
|
|
76
|
+
return typeof mw === 'function'
|
|
77
|
+
? mw
|
|
78
|
+
: (ctx, next) => mw.middleware()(ctx, next);
|
|
79
|
+
}
|
|
80
|
+
static concat(first, andThen) {
|
|
81
|
+
return async (ctx, next) => {
|
|
82
|
+
let nextCalled = false;
|
|
83
|
+
await first(ctx, async () => {
|
|
84
|
+
if (nextCalled) {
|
|
85
|
+
throw new Error('`next` already called before!');
|
|
86
|
+
}
|
|
87
|
+
nextCalled = true;
|
|
88
|
+
await andThen(ctx, next);
|
|
89
|
+
});
|
|
90
|
+
};
|
|
91
|
+
}
|
|
92
|
+
static pass(_ctx, next) {
|
|
93
|
+
return next();
|
|
94
|
+
}
|
|
95
|
+
static compose(middlewares) {
|
|
96
|
+
if (!Array.isArray(middlewares)) {
|
|
97
|
+
throw new Error('Middlewares must be an array');
|
|
98
|
+
}
|
|
99
|
+
if (middlewares.length === 0) {
|
|
100
|
+
return Composer.pass;
|
|
101
|
+
}
|
|
102
|
+
return middlewares.map(Composer.flatten).reduce(Composer.concat);
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
exports.Composer = Composer;
|
|
106
|
+
const normalizeTriggers = (triggers) => {
|
|
107
|
+
return (Array.isArray(triggers) ? triggers : [triggers]).map((trigger) => {
|
|
108
|
+
if (trigger instanceof RegExp) {
|
|
109
|
+
return (value = '') => {
|
|
110
|
+
// eslint-disable-next-line no-param-reassign
|
|
111
|
+
trigger.lastIndex = 0;
|
|
112
|
+
return trigger.exec(value.trim());
|
|
113
|
+
};
|
|
114
|
+
}
|
|
115
|
+
const regex = new RegExp(`^${trigger}$`);
|
|
116
|
+
return (value) => regex.exec(value.trim());
|
|
117
|
+
});
|
|
118
|
+
};
|
|
119
|
+
const extractTextFromMessage = (message, myId) => {
|
|
120
|
+
const { text } = message.body;
|
|
121
|
+
const mention = message.body.markup?.find((m) => {
|
|
122
|
+
return m.type === 'user_mention';
|
|
123
|
+
});
|
|
124
|
+
if (mention
|
|
125
|
+
&& mention.from === 0
|
|
126
|
+
&& mention.user_id === myId) {
|
|
127
|
+
return text?.slice(mention.length).trim();
|
|
128
|
+
}
|
|
129
|
+
return text;
|
|
130
|
+
};
|
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
import type { Guard, Guarded, MaybeArray } from './core/helpers/types';
|
|
2
|
+
import type { AnswerOnCallbackExtra, BotInfo, BotStartedUpdate, Chat, EditMessageExtra, FilteredUpdate, GetMessagesExtra, Message, MessageCallbackUpdate, SenderAction, SendMessageExtra, Update, UpdateType, User } from './core/network/api';
|
|
3
|
+
import { type Api } from './api';
|
|
4
|
+
import { EditChatExtra, GetAllChatsExtra, GetChatMembersExtra, PinMessageExtra } from './core/network/api/modules';
|
|
5
|
+
export type FilteredContext<Ctx extends Context, Filter extends UpdateType | Guard<Ctx['update']>> = Filter extends UpdateType ? Ctx & Context<FilteredUpdate<Filter>> : Ctx & Context<Guarded<Filter>>;
|
|
6
|
+
type GetMessage<U extends Update> = U extends MessageCallbackUpdate ? MessageCallbackUpdate['message'] : U extends {
|
|
7
|
+
message: Message;
|
|
8
|
+
} ? Message : undefined;
|
|
9
|
+
type GetChatId<U extends Update> = U extends {
|
|
10
|
+
chat_id: number;
|
|
11
|
+
} ? number : U extends MessageCallbackUpdate ? number | undefined : U extends {
|
|
12
|
+
message: Message;
|
|
13
|
+
} ? number : undefined;
|
|
14
|
+
type GetChat<U extends Update> = U extends {
|
|
15
|
+
chat: Chat;
|
|
16
|
+
} ? Chat : undefined;
|
|
17
|
+
type GetMsgId<U extends Update> = U extends {
|
|
18
|
+
message_id: string;
|
|
19
|
+
} ? string : U extends MessageCallbackUpdate ? string | undefined : U extends {
|
|
20
|
+
message: Message;
|
|
21
|
+
} ? string : undefined;
|
|
22
|
+
type GetCallback<U extends Update> = U extends MessageCallbackUpdate ? MessageCallbackUpdate['callback'] : undefined;
|
|
23
|
+
type GetUser<U extends Update> = U extends {
|
|
24
|
+
user: User;
|
|
25
|
+
} ? User : U extends MessageCallbackUpdate ? User : undefined;
|
|
26
|
+
type GetStartPayload<U extends Update> = U extends BotStartedUpdate ? string | undefined | null : undefined;
|
|
27
|
+
type ContactInfo = {
|
|
28
|
+
tel?: string;
|
|
29
|
+
fullName?: string;
|
|
30
|
+
};
|
|
31
|
+
type Location = {
|
|
32
|
+
latitude: number;
|
|
33
|
+
longitude: number;
|
|
34
|
+
};
|
|
35
|
+
type Sticker = {
|
|
36
|
+
width: number;
|
|
37
|
+
height: number;
|
|
38
|
+
url: string;
|
|
39
|
+
code: string;
|
|
40
|
+
};
|
|
41
|
+
export declare class Context<U extends Update = Update> {
|
|
42
|
+
readonly update: U;
|
|
43
|
+
readonly api: Api;
|
|
44
|
+
readonly botInfo?: BotInfo | undefined;
|
|
45
|
+
match?: RegExpExecArray;
|
|
46
|
+
constructor(update: U, api: Api, botInfo?: BotInfo | undefined);
|
|
47
|
+
has<Ctx extends Context, Filter extends UpdateType | Guard<Ctx['update']>>(this: Ctx, filters: MaybeArray<Filter>): this is FilteredContext<Ctx, Filter>;
|
|
48
|
+
assert<T extends string | number | object>(value: T | undefined, method: string): asserts value is T;
|
|
49
|
+
get updateType(): "message_callback" | "message_created" | "message_removed" | "message_edited" | "bot_added" | "bot_removed" | "user_added" | "user_removed" | "bot_started" | "chat_title_changed" | "message_construction_request" | "message_constructed" | "message_chat_created";
|
|
50
|
+
get myId(): number | undefined;
|
|
51
|
+
get startPayload(): GetStartPayload<U>;
|
|
52
|
+
get chat(): GetChat<U>;
|
|
53
|
+
get chatId(): GetChatId<U>;
|
|
54
|
+
get message(): GetMessage<U>;
|
|
55
|
+
get messageId(): GetMsgId<U>;
|
|
56
|
+
get callback(): GetCallback<U>;
|
|
57
|
+
get user(): GetUser<U>;
|
|
58
|
+
private _contactInfo?;
|
|
59
|
+
get contactInfo(): ContactInfo | undefined;
|
|
60
|
+
private _location?;
|
|
61
|
+
get location(): Location | undefined;
|
|
62
|
+
private _sticker?;
|
|
63
|
+
get sticker(): Sticker | undefined;
|
|
64
|
+
reply(text: string, extra?: SendMessageExtra): Promise<Message>;
|
|
65
|
+
getAllChats(extra?: GetAllChatsExtra): Promise<import("./core/network/api/modules").GetAllChatsResponse>;
|
|
66
|
+
getChat(chatId?: number): Promise<Chat>;
|
|
67
|
+
getChatByLink(link: string): Promise<Chat>;
|
|
68
|
+
editChatInfo(extra: EditChatExtra): Promise<Chat>;
|
|
69
|
+
getMessage(id: string): Promise<Message>;
|
|
70
|
+
getMessages(extra?: GetMessagesExtra): Promise<import("./core/network/api").GetMessagesResponse>;
|
|
71
|
+
getPinnedMessage(): Promise<import("./core/network/api/modules").GetPinnedMessageResponse>;
|
|
72
|
+
editMessage(extra: EditMessageExtra): Promise<import("./core/network/api").ActionResponse>;
|
|
73
|
+
deleteMessage(messageId?: string): Promise<import("./core/network/api").ActionResponse>;
|
|
74
|
+
answerOnCallback(extra: AnswerOnCallbackExtra): Promise<import("./core/network/api").ActionResponse>;
|
|
75
|
+
getChatMembership(): Promise<import("./core/network/api").ChatMember>;
|
|
76
|
+
getChatAdmins(): Promise<import("./core/network/api/modules").GetChatAdminsResponse>;
|
|
77
|
+
addChatMembers(userIds: number[]): Promise<import("./core/network/api").ActionResponse>;
|
|
78
|
+
getChatMembers(extra?: GetChatMembersExtra): Promise<import("./core/network/api/modules").GetChatMembersResponse>;
|
|
79
|
+
removeChatMember(userId: number): Promise<import("./core/network/api").ActionResponse>;
|
|
80
|
+
pinMessage(messageId: string, extra?: PinMessageExtra): Promise<import("./core/network/api").ActionResponse>;
|
|
81
|
+
unpinMessage(): Promise<import("./core/network/api").ActionResponse>;
|
|
82
|
+
sendAction(action: SenderAction): Promise<import("./core/network/api").ActionResponse>;
|
|
83
|
+
leaveChat(): Promise<import("./core/network/api").ActionResponse>;
|
|
84
|
+
}
|
|
85
|
+
export {};
|