-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
5429368
commit b22cd1f
Showing
110 changed files
with
127,072 additions
and
84 deletions.
There are no files selected for viewing
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
# Ignore artifacts: | ||
lib | ||
coverage | ||
*.lock | ||
.eslintrc.json | ||
src/WABinary/index.ts | ||
WAProto |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{ | ||
"extends": "@adiwajshing" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
# Auto detect text files and perform LF normalization | ||
* text=auto |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
node_modules | ||
auth_info*.json | ||
baileys_auth_info* | ||
baileys_store*.json | ||
output.csv | ||
*/.DS_Store | ||
.DS_Store | ||
.env | ||
browser-messages.json | ||
decoded-ws.json | ||
lib | ||
docs | ||
browser-token.json | ||
Proxy | ||
messages*.json | ||
test.ts | ||
TestData |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,169 @@ | ||
import { Boom } from '@hapi/boom' | ||
import makeWASocket, { AnyMessageContent, delay, DisconnectReason, fetchLatestBaileysVersion, isJidBroadcast, makeCacheableSignalKeyStore, makeInMemoryStore, MessageRetryMap, useMultiFileAuthState } from '../src' | ||
import MAIN_LOGGER from '../src/Utils/logger' | ||
|
||
const logger = MAIN_LOGGER.child({ }) | ||
logger.level = 'trace' | ||
|
||
const useStore = !process.argv.includes('--no-store') | ||
const doReplies = !process.argv.includes('--no-reply') | ||
|
||
// external map to store retry counts of messages when decryption/encryption fails | ||
// keep this out of the socket itself, so as to prevent a message decryption/encryption loop across socket restarts | ||
const msgRetryCounterMap: MessageRetryMap = { } | ||
|
||
// the store maintains the data of the WA connection in memory | ||
// can be written out to a file & read from it | ||
const store = useStore ? makeInMemoryStore({ logger }) : undefined | ||
store?.readFromFile('./baileys_store_multi.json') | ||
// save every 10s | ||
setInterval(() => { | ||
store?.writeToFile('./baileys_store_multi.json') | ||
}, 10_000) | ||
|
||
// start a connection | ||
const startSock = async() => { | ||
const { state, saveCreds } = await useMultiFileAuthState('baileys_auth_info') | ||
// fetch latest version of WA Web | ||
const { version, isLatest } = await fetchLatestBaileysVersion() | ||
console.log(`using WA v${version.join('.')}, isLatest: ${isLatest}`) | ||
|
||
const sock = makeWASocket({ | ||
version, | ||
logger, | ||
printQRInTerminal: true, | ||
auth: { | ||
creds: state.creds, | ||
/** caching makes the store faster to send/recv messages */ | ||
keys: makeCacheableSignalKeyStore(state.keys, logger), | ||
}, | ||
msgRetryCounterMap, | ||
generateHighQualityLinkPreview: true, | ||
// ignore all broadcast messages -- to receive the same | ||
// comment the line below out | ||
shouldIgnoreJid: jid => isJidBroadcast(jid), | ||
// implement to handle retries | ||
getMessage: async key => { | ||
if(store) { | ||
const msg = await store.loadMessage(key.remoteJid!, key.id!) | ||
return msg?.message || undefined | ||
} | ||
|
||
// only if store is present | ||
return { | ||
conversation: 'hello' | ||
} | ||
} | ||
}) | ||
|
||
store?.bind(sock.ev) | ||
|
||
const sendMessageWTyping = async(msg: AnyMessageContent, jid: string) => { | ||
await sock.presenceSubscribe(jid) | ||
await delay(500) | ||
|
||
await sock.sendPresenceUpdate('composing', jid) | ||
await delay(2000) | ||
|
||
await sock.sendPresenceUpdate('paused', jid) | ||
|
||
await sock.sendMessage(jid, msg) | ||
} | ||
|
||
// the process function lets you process all events that just occurred | ||
// efficiently in a batch | ||
sock.ev.process( | ||
// events is a map for event name => event data | ||
async(events) => { | ||
// something about the connection changed | ||
// maybe it closed, or we received all offline message or connection opened | ||
if(events['connection.update']) { | ||
const update = events['connection.update'] | ||
const { connection, lastDisconnect } = update | ||
if(connection === 'close') { | ||
// reconnect if not logged out | ||
if((lastDisconnect?.error as Boom)?.output?.statusCode !== DisconnectReason.loggedOut) { | ||
startSock() | ||
} else { | ||
console.log('Connection closed. You are logged out.') | ||
} | ||
} | ||
|
||
console.log('connection update', update) | ||
} | ||
|
||
// credentials updated -- save them | ||
if(events['creds.update']) { | ||
await saveCreds() | ||
} | ||
|
||
if(events.call) { | ||
console.log('recv call event', events.call) | ||
} | ||
|
||
// history received | ||
if(events['messaging-history.set']) { | ||
const { chats, contacts, messages, isLatest } = events['messaging-history.set'] | ||
console.log(`recv ${chats.length} chats, ${contacts.length} contacts, ${messages.length} msgs (is latest: ${isLatest})`) | ||
} | ||
|
||
// received a new message | ||
if(events['messages.upsert']) { | ||
const upsert = events['messages.upsert'] | ||
console.log('recv messages ', JSON.stringify(upsert, undefined, 2)) | ||
|
||
if(upsert.type === 'notify') { | ||
for(const msg of upsert.messages) { | ||
if(!msg.key.fromMe && doReplies) { | ||
console.log('replying to', msg.key.remoteJid) | ||
await sock!.readMessages([msg.key]) | ||
await sendMessageWTyping({ text: 'Hello there!' }, msg.key.remoteJid!) | ||
} | ||
} | ||
} | ||
} | ||
|
||
// messages updated like status delivered, message deleted etc. | ||
if(events['messages.update']) { | ||
console.log(events['messages.update']) | ||
} | ||
|
||
if(events['message-receipt.update']) { | ||
console.log(events['message-receipt.update']) | ||
} | ||
|
||
if(events['messages.reaction']) { | ||
console.log(events['messages.reaction']) | ||
} | ||
|
||
if(events['presence.update']) { | ||
console.log(events['presence.update']) | ||
} | ||
|
||
if(events['chats.update']) { | ||
console.log(events['chats.update']) | ||
} | ||
|
||
if(events['contacts.update']) { | ||
for(const contact of events['contacts.update']) { | ||
if(typeof contact.imgUrl !== 'undefined') { | ||
const newUrl = contact.imgUrl === null | ||
? null | ||
: await sock!.profilePictureUrl(contact.id!).catch(() => null) | ||
console.log( | ||
`contact ${contact.id} has a new profile pic: ${newUrl}`, | ||
) | ||
} | ||
} | ||
} | ||
|
||
if(events['chats.delete']) { | ||
console.log('chats deleted ', events['chats.delete']) | ||
} | ||
} | ||
) | ||
|
||
return sock | ||
} | ||
|
||
startSock() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
received_* | ||
media_* |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file not shown.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file not shown.
Binary file not shown.
Oops, something went wrong.