diff --git a/src/modules/1country/api/relayApi.ts b/src/modules/1country/api/relayApi.ts index e77ba9c2..1fee4349 100644 --- a/src/modules/1country/api/relayApi.ts +++ b/src/modules/1country/api/relayApi.ts @@ -1,4 +1,4 @@ -import axios from 'axios' +import axios, { AxiosError } from 'axios' import config from '../../../config' @@ -8,7 +8,7 @@ const base = axios.create({ }) export const relayApi = (): { - enableSubdomains: (domainName: string) => Promise + enableSubdomains: (domainName: string) => Promise checkDomain: ({ sld }: { sld: string }) => Promise<{ isAvailable: any, renewPrice: any, responseText: any, isReserved: any, isRegistered: any, error: string, regPrice: any, restorePrice: any, transferPrice: any } | { error: string }> createCert: ({ domain, @@ -18,9 +18,26 @@ export const relayApi = (): { genNFT: ({ domain }: { domain: string }) => Promise<{ metadata: any, generated: any }> } => { return { - enableSubdomains: async (domainName: string) => { - const { data } = await base.post('/enable-subdomains', { domain: `${domainName}${config.country.tld}` }) - console.log('enableSubdomains', data) + enableSubdomains: async (domainName: string): Promise => { + try { + const { data } = await base.post('dns/enable-subdomains', { domain: `${domainName}${config.country.tld}` }) + console.log('enableSubdomains', data) + return true + } catch (e) { + if (e instanceof AxiosError) { + if (e.code === AxiosError.ERR_BAD_REQUEST) { + const data = e.response?.data + if (data && data.error === 'already enabled') { + console.log('Axios response:', data.error) + return true + } + } + console.log('Axios Error:', e.message) + return false + } + console.log(e) + return false + } }, checkDomain: async ({ sld }: { sld: string }) => { try { diff --git a/src/modules/1country/index.ts b/src/modules/1country/index.ts index 83880f5b..6f2394be 100644 --- a/src/modules/1country/index.ts +++ b/src/modules/1country/index.ts @@ -21,7 +21,8 @@ export enum SupportedCommands { check = 'check', cert = 'cert', nft = 'nft', - set = 'set' + set = 'set', + subdomain = 'subdomain' } const COUNTRY_PREFIX_LIST = ['+', '%'] @@ -113,6 +114,11 @@ export class OneCountryBot implements PayableBot { return } + if (ctx.hasCommand(SupportedCommands.subdomain)) { + await this.onEnableSubdomain(ctx) + return + } + // if (ctx.hasCommand(SupportedCommands.RENEW)) { // this.onRenewCmd(ctx); // return; @@ -385,6 +391,54 @@ export class OneCountryBot implements PayableBot { } } + onEnableSubdomain = async (ctx: OnMessageContext | OnCallBackQueryData): Promise => { + try { + if (this.botSuspended) { + ctx.transient.analytics.sessionState = RequestState.Error + await sendMessage(ctx, 'The bot is suspended').catch(async (e) => { + await this.onError(ctx, e) + }) + ctx.transient.analytics.actualResponseTime = now() + return + } + if (!ctx.match) { + await ctx.reply(appText.setParameterError, { + message_thread_id: ctx.message?.message_thread_id, + parse_mode: 'Markdown' + }).catch(async (e) => { + await this.onError(ctx, e) + }) + return + } + const params = (ctx.match as string).split(' ') + const [domain] = params + const isDomain = await isDomainAvailable(domain) + if (isDomain.isAvailable) { + await ctx.reply(`The domain ${domain} doesn't exist`, { + message_thread_id: ctx.message?.message_thread_id, + parse_mode: 'Markdown' + }).catch(async (e) => { + await this.onError(ctx, e) + }) + ctx.transient.analytics.actualResponseTime = now() + ctx.transient.analytics.sessionState = RequestState.Error + return + } + const response = await relayApi().enableSubdomains(domain) + if (response) { + await ctx.reply('Subdomain feature enabled') + } else { + await ctx.reply('Failed to enable subdomain feature') + } + ctx.transient.analytics.actualResponseTime = now() + ctx.transient.analytics.sessionState = RequestState.Success + } catch (e) { + ctx.transient.analytics.sessionState = RequestState.Error + ctx.transient.analytics.actualResponseTime = now() + await this.onError(ctx, e) + } + } + onCertCmd = async (ctx: OnMessageContext | OnCallBackQueryData): Promise => { if (await isAdmin(ctx, false, true)) { if (!ctx.match) {