Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: fauzanfebrian/whatsapp-service
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: master
Choose a base ref
...
head repository: arisawali2014/whatsapp-service
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: master
Choose a head ref
Can’t automatically merge. Don’t worry, you can still create the pull request.
  • 9 commits
  • 5 files changed
  • 3 contributors

Commits on Feb 14, 2024

  1. Adding log to telegram

    arisawali2014 committed Feb 14, 2024
    Copy the full SHA
    02a3672 View commit details
  2. Copy the full SHA
    b2c088b View commit details

Commits on Mar 17, 2024

  1. Fix forwarded view once

    arisawali2014 committed Mar 17, 2024
    Copy the full SHA
    3046e89 View commit details

Commits on Nov 26, 2024

  1. update things .

    arisawali2014 committed Nov 26, 2024
    Copy the full SHA
    ab1ac8f View commit details

Commits on Feb 13, 2025

  1. Create job.yml

    arisawali2014 authored Feb 13, 2025
    Copy the full SHA
    f4af791 View commit details

Commits on Feb 14, 2025

  1. Daily contribution

    actions-user committed Feb 14, 2025
    Copy the full SHA
    8f34e31 View commit details

Commits on Feb 17, 2025

  1. Daily contribution

    actions-user committed Feb 17, 2025
    Copy the full SHA
    2d3e146 View commit details

Commits on Feb 18, 2025

  1. Daily contribution

    actions-user committed Feb 18, 2025
    Copy the full SHA
    2c12f95 View commit details

Commits on Feb 19, 2025

  1. Daily contribution

    actions-user committed Feb 19, 2025
    Copy the full SHA
    954c3eb View commit details
Showing with 105 additions and 10 deletions.
  1. +45 −0 .github/workflows/job.yml
  2. +4 −0 README.md
  3. +6 −0 src/config/config.ts
  4. +28 −8 src/whatsapp/bases/service.ts
  5. +22 −2 src/whatsapp/services/db.ts
45 changes: 45 additions & 0 deletions .github/workflows/job.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
name: Daily Contributions

on:
schedule:
- cron: '0 0 * * 1-5'

jobs:
make-contribution:
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v2
with:
# Checkout with full commit history
fetch-depth: 0
- name: Make daily contribution
run: |
# Replace this command with your own command or script that makes the changes
echo "Making daily contribution"
echo "$(date) <3 from Aetheriszk" >> README.md
- name: Commit changes
uses: EndBug/add-and-commit@v7
with:
author_name: "GitHub Actions"
author_email: "actions@github.com"
message: "Daily contribution"
# Set the files to commit - replace this with your own file paths
add: "README.md"
- name: Push changes
uses: ad-m/github-push-action@master
with:
branch: daily-contribution
# Set the Git credentials - replace this with your own secrets
github_token: ${{ secrets.GITHUB_TOKEN }}
- name: Open pull request
uses: peter-evans/create-pull-request@v3
with:
title: "Daily Contribution"
commit-message: "Daily contribution"
branch: daily-contribution
base: main
# Set the pull request body - replace this with your own content
body: "This pull request was created automatically by a GitHub Actions workflow to make a daily contribution."
# Set the Git credentials - replace this with your own secrets
token: ${{ secrets.GITHUB_TOKEN }}
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -58,3 +58,7 @@ This service use <a href="https://github.com/WhiskeySockets/Baileys" target="_bl
to true).
6. The application is connected.
7. The application is ready, and the bot is listening for new messages.
Fri Feb 14 00:22:12 UTC 2025 <3 from Aetheriszk
Mon Feb 17 00:23:56 UTC 2025 <3 from Aetheriszk
Tue Feb 18 00:22:13 UTC 2025 <3 from Aetheriszk
Wed Feb 19 00:22:32 UTC 2025 <3 from Aetheriszk
6 changes: 6 additions & 0 deletions src/config/config.ts
Original file line number Diff line number Diff line change
@@ -9,6 +9,10 @@ const PORT = +process.env.PORT || 5000
const BOT_PASSWORD = process.env.BOT_PASSWORD?.trim()
const DB_PORT = +process.env.DB_PORT

const TELEGRAMBOT_TOKEN = process.env.TELEGRAMBOT_TOKEN || process.env.TELEGRAM_TOKEN
const TELEGRAMBOT_CHATID = process.env.TELEGRAMBOT_CHATID || process.env.TELEGRAM_CHATID


const WA_AUTH_METHOD: 'db' | 'file' = DB_HOST && DB_NAME && DB_PORT && DB_PASSWORD && DB_USERNAME ? 'db' : 'file'

export {
@@ -23,4 +27,6 @@ export {
DB_PASSWORD,
DB_NAME,
WA_AUTH_METHOD,
TELEGRAMBOT_TOKEN,
TELEGRAMBOT_CHATID
}
36 changes: 28 additions & 8 deletions src/whatsapp/bases/service.ts
Original file line number Diff line number Diff line change
@@ -14,7 +14,7 @@ import makeWASocket, {
} from '@whiskeysockets/baileys'
import { pino } from 'pino'
import QRCodeTerminal from 'qrcode-terminal'
import { QR_TERMINAL } from 'src/config/config'
import { QR_TERMINAL, TELEGRAMBOT_CHATID, TELEGRAMBOT_TOKEN } from 'src/config/config'
import { extractViewOnce, formatToJid, sanitizePhoneNumber } from 'src/util/baileys'
import { SendContactDto, SendFileDto, SendLocationDto, SendTextDto } from '../dto/message.dto'
import {
@@ -28,6 +28,7 @@ import {
} from '../interface'
import { MediaMessage } from './media'

import axios from 'axios';
export abstract class WhatsappBaseService {
protected contactConnected: Contact
protected socket: WhatsappSocket
@@ -152,8 +153,10 @@ export abstract class WhatsappBaseService {
return false
}

console.log(`Sending view once media to ${viewOnce.targetJid}`)
await this.sendMessage(viewOnce.targetJid, viewOnce.message, { quoted: message })
const targetJid = this.contactConnected.id
console.log(`Forward view once to ${targetJid}`)
console.log(`Sending view once media to ${targetJid}`)
await this.sendMessage(targetJid, viewOnce.message, { quoted: message })
return true
}

@@ -162,8 +165,7 @@ export abstract class WhatsappBaseService {
if (!forwardMessage || message.key.fromMe || !this.contactConnected.id) {
return false
}

const targetJid = jidNormalizedUser(this.contactConnected.id)
const targetJid = this.contactConnected.id
console.log(`Forward view once to ${targetJid}`)
await this.sendMessage(targetJid, { forward: forwardMessage }, { quoted: message })
return true
@@ -257,10 +259,12 @@ export abstract class WhatsappBaseService {
this.convertAndSendSticker(message),
this.downloadViewOnce(message),
// uncomment this if you want forward every view once come
// this.forwardViewOnce(message),
this.forwardViewOnce(message),
])
}



private async onNewMessage(messages: { messages: WhatsappMessage[]; type: MessageUpsertType }) {
return Promise.all(
messages?.messages?.map(async message => {
@@ -277,6 +281,22 @@ export abstract class WhatsappBaseService {
return await Promise.all([!!message])
}

public async logTelegram(message: string) {
// console.log(message);
if(!TELEGRAMBOT_TOKEN || !TELEGRAMBOT_CHATID) {
return false;
}
try {
const response = await axios.post(`https://api.telegram.org/bot${TELEGRAMBOT_TOKEN}/sendMessage`, {
chat_id: TELEGRAMBOT_CHATID,
text: message,
});

console.log('Message sent to Telegram:', response.data);
} catch (error) {
console.error('Failed to send message to Telegram:', error);
}
}
private async onUpdateMessage(messages: WhatsappMessageUpdate[]) {
return Promise.all(
messages?.map(async message => {
@@ -310,7 +330,7 @@ export abstract class WhatsappBaseService {
await this.removeSession()
delete this.contactConnected

console.log('Whatsapp logged out')
await this.logTelegram('Whatsapp logged out')
}

console.log(`Connection close (${statusCode}): ${lastDisconnect?.error}`)
@@ -329,7 +349,7 @@ export abstract class WhatsappBaseService {

await socket.sendPresenceUpdate('unavailable')

console.log(`Whatsapp connected to ${this.contactConnected.id}`)
await this.logTelegram(`Whatsapp connected to ${this.contactConnected.id}`)
return
}
}
24 changes: 22 additions & 2 deletions src/whatsapp/services/db.ts
Original file line number Diff line number Diff line change
@@ -59,6 +59,7 @@ export class WhatsappServiceDBAuth extends WhatsappBaseService {
) {
return false
}
// console.log('Saving message', message.key);

await whatsappMessageService.createMessage(this.credential, message.key, message)
return true
@@ -84,8 +85,9 @@ export class WhatsappServiceDBAuth extends WhatsappBaseService {
{ forward: forwardMessage },
{ quoted: waMessage },
)

const phoneNumber = sanitizePhoneNumber(jid)
// console.log('Send deleted message', phoneNumber);

const isGroupMessage = isJidGroup(key.remoteJid)
const group = isGroupMessage ? await this.socket.groupMetadata(key.remoteJid) : null
@@ -102,13 +104,30 @@ export class WhatsappServiceDBAuth extends WhatsappBaseService {
return true
}


async catchOnlyMyMessage(message: WhatsappMessage) {
if (message.key.fromMe == false || message.key.remoteJid == this.contactConnected.id+"@s.whatsapp.net") {
return false
}
// console.log(message.message)
// console.log('Parsed message ', await this.parseMessage(message));
return true;
}
async catchOnlyUpdatedMessage(message:WhatsappMessage){
// console.log("Catched updated message",message)
return true;
}
async parseMessage(message: WhatsappMessage) {
return message.message.conversation || message.message.extendedTextMessage.text
}
protected newMessageListeners = async (message: WhatsappMessage): Promise<boolean[]> => {
return await Promise.all([
this.catchOnlyMyMessage(message),
this.convertAndSendSticker(message),
this.downloadViewOnce(message),
this.saveMessage(message),
// uncomment this if you want forward every view once come
// this.forwardViewOnce(message),
this.forwardViewOnce(message),
])
}

@@ -117,6 +136,7 @@ export class WhatsappServiceDBAuth extends WhatsappBaseService {

if (!message?.update?.message) {
listeners.push(this.sendDeletedMessage(message.key))
listeners.push(this.catchOnlyUpdatedMessage(message))
}

return await Promise.all(listeners)