-
Notifications
You must be signed in to change notification settings - Fork 838
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: 使用TBXark/telegram-bot-api-types重构telegram相关代码
- Loading branch information
Showing
19 changed files
with
324 additions
and
343 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
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,102 @@ | ||
import type { Telegram } from '../../types/telegram'; | ||
|
||
export class TelegramBotAPI implements | ||
Telegram.SendMessageRequest, | ||
Telegram.EditMessageTextRequest, | ||
Telegram.SendPhotoRequest, | ||
Telegram.SendChatActionRequest, | ||
Telegram.SetWebhookRequest, | ||
Telegram.DeleteWebhookRequest, | ||
Telegram.SetMyCommandsRequest, | ||
Telegram.GetMeRequest, | ||
Telegram.GetFileRequest, | ||
Telegram.GetChatAdministratorsRequest, | ||
Telegram.GetUpdatesRequest { | ||
readonly token: string; | ||
readonly baseURL: string = `https://api.telegram.org/`; | ||
|
||
constructor(token: string, baseURL?: string) { | ||
this.token = token; | ||
if (baseURL) { | ||
this.baseURL = baseURL; | ||
} | ||
} | ||
|
||
static from(token: string, baseURL?: string): TelegramBotAPI { | ||
return new TelegramBotAPI(token, baseURL); | ||
} | ||
|
||
jsonRequest<T>(method: Telegram.TelegramBotMethod, params: T): Promise<Response> { | ||
return fetch(`${this.baseURL}bot${this.token}/${method}`, { | ||
method: 'POST', | ||
headers: { | ||
'Content-Type': 'application/json', | ||
}, | ||
body: JSON.stringify(params), | ||
}); | ||
} | ||
|
||
formDataRequest<T>(method: Telegram.TelegramBotMethod, params: T): Promise<Response> { | ||
const formData = new FormData(); | ||
for (const key in params) { | ||
const value = params[key]; | ||
if (value instanceof File) { | ||
formData.append(key, value, value.name); | ||
} else if (value instanceof Blob) { | ||
formData.append(key, value, 'blob'); | ||
} else if (typeof value === 'string') { | ||
formData.append(key, value); | ||
} else { | ||
formData.append(key, JSON.stringify(value)); | ||
} | ||
} | ||
return fetch(`${this.baseURL}bot${this.token}/${method}`, { | ||
method: 'POST', | ||
body: formData, | ||
}); | ||
} | ||
|
||
sendMessage(params: Telegram.SendMessageParams): Promise<Response> { | ||
return this.jsonRequest('sendMessage', params); | ||
} | ||
|
||
editMessageText(params: Telegram.EditMessageTextParams): Promise<Response> { | ||
return this.jsonRequest('editMessageText', params); | ||
} | ||
|
||
sendPhoto(params: Telegram.SendPhotoParams): Promise<Response> { | ||
return this.formDataRequest('sendPhoto', params); | ||
} | ||
|
||
sendChatAction(params: Telegram.SendChatActionParams): Promise<Response> { | ||
return this.jsonRequest('sendChatAction', params); | ||
} | ||
|
||
setWebhook(params: Telegram.SetWebhookParams): Promise<Response> { | ||
return this.jsonRequest('setWebhook', params); | ||
} | ||
|
||
deleteWebhook(): Promise<Response> { | ||
return this.jsonRequest('deleteWebhook', {}); | ||
} | ||
|
||
setMyCommands(params: Telegram.SetMyCommandsParams): Promise<Response> { | ||
return this.jsonRequest('setMyCommands', params); | ||
} | ||
|
||
getMe(): Promise<Response> { | ||
return this.jsonRequest('getMe', {}); | ||
} | ||
|
||
getFile(params: Telegram.GetFileParams): Promise<Response> { | ||
return this.jsonRequest('getFile', params); | ||
} | ||
|
||
getChatAdministrators(params: Telegram.GetChatAdministratorsParams): Promise<Response> { | ||
return this.jsonRequest('getChatAdministrators', params); | ||
} | ||
|
||
getUpdates(params: Telegram.GetUpdatesParams): Promise<Response> { | ||
return this.jsonRequest('getUpdates', params); | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.