mirror of
https://github.com/tribufu/sdk-js
synced 2025-06-16 18:44:18 +00:00
Parse bot token to get id and add examples
This commit is contained in:
26
src/bot.ts
26
src/bot.ts
@ -11,8 +11,26 @@ import { TribufuApi } from "./api";
|
||||
* - A bot give you read and write access to the Tribufu API.
|
||||
*/
|
||||
export class TribufuBot extends TribufuApi {
|
||||
private readonly botId: string;
|
||||
|
||||
constructor(token: string) {
|
||||
const payload = TribufuApi.parseToken(token);
|
||||
|
||||
if (!payload) {
|
||||
throw new Error("Invalid token");
|
||||
}
|
||||
|
||||
if (payload.type !== "bot") {
|
||||
throw new Error("Invalid token type");
|
||||
}
|
||||
|
||||
if (!payload.bot_id) {
|
||||
throw new Error("Invalid token payload");
|
||||
}
|
||||
|
||||
super({ accessToken: token });
|
||||
|
||||
this.botId = payload.bot_id;
|
||||
}
|
||||
|
||||
/**
|
||||
@ -37,4 +55,12 @@ export class TribufuBot extends TribufuApi {
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the bot id.
|
||||
* @returns string
|
||||
*/
|
||||
public getBotId(): string {
|
||||
return this.botId;
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user