Skip to content

Commit 486691c

Browse files
committed
Add VerifyBotAuth
1 parent fd076e2 commit 486691c

8 files changed

Lines changed: 272 additions & 15 deletions

File tree

README.md

Lines changed: 33 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,9 @@
22

33
A simple way to send and verify verification code with SMS, Email, Telegram and etc.
44

5-
- [x] Send verify code
6-
- [x] Verify a code
5+
- [x] Send verify code.
6+
- [x] Verify a code.
7+
- [x] VerifyBotAuth.
78

89
## Supported Providers
910

@@ -13,11 +14,12 @@ A simple way to send and verify verification code with SMS, Email, Telegram and
1314

1415
## Usages
1516

16-
- Create a verify message and send it to the target
17+
### Verify Feature
18+
19+
- Create a verify message and send it to the target.
1720

1821
```typescript
1922
const API_KEY = 'vm_gX9WwSdKatMNdpUClLU0IfCx575tvdoeQ';
20-
2123
const sdk = Verifyme.create(VerifymeOptions.builder().apiKey(API_KEY).build());
2224

2325
const request = CreateVerifyRequest.builder()
@@ -31,7 +33,7 @@ console.log('Request: ', request);
3133
console.log('Response: ', response);
3234
```
3335

34-
- Verify the message
36+
- Verify the message.
3537

3638
```typescript
3739
const request = VerifyMessageRequest.builder()
@@ -44,6 +46,32 @@ console.log('Request: ', request);
4446
console.log('Response: ', response);
4547
```
4648

49+
### VerifyBotAuth Feature
50+
51+
- Create an auth state request with the target.
52+
53+
```typescript
54+
const API_KEY = 'vm_gX9WwSdKatMNdpUClLU0IfCx575tvdoeQ';
55+
const sdk = Verifyme.create(VerifymeOptions.builder().apiKey(API_KEY).build());
56+
57+
const request: VerifyBotAuthCreate = {
58+
target: '+855769995149',
59+
type: 'telegram_bot',
60+
};
61+
62+
const response = await sdk.botAuth().auth(request);
63+
console.log('Request: ', request);
64+
console.log('Response: ', response);
65+
```
66+
67+
- Get the auth state.
68+
69+
```typescript
70+
const result = await sdk.botAuth().state(response.state!);
71+
console.log('State: ', state);
72+
console.log('Result: ', result);
73+
```
74+
4775
### Contributors
4876

4977
- Sambo Chea <sombochea@cubetiqs.com>

package-lock.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@cubetiq/verifyme",
3-
"version": "0.0.4",
3+
"version": "0.1.5",
44
"description": "A simple way to send and verify verification code with SMS, Email, Telegram and etc.",
55
"main": "dist/index.js",
66
"private": false,

src/model.ts

Lines changed: 65 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@ enum Provider {
77

88
type ProviderType = 'telegram' | 'verifybot' | 'sms' | 'email';
99

10+
export type VerifyBotAuthType = 'telegram_bot' | 'qr_code';
11+
export type VerifyBotAuthStatus = 'pending' | 'verified' | 'expired' | 'rejected';
12+
1013
class CreateVerifyRequest {
1114
provider?: Provider | ProviderType | string; // telegram, verifybot, sms, email
1215
target?: string; // phone, email, telegram chat id, etc.
@@ -41,7 +44,7 @@ class CreateVerifyRequestBuilder {
4144
private _timeout?: number;
4245
private _template?: string;
4346

44-
constructor() {}
47+
constructor() { }
4548

4649
provider(
4750
provider: Provider | ProviderType | string | undefined
@@ -113,7 +116,7 @@ class VerifyMessageRequestBuilder {
113116
private _token?: string;
114117
private _code?: string;
115118

116-
constructor() {}
119+
constructor() { }
117120

118121
token(token: string | undefined): VerifyMessageRequestBuilder {
119122
this._token = token;
@@ -182,7 +185,7 @@ class VerifymeOptionsBuilder {
182185
private _apiKey?: string;
183186
private _connectionTimeout?: number;
184187

185-
constructor() {}
188+
constructor() { }
186189

187190
url(url: string | undefined): VerifymeOptionsBuilder {
188191
this._url = url;
@@ -210,6 +213,65 @@ class VerifymeOptionsBuilder {
210213
}
211214
}
212215

216+
export class VerifyBotAuthCreate {
217+
target?: string;
218+
type?: VerifyBotAuthType | string;
219+
220+
constructor({
221+
target,
222+
type,
223+
}: {
224+
target?: string;
225+
type?: VerifyBotAuthType | string;
226+
}) {
227+
this.target = target;
228+
this.type = type;
229+
}
230+
231+
static builder(): VerifyBotAuthCreateBuilder {
232+
return new VerifyBotAuthCreateBuilder();
233+
}
234+
}
235+
236+
export class VerifyBotAuthCreateBuilder {
237+
private _target?: string;
238+
private _type?: VerifyBotAuthType | string;
239+
240+
constructor() { }
241+
242+
target(target: string | undefined): VerifyBotAuthCreateBuilder {
243+
this._target = target;
244+
return this;
245+
}
246+
247+
type(type: VerifyBotAuthType | string | undefined): VerifyBotAuthCreateBuilder {
248+
this._type = type;
249+
return this;
250+
}
251+
252+
build(): VerifyBotAuthCreate {
253+
return new VerifyBotAuthCreate({
254+
target: this._target,
255+
type: this._type,
256+
});
257+
}
258+
}
259+
260+
export interface VerifyBotAuthCreated {
261+
state?: string;
262+
exp?: number;
263+
link?: string;
264+
qr_link?: string;
265+
error?: string;
266+
}
267+
268+
export interface VerifyBotAuthGetState {
269+
target?: string;
270+
status?: VerifyBotAuthStatus | string;
271+
data?: any;
272+
error?: string;
273+
}
274+
213275
export {
214276
Provider,
215277
ProviderType,

src/service.ts

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,89 @@ class VerifymeService {
8585
};
8686
});
8787
}
88+
89+
///////// VerifyBotAuth /////////
90+
async createBotAuth(
91+
body: any,
92+
headers?: any,
93+
timeout?: number
94+
): Promise<CreateVerifyResponse> {
95+
const config: AxiosRequestConfig = {
96+
method: 'POST',
97+
url: `${this.url}/botauth`,
98+
data: body,
99+
headers: headers,
100+
timeout: timeout ? timeout * 1000 : undefined,
101+
};
102+
103+
return axios(config)
104+
.then((response) => response.data)
105+
.catch((error) => {
106+
if (error.response) {
107+
console.debug(
108+
`CreateBotAuth Error: ${JSON.stringify(
109+
error.response.data
110+
)}`
111+
);
112+
113+
return {
114+
...error.response.data,
115+
};
116+
} else if (error.request) {
117+
console.debug(
118+
`CreateBotAuth Error: ${JSON.stringify(error.request)}`
119+
);
120+
121+
return {
122+
error: error.request,
123+
};
124+
}
125+
126+
return {
127+
error: error?.message ?? 'Unknown error',
128+
};
129+
});
130+
}
131+
132+
async getBotAuth(
133+
state: string,
134+
headers?: any,
135+
timeout?: number
136+
): Promise<VerifyMessageResponse> {
137+
const config: AxiosRequestConfig = {
138+
method: 'GET',
139+
url: `${this.url}/botauth/${state}`,
140+
headers: headers,
141+
timeout: timeout ? timeout * 1000 : undefined,
142+
};
143+
144+
return axios(config)
145+
.then((response) => response.data)
146+
.catch((error) => {
147+
if (error.response) {
148+
console.debug(
149+
`GetBotAuth Error: ${JSON.stringify(error.response.data)}`
150+
);
151+
152+
return {
153+
...error.response.data,
154+
};
155+
} else if (error.request) {
156+
console.debug(
157+
`GetBotAuth Error: ${JSON.stringify(error.request)}`
158+
);
159+
160+
return {
161+
error: error.request,
162+
};
163+
}
164+
165+
return {
166+
error: error?.message ?? 'Unknown error',
167+
};
168+
});
169+
}
170+
///////// VerifyBotAuth /////////
88171
}
89172

90173
export { VerifymeService };

src/verifybotauth.ts

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import { VerifyBotAuthCreate, VerifyBotAuthCreated, VerifyBotAuthGetState } from "./model";
2+
import { VerifymeService } from "./service";
3+
4+
export class VerifyBotAuth {
5+
private readonly _logger = console;
6+
private readonly _service: VerifymeService;
7+
8+
constructor(service: VerifymeService) {
9+
this._service = service;
10+
}
11+
12+
async auth(request: VerifyBotAuthCreate): Promise<VerifyBotAuthCreated> {
13+
this._logger.info(`[VerifyBotAuth] Creating auth with target: ${request.target}`);
14+
15+
const response = await this._service.createBotAuth(request);
16+
return response;
17+
}
18+
19+
async state(state: string): Promise<VerifyBotAuthGetState> {
20+
if (!state) {
21+
throw new Error('State is required');
22+
}
23+
24+
this._logger.info(`[VerifyBotAuth] Getting auth state with state: ${state}`);
25+
26+
const response = await this._service.getBotAuth(state);
27+
return response;
28+
}
29+
}

src/verifyme.ts

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,20 @@
11
import { CreateVerifyRequest, CreateVerifyResponse, VerifyMessageRequest, VerifyMessageResponse, VerifymeOptions } from "./model";
22
import { VerifymeService } from "./service";
33
import { getSystemHostname, getSystemUsername } from "./util";
4+
import { VerifyBotAuth } from "./verifybotauth";
45

56
export class Verifyme {
67
private static readonly _logger = console;
78
private static readonly NAME = 'verifyme';
8-
private static readonly VERSION = '0.0.4';
9-
private static readonly VERSION_CODE = '2';
9+
private static readonly VERSION = '0.1.5';
10+
private static readonly VERSION_CODE = '3';
1011
private static readonly DEFAULT_URL = 'https://verifyme-api.cubetiq.app';
1112
private static readonly API_KEY_HEADER_PREFIX = 'x-api-key';
1213
private static readonly DEFAULT_CONNECT_TIMEOUT = 60; // seconds
1314

1415
private _options!: VerifymeOptions;
1516
private _service!: VerifymeService;
17+
private _botAuth!: VerifyBotAuth;
1618

1719
constructor(options: VerifymeOptions) {
1820
if (!options.apiKey) {
@@ -26,6 +28,9 @@ export class Verifyme {
2628
// Initialize service
2729
this._service = new VerifymeService(this._options.url);
2830

31+
// Initialize bot auth
32+
this._botAuth = new VerifyBotAuth(this._service);
33+
2934
Verifyme._logger.log(`[Verifyme] Initialized SDK Version: ${Verifyme.VERSION}-${Verifyme.VERSION_CODE}`);
3035
}
3136

@@ -37,7 +42,7 @@ export class Verifyme {
3742
const sender = getSystemUsername() ?? 'unknown';
3843

3944
// User agent to request
40-
const userAgent = `verifyme-sdk-node/${Verifyme.VERSION}-${Verifyme.VERSION_CODE} (${hostname}:${sender})`;
45+
const userAgent = `${Verifyme.NAME}-sdk-node/${Verifyme.VERSION}-${Verifyme.VERSION_CODE} (${hostname}:${sender})`;
4146
Verifyme._logger.info(`[Verifyme] User agent: ${userAgent}`);
4247

4348
const headers: Record<string, string> = {
@@ -71,6 +76,10 @@ export class Verifyme {
7176
return response;
7277
}
7378

79+
botAuth(): VerifyBotAuth {
80+
return this._botAuth;
81+
}
82+
7483
static create(options: VerifymeOptions): Verifyme {
7584
return new Verifyme(options);
7685
}

0 commit comments

Comments
 (0)