-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(bot): begin with Sport API, add
/howgoodami
command
- Loading branch information
Showing
12 changed files
with
177 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
import type { ChatTypeContext, Context } from 'grammy' | ||
|
||
export function pm<C extends Context>(ctx: C): ctx is ChatTypeContext<C, 'private'> { | ||
return ctx.chat?.type === 'private' | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
import axios from 'axios' | ||
import { z } from 'zod' | ||
import type { AxiosInstance } from 'axios' | ||
import type { Logger } from '~/lib/logging' | ||
|
||
export class SportClient { | ||
private logger: Logger | ||
private axios: AxiosInstance | ||
|
||
constructor({ | ||
logger, | ||
baseUrl, | ||
token, | ||
}: { | ||
logger: Logger | ||
baseUrl: string | ||
token: string | ||
}) { | ||
const axiosInstance = axios.create({ | ||
baseURL: baseUrl, | ||
headers: { | ||
Authorization: `Bearer ${token}`, | ||
}, | ||
}) | ||
|
||
axiosInstance.interceptors.request.use( | ||
(config) => { | ||
logger.debug({ | ||
msg: 'API request initiated', | ||
config: config, | ||
}) | ||
return config | ||
}, | ||
) | ||
|
||
axiosInstance.interceptors.response.use( | ||
(response) => { | ||
logger.debug({ | ||
msg: 'API request finished', | ||
response: response, | ||
}) | ||
return response | ||
}, | ||
(error) => { | ||
logger.error({ | ||
msg: 'YooKassa API request failed', | ||
error: error, | ||
}) | ||
return Promise.reject(error) | ||
}, | ||
) | ||
|
||
this.logger = logger | ||
this.axios = axiosInstance | ||
} | ||
|
||
public async getBetterThan({ studentId }: { studentId: number }) { | ||
return this.request({ | ||
method: 'GET', | ||
path: `/attendance/${studentId}/better_than`, | ||
responseSchema: z.number(), | ||
}) | ||
} | ||
|
||
private async request<S extends z.ZodSchema>({ | ||
method, | ||
path, | ||
responseSchema, | ||
data, | ||
}: { | ||
method: 'GET' | 'POST' | 'DELETE' | 'PUT' | 'PATCH' | ||
path: string | ||
responseSchema: S | ||
data?: any | ||
}): Promise<z.infer<S>> { | ||
const response = await this.axios.request({ | ||
method: method, | ||
url: path, | ||
data: data, | ||
}) | ||
return responseSchema.parse(response.data) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1 @@ | ||
export class Sport { | ||
|
||
} | ||
export * from './client' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,7 @@ | ||
export const en = { | ||
Welcome: 'Up and running!', | ||
'Welcome': 'Up and running!', | ||
|
||
'HowGoodAmI.Thinking': 'Hmm... Let me think 🤔', | ||
'HowGoodAmI.Answer': (percent: number) => `You're better than ${percent}% of students!`, | ||
'HowGoodAmI.Failed': 'I don\'t know 🤷♂️', | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,9 @@ | ||
import type { Translation } from '.' | ||
|
||
export const ru: Translation = { | ||
Welcome: 'Всё работает!', | ||
'Welcome': 'Всё работает!', | ||
|
||
'HowGoodAmI.Thinking': 'Дай-ка подумаю 🤔', | ||
'HowGoodAmI.Answer': (percent: number) => `Ты лучше чем ${percent}% студентов!`, | ||
'HowGoodAmI.Failed': 'Я не знаю 🤷♂️', | ||
} |