Skip to content

Instantly share code, notes, and snippets.

@smashah
Last active June 18, 2020 18:25
Show Gist options
  • Save smashah/c663aaf8ed25459d042dae0c067b3b86 to your computer and use it in GitHub Desktop.
Save smashah/c663aaf8ed25459d042dae0c067b3b86 to your computer and use it in GitHub Desktop.
Easily get started with wa-automate
import {
create,
Client,
decryptMedia,
ev,
smartUserAgent
} from "@open-wa/wa-automate";
const mime = require("mime-types");
const fs = require("fs");
const ON_DEATH = require("death");
let globalClient: Client;
ON_DEATH(async function(signal, err) {
console.log("killing session");
if (globalClient) await globalClient.kill();
});
ev.on("qr.**", async (qrcode, sessionId) => {
//base64 encoded qr code image, writes the qr code to file. Updates with new qr codes.
const imageBuffer = Buffer.from(
qrcode.replace("data:image/png;base64,", ""),
"base64"
);
fs.writeFileSync(
`qr_code${sessionId ? "_" + sessionId : ""}.png`,
imageBuffer
);
});
ev.on("**", async (data, sessionId, namespace) => {
console.log("\n----------");
console.log("EV", data, sessionId, namespace);
console.log("----------");
});
ev.on("sessionData.**", async (sessionData, sessionId) => {
console.log("\n----------");
console.log("sessionData", sessionId, sessionData);
console.log("----------");
});
async function start(client: Client) {
globalClient = client;
console.log("starting");
const me = await client.getMe();
// const chats = await client.getAllChatsWithMessages(false);
// const newMessages = await client.getAllUnreadMessages();
// client.onAck((c:any) => console.log(c.id._serialized,c.body,c.ack));
client.onAddedToGroup(newGroup =>
console.log("Added to new Group", newGroup.id)
);
client.onIncomingCall(call => console.log("newcall", call));
// client.onParticipantsChanged("XXXXXXXX-YYYYYYYY@g.us", (participantChangedEvent:any) => console.log("participant changed for group", participantChangedEvent));
//Returns 'CONNECTED' or 'TIMEOUT' or 'CONFLICT' (if user opens whatsapp web somewhere else)
client.onStateChanged(state => {
console.log("statechanged", state);
if (state === "CONFLICT") client.forceRefocus();
});
// setTimeout(_=> client.kill(), 3000);
// const allmsgs = await client.loadAndGetAllMessagesInChat('XXXXXXXX-YYYYYYYY@g.us",true,false);
// console.log("TCL: start -> allMessages", allmsgs.length);
client.onAnyMessage(message => console.log(message.type));
// client.onParticipantsChanged("XXXXXXXXXX-YYYYYYYYY@g.us",x=>console.log(x))
client.onMessage(async message => {
try {
const isConnected = await client.isConnected();
console.log("TCL: start -> isConnected", isConnected);
console.log(message.body, message.id, message?.quotedMsgObj?.id);
if (message.mimetype) {
const filename = `${message.t}.${mime.extension(message.mimetype)}`;
const mediaData = await decryptMedia(message);
// you can send a file also with sendImage or await client.sendFile
// await client.sendImage(
// message.from,
// `data:${message.mimetype};base64,${mediaData.toString('base64')}`,
// filename,
// `You just sent me this ${message.type}`
// );
//send the whole data URI so the mimetype can be checked.
await client.sendImageAsSticker(
message.from,
`data:${message.mimetype};base64,${mediaData.toString("base64")}`
);
//get this numbers products
// const products = await client.getBusinessProfilesProducts(message.to);
// //send a product from this number to that number
// await client.sendImageWithProduct(
// `data:${message.mimetype};base64,${mediaData.toString('base64')}`,
// message.from,
// 'check out this product',
// message.to,
// products[0].id)
// await client.forwardMessages(message.from,message,false);
await client.forwardMessages(message.from, message, false);
fs.writeFile(filename, mediaData, function(err) {
if (err) {
return console.log(err);
}
console.log("The file was saved!");
});
} else if (message.type === "location") {
console.log(
"TCL: location -> message",
message.lat,
message.lng,
message.loc
);
await client.sendLocation(
message.from,
`${message.lat}`,
`${message.lng}`,
`Youre are at ${message.loc}`
);
} else {
// var sentMessageId = await client.sendText(message.from, message.body);
// console.log("start -> sentMessageId", sentMessageId)
// //send a giphy gif
// await client.forwardMessages(message.from,message,false);
// await client.sendGiphy(message.from,'https://media.giphy.com/media/oYtVHSxngR3lC/giphy.gif','Oh my god it works');
// console.log("TCL: start -> message.from,message.body,message.id.toString()", message.from,message.body,message.id.toString())
// await client.reply(message.from,message.body,message);
}
} catch (error) {
console.log("TCL: start -> error", error);
}
});
// const groupCreationEvent = await client.createGroup('coolnewgroup','0000000000@c.us');
// console.log("start -> groupCreationEvent", groupCreationEvent)
//wait a few seconds and make a group
}
//you can now create two sessions pointing
//two the same message handler
/**
* it can be null, which will default to 'session' folder.
* You can also override some puppeteer configs, set an executable path for your instance of chrome for ffmpeg (video+GIF) support
* and you can AND SHOULD override the user agent.
*/
create({
//this will save a session1.data.json file, which you can then inject back using sessionData (https://open-wa.github.io/wa-automate-nodejs/interfaces/configobject.html)
sessionId: "session1",
// executablePath: '/Applications/Google Chrome.app/Contents/MacOS/Google Chrome',
useChrome: true,
restartOnCrash: start,
headless: false,
throwErrorOnTosBlock: true,
killTimer: 40,
autoRefresh: true, //default to true
qrRefreshS: 15 //please note that if this is too long then your qr code scan may end up being invalid. Generally qr codes expire every 15 seconds.
// cacheEnabled:false,
// devtools:true,
// blockCrashLogs:true,
//OR
// devtools:{
// user:'admin',
// pass:'root'
// },
//extra chromium arguments
// chromiumArgs:[
// '--aggressive-cache-discard',
// '--disable-cache',
// '--disable-application-cache',
// '--disable-offline-load-stale-cache',
// '--disk-cache-size=0'
// '--no-sandbox'
// ]
})
// create()
.then(async client => await start(client))
.catch(e => {
console.log("Error", e.message);
// process.exit();
});
//or you can set a 'session id'
// create('newsession').then(client => start(client));
//DO NOT HAVE TO SESSIONS WITH THE SAME ID
//BE WARNED, SETTING THIS UP WITH 2 NUMBERS WILL RESULT IN AN ECHO CHAMBER
//IF YOU SEND AN IMAGE WITH ONE PHONE IT WILL PING PONG THAT IMAGE FOR ETERNITY
/** Amazing features exclusively in open-wa:
* 1. Reduced memory consumption (There is no need for a sessions folder)
* 2. Easily migrate sessions. You can inject sessionData! https://open-wa.github.io/wa-automate-nodejs/interfaces/configobject.html
* 3. Premium features like automating publishing stories
* 4. Constantly updating (multiple releases per week)
* 5. Ecosystem which includes open-wa-python, open-wa-avd-docker, open-wa-decrypt-nodejs
* 6. Amazing discord community
*/
GOTO: https://github.com/open-wa
@EdoWahdana
Copy link

How can i join the discord community?

@smashah
Copy link
Author

smashah commented May 28, 2020

@EdoWahdana click the discord badge on the repo https://github.com/open-wa/wa-automate-nodejs

@smashah
Copy link
Author

smashah commented Jun 15, 2020

@smashah
Copy link
Author

smashah commented Jun 18, 2020

@smashah
Copy link
Author

smashah commented Jun 18, 2020

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment