你好,我正在尝试使用 GitHub 操作在 nodejs 中运行 cron,它将向我的团队发送生日提醒。
我有两个文件,运行发送消息的脚本和 csv 格式的联系人。 这是我的工作流程文件,位于 .github/workflows/birthday-messages-cron.yml
name: Birthday Messages Cron
on:
schedule:
- cron: "*/5 * * * *"
jobs:
send_birthday_messages:
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v2
- name: Set up Node.js
uses: actions/setup-node@v3
with:
node-version: 16
- name: Install dependencies
run: npm ci
- name: Run birthday message sender
run: node birthday-message-sender.js
- name: Send email notification
if: ${{ always() }}
uses: dawidd6/action-send-mail@v3
with:
server_address: smtp.gmail.com
server_port: 465
username: ${{ secrets.EMAIL_USERNAME }}
password: ${{ secrets.EMAIL_PASSWORD }}
to: [email protected]
subject: Birthday Messages Cron Status
body: |
Workflow Run: ${{ github.sha }}
Status: ${{ job.status }}
Error: ${{ job.steps[3].outcome == 'failure' }}
- name: Set environment variables
run: |
echo "BULKSMSNIGERIA_API_KEY=${{ secrets.BULKSMSNIGERIA_API_KEY }}" >> $GITHUB_ENV
- name: Check exit code
if: ${{ always() }}
run: exit ${{ job.status == 'success' && steps.send_email.outcome == 'success' ? 0 : 1 }}
这是我的 birthday-message-sender.js 文件
const axios = require('axios');
const csv = require('csv-parser');
const fs = require('fs');
// Function to send SMS
async function sendSMS(name, phoneNumber, message) {
try {
const response = await axios.post('https://www.bulksmsnigeria.com/api/v1/sms/create', {
api_token: process.env.BULKSMSNIGERIA_API_KEY,
from: "RCCG SRP",
to: phoneNumber,
body: message,
});
if (response.data.status === 'success') {
console.log(`SMS sent to ${name} at ${phoneNumber} successfully.`);
return true;
} else {
throw new Error(`Failed to send SMS to ${name} at ${phoneNumber}. Reason: ${response.data.message}`);
}
} catch (error) {
console.error(`Failed to send SMS to ${name} at ${phoneNumber}. Error: ${error.message}`);
return false;
}
}
// Get current date
const currentDate = new Date();
const currentMonth = currentDate.getMonth() + 1; // Month is 0-indexed, so adding 1
const currentDay = currentDate.getDate();
// Read contacts from CSV file
fs.createReadStream('contacts.csv')
.pipe(csv())
.on('data', async (data) => {
// Extract contact details
const name = data.name;
const phoneNumber = data.phone_number;
const birthdate = new Date(data.birthday); // Assuming birthday is in the format 'YYYY-MM-DD'
const birthMonth = birthdate.getMonth() + 1; // Month is 0-indexed, so adding 1
const birthDay = birthdate.getDate();
// Check if birthday matches current date
if (birthMonth === currentMonth && birthDay === currentDay) {
// Construct birthday message with line breaks
const message = `Happy birthday, ${name}!\nMay this lovely day bring happiness and new opportunities in your life.\nWishing you the happiest birthday ever!\n\nRCCG - Solid Rock Parish`;
// Send birthday message
const result = await sendSMS(name, phoneNumber, message);
// Log result
console.log(`SMS send result: ${result}`);
}
})
.on('end', () => {
console.log('Finished sending birthday messages.');
});
拜托,我对 GitHub 操作或设置工作流文件了解不多。
我做了cron
schedule:
- cron: "*/5 * * * *"
用于测试目的 cron 预计每天运行 我需要你的帮助。 谢谢