Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- const wMexQuery = (variables, queryId) => {
- return sock.query({
- tag: 'iq',
- attrs: {
- id: sock.generateMessageTag(),
- type: 'get',
- to: 's.whatsapp.net',
- xmlns: 'w:mex'
- },
- content: [
- {
- tag: 'query',
- attrs: { query_id: queryId },
- content: Buffer.from(JSON.stringify({ variables }), 'utf-8')
- }
- ]
- })
- }
- const executeWMexQuery = async (variables, queryId, dataPath) => {
- const result = await wMexQuery(variables, queryId)
- const child = result?.content?.[0]
- if (child?.content) {
- const data = JSON.parse(child.content.toString())
- if (data.errors && data.errors.length > 0) {
- throw new Error(data.errors.map(err => err.message).join(', '))
- }
- const response = dataPath ? data?.data?.[dataPath] : data?.data
- if (typeof response !== 'undefined') {
- return response
- }
- }
- throw new Error('Failed to execute query')
- }
- const USERNAME_QUERY_IDS = {
- CHECK: '26124072630599518'
- }
- const USERNAME_CHECK_RESULT = {
- SUCCESS: 'SUCCESS'
- }
- const USERNAME_SOURCE = {
- USER_INPUT: 'USER_INPUT'
- }
- const checkUsername = async (username, includeSuggestions = true, sessionId) => {
- const crypto = require('crypto')
- const session_id = sessionId || crypto.randomUUID()
- const data = await executeWMexQuery(
- {
- username,
- include_suggestions: includeSuggestions,
- session_id,
- source: USERNAME_SOURCE.USER_INPUT
- },
- USERNAME_QUERY_IDS.CHECK,
- 'xwa2_username_check'
- )
- if (data?.result === USERNAME_CHECK_RESULT.SUCCESS) {
- return { available: true, username, session_id }
- }
- return {
- available: false,
- username,
- session_id,
- suggestions: data?.suggestions ?? [],
- rejectionReasons: data?.rejection_reasons ?? [],
- suggestionsEligible: data?.suggestions_eligible ?? true
- }
- }
- /* result:
- {
- available: false,
- username: 'username',
- session_id: 'xxxx-35a9-4f4a-xxxx-xxxx',
- suggestions: [ 'username1', 'username2', 'usermame3' ],
- rejectionReasons: [],
- suggestionsEligible: true
- }
- */
Advertisement