mirror of
https://github.com/flibusta-apps/book_bot.git
synced 2025-12-06 15:35:35 +01:00
Add user settings updating
This commit is contained in:
@@ -12,6 +12,7 @@ import * as BookLibrary from "./services/book_library";
|
||||
import { CachedMessage, getBookCache } from './services/book_cache';
|
||||
import { getBookCacheBuffer } from './services/book_cache_buffer';
|
||||
import { download } from './services/downloader';
|
||||
import { createOrUpdateUserSettings } from './services/user_settings';
|
||||
import { formatBook, formatAuthor, formatSequence } from './format';
|
||||
import { getPaginatedMessage, registerPaginationCommand } from './utils';
|
||||
import { getRandomKeyboard } from './keyboard';
|
||||
@@ -31,6 +32,20 @@ export async function createApprovedBot(token: string, state: BotState): Promise
|
||||
{command: "help", description: "Помощь"},
|
||||
]);
|
||||
|
||||
bot.use(async (ctx: Context, next) => {
|
||||
if (ctx.from) {
|
||||
const user = ctx.from;
|
||||
createOrUpdateUserSettings({
|
||||
user_id: user.id,
|
||||
last_name: user.last_name || '',
|
||||
first_name: user.first_name,
|
||||
username: user.username || '',
|
||||
source: ctx.botInfo.username,
|
||||
});
|
||||
}
|
||||
await next();
|
||||
});
|
||||
|
||||
bot.help((ctx: Context) => ctx.reply(Messages.HELP_MESSAGE));
|
||||
|
||||
bot.start((ctx: Context) => {
|
||||
|
||||
64
src/bots/factory/bots/approved/services/user_settings.ts
Normal file
64
src/bots/factory/bots/approved/services/user_settings.ts
Normal file
@@ -0,0 +1,64 @@
|
||||
import got, { HTTPError } from 'got';
|
||||
|
||||
import env from '@/config';
|
||||
|
||||
|
||||
interface Language {
|
||||
id: number;
|
||||
label: string;
|
||||
code: string;
|
||||
}
|
||||
|
||||
|
||||
interface UserSettings {
|
||||
user_id: number;
|
||||
last_name: string;
|
||||
first_name: string;
|
||||
source: string;
|
||||
allowed_langs: Language[];
|
||||
}
|
||||
|
||||
|
||||
export interface UserSettingsUpdateData {
|
||||
user_id: number;
|
||||
last_name: string;
|
||||
first_name: string;
|
||||
username: string;
|
||||
source: string;
|
||||
}
|
||||
|
||||
|
||||
async function _makeGetRequest<T>(url: string, searchParams?: string | Record<string, string | number | boolean | null | undefined> | URLSearchParams | undefined): Promise<T> {
|
||||
const response = await got<T>(`${env.USER_SETTINGS_URL}${url}`, {
|
||||
searchParams,
|
||||
headers: {
|
||||
'Authorization': env.USER_SETTINGS_API_KEY,
|
||||
},
|
||||
responseType: 'json'
|
||||
});
|
||||
|
||||
return response.body;
|
||||
}
|
||||
|
||||
|
||||
export async function getLanguages(): Promise<Language[]> {
|
||||
return _makeGetRequest<Language[]>('/languages');
|
||||
}
|
||||
|
||||
|
||||
export async function getUserSettings(user_id: number): Promise<UserSettings | null> {
|
||||
return _makeGetRequest<UserSettings>(`/users/${user_id}`);
|
||||
}
|
||||
|
||||
export async function createOrUpdateUserSettings(data: UserSettingsUpdateData): Promise<UserSettings> {
|
||||
const response = await got.post<UserSettings>(`${env.USER_SETTINGS_URL}/users/`, {
|
||||
json: data,
|
||||
headers: {
|
||||
'Authorization': env.USER_SETTINGS_API_KEY,
|
||||
'Content-Type': 'application/json',
|
||||
},
|
||||
responseType: 'json'
|
||||
});
|
||||
|
||||
return response.body;
|
||||
}
|
||||
@@ -15,4 +15,6 @@ export default cleanEnv(process.env, {
|
||||
BUFFER_SERVER_API_KEY: str(),
|
||||
DOWNLOADER_URL: str(),
|
||||
DOWNLOADER_API_KEY: str(),
|
||||
USER_SETTINGS_URL: str(),
|
||||
USER_SETTINGS_API_KEY: str(),
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user