feat(telegram-bot): add authentication support

This commit is contained in:
Ali BARIN
2022-12-04 16:49:13 +01:00
parent 677880c633
commit 39ac3ff4c2
5 changed files with 57 additions and 0 deletions

View File

@@ -0,0 +1,21 @@
import verifyCredentials from './verify-credentials';
import isStillVerified from './is-still-verified';
export default {
fields: [
{
key: 'token',
label: 'Bot token',
type: 'string' as const,
required: true,
readOnly: false,
value: null,
placeholder: null,
description: 'Bot token which should be retrieved from @botfather.',
clickToCopy: false,
},
],
verifyCredentials,
isStillVerified,
};

View File

@@ -0,0 +1,9 @@
import { IGlobalVariable } from '@automatisch/types';
import verifyCredentials from './verify-credentials';
const isStillVerified = async ($: IGlobalVariable) => {
await verifyCredentials($);
return true;
};
export default isStillVerified;

View File

@@ -0,0 +1,12 @@
import { IGlobalVariable } from '@automatisch/types';
const verifyCredentials = async ($: IGlobalVariable) => {
const { data } = await $.http.get('/getMe');
const { result: me } = data;
await $.auth.set({
screenName: me.first_name,
});
};
export default verifyCredentials;

View File

@@ -0,0 +1,13 @@
import { TBeforeRequest } from '@automatisch/types';
import { URL } from 'node:url';
const addAuthHeader: TBeforeRequest = ($, requestConfig) => {
if ($.auth.data?.token) {
const token = $.auth.data.token as string;
requestConfig.baseURL = (new URL(`/bot${token}`, requestConfig.baseURL)).toString();
}
return requestConfig;
};
export default addAuthHeader;

View File

@@ -12,4 +12,6 @@ export default defineApp({
baseUrl: 'https://telegram.org', baseUrl: 'https://telegram.org',
apiBaseUrl: 'https://api.telegram.org', apiBaseUrl: 'https://api.telegram.org',
primaryColor: '2AABEE', primaryColor: '2AABEE',
beforeRequest: [addAuthHeader],
auth,
}); });