-
-
Notifications
You must be signed in to change notification settings - Fork 0
Add support for fetching subscribed newsletters (Whatsapp Channels) #13
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,60 @@ | ||
| import { SocketConfig } from '../Types' | ||
| import { GraphQLResponse, NewsletterMetadata } from '../Types/Newsletter' | ||
| import { convertKeysToCamelCase, getBinaryNodeChildBuffer, S_WHATSAPP_NET } from '..' | ||
| import { makeRegistrationSocket } from './registration' | ||
|
|
||
| export function makeNewslettersSocket(config: SocketConfig) { | ||
| const sock = makeRegistrationSocket(config) | ||
| const { query } = sock | ||
|
|
||
| const mexQuery = async(queryId: string, variables = {}) => { | ||
| const response = await query({ | ||
| tag: 'iq', | ||
| attrs: { | ||
| type: 'get', | ||
| xmlns: 'w:mex', | ||
| to: S_WHATSAPP_NET | ||
| }, | ||
| content: [ { tag: 'query', attrs: { 'query_id': queryId }, content: JSON.stringify({ variables }) } ] | ||
| }) | ||
|
|
||
| const result = getBinaryNodeChildBuffer(response, 'result') | ||
|
|
||
| if(!result) { | ||
| throw new Error('Missing "result" element in response') | ||
| } | ||
|
|
||
| const gqlResp: GraphQLResponse = JSON.parse(result.toString()) | ||
|
|
||
| if(gqlResp.errors?.length > 0) { | ||
| throw new Error('Graphql Error when fetching newsletters') | ||
| } | ||
|
|
||
| return gqlResp.data | ||
| } | ||
|
|
||
| // const queryFetchNewsletter = '6563316087068696' | ||
| // const queryFetchNewsletterDehydrated = '7272540469429201' | ||
| // const queryRecommendedNewsletters = '7263823273662354' // variables -> input -> {limit: 20, country_codes: [string]}, output: xwa2_newsletters_recommended | ||
| // const queryNewslettersDirectory = '6190824427689257' // variables -> input -> {view: "RECOMMENDED", limit: 50, start_cursor: base64, filters: {country_codes: [string]}} | ||
| const querySubscribedNewsletters = '6388546374527196' // variables -> empty, output: xwa2_newsletter_subscribed | ||
| // const queryNewsletterSubscribers = '9800646650009898' // variables -> input -> {newsletter_id, count}, output: xwa2_newsletter_subscribers -> subscribers -> edges | ||
| // const mutationMuteNewsletter = '6274038279359549' // variables -> {newsletter_id, updates->{description, settings}}, output: xwa2_newsletter_update -> NewsletterMetadata without viewer meta | ||
| // const mutationUnmuteNewsletter = '6068417879924485' | ||
| // const mutationUpdateNewsletter = '7150902998257522' | ||
| // const mutationCreateNewsletter = '6234210096708695' | ||
| // const mutationUnfollowNewsletter = '6392786840836363' | ||
| // const mutationFollowNewsletter = '9926858900719341' | ||
|
Comment on lines
+36
to
+47
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Leaving the ids for the remaining queries and mutations here for future reference. |
||
|
|
||
| return { | ||
| ...sock, | ||
| getSubscribedNewsletters: async() => { | ||
| const result = await mexQuery(querySubscribedNewsletters, {}) | ||
| if(result?.['xwa2_newsletter_subscribed']) { | ||
| return convertKeysToCamelCase(result['xwa2_newsletter_subscribed']) as NewsletterMetadata[] | ||
| } else { | ||
| throw new Error('Missing "xwa2_newsletter_subscribed" in response') | ||
| } | ||
| }, | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,78 @@ | ||
| export interface NewsletterMuted { | ||
| muted: boolean | ||
| } | ||
|
|
||
| interface WrappedNewsletterState { | ||
| type: string | ||
| } | ||
|
|
||
| export interface NewsletterMetadata { | ||
| id: string | ||
| state: WrappedNewsletterState | ||
| threadMetadata: NewsletterThreadMetadata | ||
| viewerMetadata: NewsletterViewerMetadata | null | ||
| } | ||
|
|
||
| interface NewsletterViewerMetadata { | ||
| mute: string | ||
| role: string | ||
| } | ||
|
|
||
| interface NewsletterReactionSettings { | ||
| value: string | ||
| } | ||
|
|
||
| interface NewsletterSettings { | ||
| reactionCodes: NewsletterReactionSettings | ||
| } | ||
|
|
||
| interface ProfilePictureInfo { | ||
| url: string | ||
| id: string | ||
| type: string | ||
| directPath: string | ||
| } | ||
|
|
||
| interface NewsletterThreadMetadata { | ||
| creationTime: string | ||
| invite: string | ||
| name: NewsletterText | ||
| description: NewsletterText | ||
| subscribersCount: number | ||
| verification: string | ||
| picture: ProfilePictureInfo | null | ||
| preview: ProfilePictureInfo | ||
| settings: NewsletterSettings | ||
| } | ||
|
|
||
| interface NewsletterText { | ||
| text: string | ||
| id: string | ||
| updateTime: string | ||
| } | ||
|
|
||
| export interface NewsletterMessage { | ||
| messageServerId: string | ||
| viewsCount: number | ||
| reactionCounts: { [key: string]: number } | ||
| message: any // Replace with actual type | ||
| } | ||
|
|
||
| interface GraphQLErrorExtensions { | ||
| errorCode: number | ||
| isRetryable: boolean | ||
| severity: string | ||
| } | ||
|
|
||
| interface GraphQLError { | ||
| extensions: GraphQLErrorExtensions | ||
| message: string | ||
| path: string[] | ||
| } | ||
|
|
||
| interface GraphQLErrors extends Array<GraphQLError> {} | ||
|
|
||
| export interface GraphQLResponse { | ||
| data: any // Replace with actual type | ||
| errors: GraphQLErrors | ||
| } | ||
|
Comment on lines
+1
to
+78
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Some of these types (ported from whatsmeow) are not used yet, but they will be once the remaining queries/mutations are implemented. |
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Baileys uses factory functions to create the final socket object:
Each one call the next. I'm not sure if the order matters here, so I just put
makeNewslettersSocketa the end.Any insight here?