mirror of
https://github.com/FranP-code/Baileys.git
synced 2025-10-13 00:32:22 +00:00
feat: use axios and config.options for registration
This commit is contained in:
@@ -62,8 +62,6 @@ const startSock = async() => {
|
|||||||
|
|
||||||
if(!registration.phoneNumber) {
|
if(!registration.phoneNumber) {
|
||||||
registration.phoneNumber = await question('Please enter your mobile phone number:\n')
|
registration.phoneNumber = await question('Please enter your mobile phone number:\n')
|
||||||
} else {
|
|
||||||
console.log('Your mobile phone number is not registered.')
|
|
||||||
}
|
}
|
||||||
|
|
||||||
const phoneNumber = parsePhoneNumber(registration!.phoneNumber)
|
const phoneNumber = parsePhoneNumber(registration!.phoneNumber)
|
||||||
|
|||||||
@@ -1,4 +1,5 @@
|
|||||||
/* eslint-disable camelcase */
|
/* eslint-disable camelcase */
|
||||||
|
import axios, { AxiosRequestConfig } from 'axios'
|
||||||
import { MOBILE_REGISTRATION_ENDPOINT, MOBILE_TOKEN, MOBILE_USERAGENT, REGISTRATION_PUBLIC_KEY } from '../Defaults'
|
import { MOBILE_REGISTRATION_ENDPOINT, MOBILE_TOKEN, MOBILE_USERAGENT, REGISTRATION_PUBLIC_KEY } from '../Defaults'
|
||||||
import { KeyPair, SignedKeyPair, SocketConfig } from '../Types'
|
import { KeyPair, SignedKeyPair, SocketConfig } from '../Types'
|
||||||
import { aesEncryptGCM, Curve, md5 } from '../Utils/crypto'
|
import { aesEncryptGCM, Curve, md5 } from '../Utils/crypto'
|
||||||
@@ -22,7 +23,7 @@ export const makeRegistrationSocket = (config: SocketConfig) => {
|
|||||||
throw new Error('please specify the registration options')
|
throw new Error('please specify the registration options')
|
||||||
}
|
}
|
||||||
|
|
||||||
const result = await mobileRegister({ ...sock.authState.creds, ...sock.authState.creds.registration as RegistrationOptions, code })
|
const result = await mobileRegister({ ...sock.authState.creds, ...sock.authState.creds.registration as RegistrationOptions, code }, config.options)
|
||||||
|
|
||||||
sock.authState.creds.me = {
|
sock.authState.creds.me = {
|
||||||
id: jidEncode(result.login!, 's.whatsapp.net'),
|
id: jidEncode(result.login!, 's.whatsapp.net'),
|
||||||
@@ -49,7 +50,7 @@ export const makeRegistrationSocket = (config: SocketConfig) => {
|
|||||||
|
|
||||||
sock.ev.emit('creds.update', sock.authState.creds)
|
sock.ev.emit('creds.update', sock.authState.creds)
|
||||||
|
|
||||||
return mobileRegisterCode({ ...config.auth.creds, ...registrationOptions })
|
return mobileRegisterCode({ ...config.auth.creds, ...registrationOptions }, config.options)
|
||||||
}
|
}
|
||||||
|
|
||||||
return {
|
return {
|
||||||
@@ -146,7 +147,7 @@ export function registrationParams(params: RegistrationParams) {
|
|||||||
/**
|
/**
|
||||||
* Requests a registration code for the given phone number.
|
* Requests a registration code for the given phone number.
|
||||||
*/
|
*/
|
||||||
export function mobileRegisterCode(params: RegistrationParams) {
|
export function mobileRegisterCode(params: RegistrationParams, fetchOptions?: AxiosRequestConfig) {
|
||||||
return mobileRegisterFetch('/code', {
|
return mobileRegisterFetch('/code', {
|
||||||
params: {
|
params: {
|
||||||
...registrationParams(params),
|
...registrationParams(params),
|
||||||
@@ -158,23 +159,26 @@ export function mobileRegisterCode(params: RegistrationParams) {
|
|||||||
reason: '',
|
reason: '',
|
||||||
hasav: '1'
|
hasav: '1'
|
||||||
},
|
},
|
||||||
|
...fetchOptions,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
export function mobileRegisterExists(params: RegistrationParams) {
|
export function mobileRegisterExists(params: RegistrationParams, fetchOptions?: AxiosRequestConfig) {
|
||||||
return mobileRegisterFetch('/exist', {
|
return mobileRegisterFetch('/exist', {
|
||||||
params: registrationParams(params)
|
params: registrationParams(params),
|
||||||
|
...fetchOptions
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Registers the phone number on whatsapp with the received OTP code.
|
* Registers the phone number on whatsapp with the received OTP code.
|
||||||
*/
|
*/
|
||||||
export async function mobileRegister(params: RegistrationParams & { code: string }) {
|
export async function mobileRegister(params: RegistrationParams & { code: string }, fetchOptions?: AxiosRequestConfig) {
|
||||||
//const result = await mobileRegisterFetch(`/reg_onboard_abprop?cc=${params.phoneNumberCountryCode}&in=${params.phoneNumberNationalNumber}&rc=0`)
|
//const result = await mobileRegisterFetch(`/reg_onboard_abprop?cc=${params.phoneNumberCountryCode}&in=${params.phoneNumberNationalNumber}&rc=0`)
|
||||||
|
|
||||||
return mobileRegisterFetch('/register', {
|
return mobileRegisterFetch('/register', {
|
||||||
params: { ...registrationParams(params), code: params.code.replace('-', '') },
|
params: { ...registrationParams(params), code: params.code.replace('-', '') },
|
||||||
|
...fetchOptions,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -190,7 +194,7 @@ export function mobileRegisterEncrypt(data: string) {
|
|||||||
return Buffer.concat([Buffer.from(keypair.public), buffer]).toString('base64url')
|
return Buffer.concat([Buffer.from(keypair.public), buffer]).toString('base64url')
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function mobileRegisterFetch(path: string, opts: { params?: Record<string, string>, headers?: Record<string, string> } = {}) {
|
export async function mobileRegisterFetch(path: string, opts: AxiosRequestConfig = {}) {
|
||||||
let url = `${MOBILE_REGISTRATION_ENDPOINT}${path}`
|
let url = `${MOBILE_REGISTRATION_ENDPOINT}${path}`
|
||||||
|
|
||||||
if(opts.params) {
|
if(opts.params) {
|
||||||
@@ -205,6 +209,7 @@ export async function mobileRegisterFetch(path: string, opts: { params?: Record<
|
|||||||
// const params = urlencode(mobileRegisterEncrypt(parameter.join('&')))
|
// const params = urlencode(mobileRegisterEncrypt(parameter.join('&')))
|
||||||
// url += `?ENC=${params}`
|
// url += `?ENC=${params}`
|
||||||
url += `?${parameter.join('&')}`
|
url += `?${parameter.join('&')}`
|
||||||
|
delete opts.params
|
||||||
}
|
}
|
||||||
|
|
||||||
if(!opts.headers) {
|
if(!opts.headers) {
|
||||||
@@ -213,17 +218,11 @@ export async function mobileRegisterFetch(path: string, opts: { params?: Record<
|
|||||||
|
|
||||||
opts.headers['User-Agent'] = MOBILE_USERAGENT
|
opts.headers['User-Agent'] = MOBILE_USERAGENT
|
||||||
|
|
||||||
const response = await fetch(url, opts)
|
const response = await axios(url, opts)
|
||||||
|
|
||||||
const text = await response.text()
|
var json = response.data
|
||||||
|
|
||||||
try {
|
if(response.status > 300 || json.reason) {
|
||||||
var json = JSON.parse(text)
|
|
||||||
} catch(error) {
|
|
||||||
throw text
|
|
||||||
}
|
|
||||||
|
|
||||||
if(!response.ok || json.reason) {
|
|
||||||
throw json
|
throw json
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user