mirror of
https://github.com/FranP-code/Baileys.git
synced 2025-10-13 00:32:22 +00:00
[READY FOR MERGE] Implement newsletter (#1532)
* feat: implement basic newsletter functionality with socket integration and event handling * feat: enhance media handling for newsletters with raw media upload support * feat: working updatePicture, removePicure, adminCount, mute, Unmute * fix: fetchMessages * chore: cleanup * fix: update newsletter metadata path and query ID for consistency. newsletterMetadata works now * chore: enhance newsletter metadata parsing and error handling * fix: correct DELETE QueryId value in Newsletter.ts * chore: split mex stuffs to own file * chore: remove as any
This commit is contained in:
committed by
GitHub
parent
5ffb19120d
commit
8391c02e0b
58
src/Socket/mex.ts
Normal file
58
src/Socket/mex.ts
Normal file
@@ -0,0 +1,58 @@
|
||||
import { Boom } from '@hapi/boom'
|
||||
import { BinaryNode } from '../WABinary'
|
||||
import { getBinaryNodeChild, S_WHATSAPP_NET } from '../WABinary'
|
||||
|
||||
const wMexQuery = (
|
||||
variables: Record<string, unknown>,
|
||||
queryId: string,
|
||||
query: (node: BinaryNode) => Promise<BinaryNode>,
|
||||
generateMessageTag: () => string
|
||||
) => {
|
||||
return query({
|
||||
tag: 'iq',
|
||||
attrs: {
|
||||
id: 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')
|
||||
}
|
||||
]
|
||||
})
|
||||
}
|
||||
|
||||
export const executeWMexQuery = async <T>(
|
||||
variables: Record<string, unknown>,
|
||||
queryId: string,
|
||||
dataPath: string,
|
||||
query: (node: BinaryNode) => Promise<BinaryNode>,
|
||||
generateMessageTag: () => string
|
||||
): Promise<T> => {
|
||||
const result = await wMexQuery(variables, queryId, query, generateMessageTag)
|
||||
const child = getBinaryNodeChild(result, 'result')
|
||||
if (child?.content) {
|
||||
const data = JSON.parse(child.content.toString())
|
||||
|
||||
if (data.errors && data.errors.length > 0) {
|
||||
const errorMessages = data.errors.map((err: Error) => err.message || 'Unknown error').join(', ')
|
||||
const firstError = data.errors[0]
|
||||
const errorCode = firstError.extensions?.error_code || 400
|
||||
throw new Boom(`GraphQL server error: ${errorMessages}`, { statusCode: errorCode, data: firstError })
|
||||
}
|
||||
|
||||
const response = dataPath ? data?.data?.[dataPath] : data?.data
|
||||
if (typeof response !== 'undefined') {
|
||||
return response as T
|
||||
}
|
||||
}
|
||||
|
||||
const action = (dataPath || '').startsWith('xwa2_')
|
||||
? dataPath.substring(5).replace(/_/g, ' ')
|
||||
: dataPath?.replace(/_/g, ' ')
|
||||
throw new Boom(`Failed to ${action}, unexpected response structure.`, { statusCode: 400, data: result })
|
||||
}
|
||||
Reference in New Issue
Block a user