Skip to content

Instantly share code, notes, and snippets.

@IgorDePaula
Created December 24, 2023 19:57
Show Gist options
  • Save IgorDePaula/adb9c735a5f49b00d7bf4ee965d984d6 to your computer and use it in GitHub Desktop.
Save IgorDePaula/adb9c735a5f49b00d7bf4ee965d984d6 to your computer and use it in GitHub Desktop.
const {DisconnectReason, useMultiFileAuthState} = require("@whiskeysockets/baileys")
const util = require('util')
const makeWASocket = require('@whiskeysockets/baileys').default
async function connectZap(){
const {state, saveCreds} = await useMultiFileAuthState("auth_info_baileys")
const sock = makeWASocket({
printQRInTerminal : true,
auth:state
})
sock.ev.on('connection.update',async (update)=>{
const {connection, lastDisconnect, qr} = update || {}
if(qr){
console.log(util.inspect(qr, false, null, true /* enable colors */))
}
if(connection === 'close'){
const shouldReconnect = lastDisconnect?.error?.output?.statusCode !== DisconnectReason.loggedOut
if(shouldReconnect){
connectZap()
}
}
})
sock.ev.on('messages.update',(message)=>{
console.log('message.update')
console.log(util.inspect(message , false, null, true /* enable colors */))
})
sock.ev.on('messages.upsert',async(message)=>{
console.log('message.upsert')
//console.log(util.inspect(message, false, null, true /* enable colors */))
console.log(util.inspect(message.messages[0].pushName, false, null, true /* enable colors */))
if(message.messages[0].pushName === 'Igor De Paula'){
const id = message.messages[0].key.remoteJid
console.log('entrou')
const buttons = [
{buttonId: 'id1', buttonText: {displayText: 'Button 1'}, type: 1},
{buttonId: 'id2', buttonText: {displayText: 'Button 2'}, type: 1},
{buttonId: 'id3', buttonText: {displayText: 'Button 3'}, type: 1}
]
const buttonMessage = {
image: {url:'./2023-12-05-123936.jpg'},
caption: "Hi it's button message",
footer: 'Hello World',
buttons: buttons,
headerType: 4
}
const sendMsg = await sock.sendMessage(id, buttonMessage)
//send a template message with an image **attached**!
const templateButtons = [
{index: 1, urlButton: {displayText: '⭐ Star Baileys on GitHub!', url: 'https://github.com/adiwajshing/Baileys'}},
{index: 2, callButton: {displayText: 'Call me!', phoneNumber: '+1 (234) 5678-901'}},
{index: 3, quickReplyButton: {displayText: 'This is a reply, just like normal buttons!', id: 'id-like-buttons-message'}},
]
const buttonMessage2 = {
text: "Hi it's a template message",
footer: 'Hello World',
headerType: 4,
templateButtons: templateButtons,
image: {url:'./2023-12-05-123936.jpg'}
}
const sendMsg2 = await sock.sendMessage(id, buttonMessage2)
}
})
sock.ev.on("creds.update", saveCreds)
}
connectZap()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment