Reputation: 21
I built an automated WhatsApp birthday wishing bot using whatsapp-web.js that works perfectly on my local machine. The bot is scheduled to check a database at 8 AM every day for birthdays and sends personalized messages. However, I want to make the bot run automatically on the internet, every day at 8 AM, without requiring my manual intervention, since it's a small school project and I can't afford to pay for deploying/hosting.
GitHub Actions: I set up a workflow to run the bot, and although it triggers the bot daily, it always re-authenticates the WhatsApp session. This defeats the purpose of automation, as I have to re-scan the QR code each time. I also tried caching the authentication session using actions/cache@v3
, but it still doesn’t persist properly between runs.
Sometimes, the bot says it successfully sent a message, but nothing appears on my mobile device. At other times, it only sends messages to one person in the group instead of all the intended recipients. This doesn't happen when I run the bot locally, where it works flawlessly.
GitHub Codespace: I also tried using the codespace but I don't know if it is that efficent for this project.
Is there a free platform or hosting service where I can run my bot daily (e.g., using a cron job) and persist the WhatsApp authentication session between runs without having to manually scan the QR code every day?
name: Birthday Bot
on:
schedule:
\- cron: '0 8 \* \* \*' # Runs daily at 8 AM
jobs:
run-bot:
runs-on: ubuntu-latest
steps:
- name: Checkout repository
uses: actions/checkout@v2
- name: Set up Node.js
uses: actions/setup-node@v2
with:
node-version: '18'
- name: Restore WhatsApp auth cache
uses: actions/cache@v3
with:
path: .wwebjs_auth
key: whatsapp-auth-${{ github.sha }}
restore-keys: |
whatsapp-auth-
- name: Install dependencies
run: npm install
- name: Run the bot
run: node bot.js
env:
AIRTABLE_API_KEY: ${{ secrets.AIRTABLE_API_KEY }}
GROUP_CHAT_ID: ${{ secrets.GROUP_CHAT_ID }}
ADMIN_NUMBER: ${{ secrets.ADMIN_NUMBER }}
- name: Save WhatsApp auth cache
uses: actions/cache@v3
with:
path: .wwebjs_auth
key: whatsapp-auth-${{ github.run_id }}
- name: Check auth cache directory
run: ls -la .wwebjs_auth || echo "No .wwebjs_auth found"`
require('dotenv').config();
const qrcode = require('qrcode-terminal');
const { Client, LocalAuth, MessageMedia } = require('whatsapp-web.js');
const client = new Client({
authStrategy: new LocalAuth()
});
client.on('qr', (qr) =\> {
qrcode.generate(qr, { small: true });
});
client.on('ready', () =\> {
console.log('Client is ready!');
sendBirthdayMessages();
});
client.on('disconnected', (reason) =\> {
console.log('Bot was disconnected. Reason:', reason);
client.sendMessage(adminNumber, `The bot was disconnected. Reason: ${reason}`);
});
client.initialize();
const birthdayMessages = require('./birthdayMessages.js');
const adminNumber = process.env.ADMIN_NUMBER;
// Function to send birthday messages
async function sendBirthdayMessages() {
// Fetching records from Airtable, logic for checking birthdays and sending WhatsApp messages.
}
Upvotes: 2
Views: 131