WhiskeySockets / Baileys

Lightweight full-featured typescript/javascript WhatsApp Web API
https://baileys.whiskeysockets.io/
MIT License
4.1k stars 1.38k forks source link

How Invite Admin channel in bot?? #1045

Closed cifumo closed 2 months ago

cifumo commented 2 months ago

howwww

Akkun3704 commented 2 months ago

You mean sending channel admin invitation using bot?

cifumo commented 2 months ago

You mean sending channel admin invitation using bot?

yess

Akkun3704 commented 2 months ago

Screenshot_2024-09-19-12-36-06-397_com whatsapp Yeah you can

Akkun3704 commented 2 months ago

My code

const {
    getBinaryNodeChild,
    getBinaryNodeChildString,
    generateWAMessageFromContent,
    isLidUser,
    S_WHATSAPP_NET
} = require('baileys')

const fetchUserLid = async (jid) => {
    // based on https://github.com/WhiskeySockets/Baileys/blob/master/src/Socket/chats.ts#L142-L181
    const [result] = await conn.sendInteractiveQuery([
        {
            tag: 'lid',
            attrs: {}
        }
    ], [
        {
            tag: 'user',
            attrs: { jid }
        }
    ])
    if (result) {
        const lid = getBinaryNodeChild(result, 'lid')
        return lid.attrs.val
    }
}

const getNewsletterInvite = async (newsletterJid, userLid) => {
    const encoder = new TextEncoder()
    const node = await conn.query({
        tag: 'iq',
        attrs: {
            to: S_WHATSAPP_NET,
            xmlns: 'w:mex',
            type: 'get'
        },
        content: [{
            tag: 'query',
            attrs: { query_id: '6826078034173770' },
            content: encoder.encode(JSON.stringify({
                variables: {
                    newsletter_id: newsletterJid,
                    user_id: userLid
                }
            }))
        }]
    })
    const result = JSON.parse(getBinaryNodeChildString(node, 'result'))
    // if (result.errors) throw result.errors
    return result.data['xwa2_newsletter_admin_invite_create']
}

const sendNewsletterAdminInvite = async (jid, content, options) => {
    jid = isLidUser(jid) ? jid : await fetchUserLid(jid)

    const inviteResp = await getNewsletterInvite(content.newsletter.jid, jid)
    const fullMsg = await generateWAMessageFromContent(jid, {
        newsletterAdminInviteMessage: {
            newsletterJid: inviteResp.id,
            newsletterName: content.newsletter.name,
            jpegThumbnail: content.newsletter.thumbnail,
            caption: content.newsletter.text,
            inviteExpiration: inviteResp.invite_expiration_time
        }
    }, options)
    await conn.relayMessage(jid, fullMsg.message, options)
    return fullMsg
}

return sendNewsletterAdminInvite('xxx@s.whatsapp.net', {
    newsletter: {
        jid: 'xxx@newsletter',
        name: 'Test',
        text: 'Masuk gan',
        thumbnail: Buffer.alloc(0) // customize your own thumbnail 
    }
}, {})
cifumo commented 2 months ago

My code

const {
  getBinaryNodeChild,
  getBinaryNodeChildString,
  generateWAMessageFromContent,
  isLidUser,
  S_WHATSAPP_NET
} = require('baileys')

const fetchUserLid = async (jid) => {
  // based on https://github.com/WhiskeySockets/Baileys/blob/master/src/Socket/chats.ts#L142-L181
  const [result] = await conn.sendInteractiveQuery([
      {
          tag: 'lid',
          attrs: {}
      }
  ], [
      {
          tag: 'user',
          attrs: { jid }
      }
  ])
  if (result) {
      const lid = getBinaryNodeChild(result, 'lid')
      return lid.attrs.val
  }
}

const getNewsletterInvite = async (newsletterJid, userLid) => {
  const encoder = new TextEncoder()
  const node = await conn.query({
      tag: 'iq',
      attrs: {
          to: S_WHATSAPP_NET,
          xmlns: 'w:mex',
          type: 'get'
      },
      content: [{
          tag: 'query',
          attrs: { query_id: '6826078034173770' },
          content: encoder.encode(JSON.stringify({
              variables: {
                  newsletter_id: newsletterJid,
                  user_id: userLid
              }
          }))
      }]
  })
  const result = JSON.parse(getBinaryNodeChildString(node, 'result'))
  // if (result.errors) throw result.errors
  return result.data['xwa2_newsletter_admin_invite_create']
}

const sendNewsletterAdminInvite = async (jid, content, options) => {
  jid = isLidUser(jid) ? jid : await fetchUserLid(jid)

  const inviteResp = await getNewsletterInvite(content.newsletter.jid, jid)
  const fullMsg = await generateWAMessageFromContent(jid, {
      newsletterAdminInviteMessage: {
          newsletterJid: inviteResp.id,
          newsletterName: content.newsletter.name,
          jpegThumbnail: content.newsletter.thumbnail,
          caption: content.newsletter.text,
          inviteExpiration: inviteResp.invite_expiration_time
      }
  }, options)
  await conn.relayMessage(jid, fullMsg.message, options)
  return fullMsg
}

return sendNewsletterAdminInvite('xxx@s.whatsapp.net', {
  newsletter: {
      jid: 'xxx@newsletter',
      name: 'Test',
      text: 'Masuk gan',
      thumbnail: Buffer.alloc(0) // customize your own thumbnail 
  }
}, {})

arigatoo

cifumo commented 2 months ago

My code

const {
  getBinaryNodeChild,
  getBinaryNodeChildString,
  generateWAMessageFromContent,
  isLidUser,
  S_WHATSAPP_NET
} = require('baileys')

const fetchUserLid = async (jid) => {
  // based on https://github.com/WhiskeySockets/Baileys/blob/master/src/Socket/chats.ts#L142-L181
  const [result] = await conn.sendInteractiveQuery([
      {
          tag: 'lid',
          attrs: {}
      }
  ], [
      {
          tag: 'user',
          attrs: { jid }
      }
  ])
  if (result) {
      const lid = getBinaryNodeChild(result, 'lid')
      return lid.attrs.val
  }
}

const getNewsletterInvite = async (newsletterJid, userLid) => {
  const encoder = new TextEncoder()
  const node = await conn.query({
      tag: 'iq',
      attrs: {
          to: S_WHATSAPP_NET,
          xmlns: 'w:mex',
          type: 'get'
      },
      content: [{
          tag: 'query',
          attrs: { query_id: '6826078034173770' },
          content: encoder.encode(JSON.stringify({
              variables: {
                  newsletter_id: newsletterJid,
                  user_id: userLid
              }
          }))
      }]
  })
  const result = JSON.parse(getBinaryNodeChildString(node, 'result'))
  // if (result.errors) throw result.errors
  return result.data['xwa2_newsletter_admin_invite_create']
}

const sendNewsletterAdminInvite = async (jid, content, options) => {
  jid = isLidUser(jid) ? jid : await fetchUserLid(jid)

  const inviteResp = await getNewsletterInvite(content.newsletter.jid, jid)
  const fullMsg = await generateWAMessageFromContent(jid, {
      newsletterAdminInviteMessage: {
          newsletterJid: inviteResp.id,
          newsletterName: content.newsletter.name,
          jpegThumbnail: content.newsletter.thumbnail,
          caption: content.newsletter.text,
          inviteExpiration: inviteResp.invite_expiration_time
      }
  }, options)
  await conn.relayMessage(jid, fullMsg.message, options)
  return fullMsg
}

return sendNewsletterAdminInvite('xxx@s.whatsapp.net', {
  newsletter: {
      jid: 'xxx@newsletter',
      name: 'Test',
      text: 'Masuk gan',
      thumbnail: Buffer.alloc(0) // customize your own thumbnail 
  }
}, {})

Screenshot_20240919_175258 nooo error huhu

Akkun3704 commented 2 months ago

My code

const {
    getBinaryNodeChild,
    getBinaryNodeChildString,
    generateWAMessageFromContent,
    isLidUser,
    S_WHATSAPP_NET
} = require('baileys')

const fetchUserLid = async (jid) => {
    // based on https://github.com/WhiskeySockets/Baileys/blob/master/src/Socket/chats.ts#L142-L181
    const [result] = await conn.sendInteractiveQuery([
        {
            tag: 'lid',
            attrs: {}
        }
    ], [
        {
            tag: 'user',
            attrs: { jid }
        }
    ])
    if (result) {
        const lid = getBinaryNodeChild(result, 'lid')
        return lid.attrs.val
    }
}

const getNewsletterInvite = async (newsletterJid, userLid) => {
    const encoder = new TextEncoder()
    const node = await conn.query({
        tag: 'iq',
        attrs: {
            to: S_WHATSAPP_NET,
            xmlns: 'w:mex',
            type: 'get'
        },
        content: [{
            tag: 'query',
            attrs: { query_id: '6826078034173770' },
            content: encoder.encode(JSON.stringify({
                variables: {
                    newsletter_id: newsletterJid,
                    user_id: userLid
                }
            }))
        }]
    })
    const result = JSON.parse(getBinaryNodeChildString(node, 'result'))
    // if (result.errors) throw result.errors
    return result.data['xwa2_newsletter_admin_invite_create']
}

const sendNewsletterAdminInvite = async (jid, content, options) => {
    jid = isLidUser(jid) ? jid : await fetchUserLid(jid)

    const inviteResp = await getNewsletterInvite(content.newsletter.jid, jid)
    const fullMsg = await generateWAMessageFromContent(jid, {
        newsletterAdminInviteMessage: {
            newsletterJid: inviteResp.id,
            newsletterName: content.newsletter.name,
            jpegThumbnail: content.newsletter.thumbnail,
            caption: content.newsletter.text,
            inviteExpiration: inviteResp.invite_expiration_time
        }
    }, options)
    await conn.relayMessage(jid, fullMsg.message, options)
    return fullMsg
}

return sendNewsletterAdminInvite('xxx@s.whatsapp.net', {
    newsletter: {
        jid: 'xxx@newsletter',
        name: 'Test',
        text: 'Masuk gan',
        thumbnail: Buffer.alloc(0) // customize your own thumbnail 
    }
}, {})

Screenshot_20240919_175258 nooo error huhu

Yeah, because interactiveQuery function is not exported by baileys, so you have to create/export the function yourself