استلام تعليقات GitLab على WhatsApp باستخدام Webhooks

التواصل الفعال ضروري لإدارة المشاريع الناجحة، خاصة عند التعامل مع عملاء متعددين ومشاريع معقدة. سيوضح لك هذا الدليل كيفية استخدام webhooks الخاصة بـ GitLab لمركزة التواصل وتحسين أوقات الاستجابة من خلال التكامل مع WhatsApp.
المقدمة
في هذا الدليل، سنمر عبر الخطوات لإعداد نظام يستمع لأحداث GitLab webhook ويرسل إشعارات إلى WhatsApp باستخدام Node.js ومكتبة whatsapp-web.js. يمكن لهذا النظام مساعدتك في تتبع أنشطة GitLab المهمة وضمان الاستجابات في الوقت المناسب.
المتطلبات الأساسية
لمتابعة هذا الدليل، ستحتاج إلى:
- حساب GitLab
- Node.js و npm مثبتين
- حساب WhatsApp
- معرفة أساسية بـ JavaScript
إعداد المشروع
أولاً، أنشئ مجلداً جديداً لمشروعك وهيئ تطبيق Node.js جديد:
mkdir whatsapp-gitlab-integration
cd whatsapp-gitlab-integration
npm init -yبعد ذلك، ثبّت التبعيات المطلوبة:
npm install whatsapp-web.js qrcode-terminal express body-parser puppeteer ai @ai-sdk/openaiكود المشروع
أنشئ ملفاً جديداً باسم index.js وأضف الكود التالي:
const { Client, LocalAuth } = require('whatsapp-web.js');
const qrcode = require('qrcode-terminal');
const express = require('express');
const bodyParser = require('body-parser');
const puppeteer = require('puppeteer');
const { generateText } = require('ai');
const { openai } = require('@ai-sdk/openai');
const HANDLE = 'marrouchi';
const PHONE_NUMBER = '21624309128@c.us';
// Personalize the contacts and companies arrays with your own information
const contacts = [
{ phoneNumber: PHONE_NUMBER, name: 'Anis Marrouchi', company: 'NOQTA', role: 'Founder' }
// Add more contacts as needed
];
const companies = [
{
name: 'NOQTA',
description: 'NOQTA is a web agency AI centered, based in Tunisia. We provide web & mobile development, AI & Automation, Cybersecurity, Consulting, IT Maintenance & support.',
projects: [
`Let's us build your next project together.`,
],
latestInteraction: ''
}
];
const app = express();
app.use(bodyParser.json());
const client = new Client({
authStrategy: new LocalAuth(),
puppeteer: {
executablePath: puppeteer.executablePath(),
args: ['--no-sandbox', '--disable-setuid-sandbox'],
timeout: 60000 // Increase timeout to 60 seconds
}
});
client.on('qr', (qr) => {
console.log('QR event received, generating QR code...');
qrcode.generate(qr, { small: true });
console.log('QR code generated, scan it using WhatsApp');
});
client.on('ready', () => {
console.log('WhatsApp client is ready!');
});
client.on('message', async (msg) => {
console.log('Message received:', msg.body);
const contact = contacts.find(contact => contact.phoneNumber === msg.from);
if (contact) {
const company = companies.find(company => company.name === contact.company);
const description = company ? company.description : '';
const projects = company ? company.projects.join(', ') : '';
const latestInteraction = company ? company.latestInteraction : '';
try {
// Generate a response using Vercel AI SDK
const { text } = await generateText({
model: openai('gpt-4o'),
system: `You are an AI assistant for a web agency...`,
prompt: `Company: ${contact.company}\nDescription: ${description}\nProjects: ${projects}\nLatest Interaction: ${latestInteraction}\nMessage: ${msg.body}`
});
msg.reply(text);
} catch (error) {
console.error('Error generating response:', error);
msg.reply('Sorry, I encountered an error while generating a response.');
}
}
});
client.initialize();
// Endpoint to handle GitLab webhooks
app.post('/webhook', (req, res) => {
const mentionEvent = req.body;
const mentionText = mentionEvent.object_attributes.note;
const mentionedUser = mentionEvent.user.name;
if (mentionText.includes(HANDLE)) {
const message = `You were mentioned in a GitLab note: "${mentionText}" \n ${mentionEvent.object_attributes.url}`;
client.sendMessage(PHONE_NUMBER, message)
.then(response => {
console.log('Message sent:', response);
res.status(200).send('Message sent successfully');
})
.catch(err => {
console.error('Failed to send message:', err);
res.status(500).send('Failed to send message');
});
}
});
const PORT = process.env.PORT || 3000;
app.listen(PORT, () => {
console.log(`Server running on port ${PORT}`);
});كشف خادم محلي باستخدام ngrok
لاختبار webhook محلياً، يمكنك استخدام ngrok لكشف خادمك المحلي على الإنترنت. اتبع هذه الخطوات:
-
تثبيت ngrok:
npm install -g ngrok -
تشغيل خادمك المحلي:
node index.js -
كشف خادمك المحلي:
ngrok http 3000 -
الحصول على الرابط العام: سيوفر ngrok رابطاً عاماً يمكنك استخدامه لتهيئة GitLab webhook.
-
تهيئة GitLab Webhook:
- اذهب إلى مشروع GitLab الخاص بك.
- انتقل إلى Settings > Webhooks.
- أضف رابط ngrok كرابط webhook.
- اختر الأحداث التي تريد تفعيل webhook لها.
تشغيل خادم عام مع PM2
لإعداد الإنتاج، يمكنك استخدام PM2 لتشغيل خادمك باستمرار وإدارته بكفاءة:
-
تثبيت PM2:
npm install -g pm2 -
تشغيل الخادم مع PM2:
pm2 start index.js --name whatsapp-gitlab-integration -
مراقبة الخادم:
pm2 monit -
إعداد PM2 لإعادة التشغيل عند إعادة تشغيل الخادم:
pm2 startup pm2 save
الخلاصة
من خلال دمج webhooks الخاصة بـ GitLab مع WhatsApp، يمكنك تبسيط التواصل وضمان عدم تفويت التحديثات والإشارات المهمة أبداً. يسمح هذا الإعداد بالإشعارات والاستجابات في الوقت الفعلي، مما يحسن الكفاءة العامة واستجابة سير عمل إدارة مشروعك.
لمزيد من التفاصيل، يمكنك العثور على المشروع الكامل على GitHub: مستودع GitHub
ناقش مشروعك معنا
نحن هنا للمساعدة في احتياجات تطوير الويب الخاصة بك. حدد موعدًا لمناقشة مشروعك وكيف يمكننا مساعدتك.
دعنا نجد أفضل الحلول لاحتياجاتك.
مقالات ذات صلة

تحسين التواصل في GitLab باستخدام Webhooks
تعلم كيفية استخدام webhooks في GitLab لمركزة التواصل وتحسين أوقات الاستجابة. يتضمن أمثلة كود في Laravel و Next.js.

تحسين التواصل في GitLab باستخدام Webhooks
تعرف على كيفية استخدام Webhooks في GitLab لتوحيد التواصل وتحسين أوقات الاستجابة. يتضمن أمثلة على الأكواد في Laravel و Next.js.

الحساب البنكي المجاني للمبادرين الذاتيين والمحترفين مع فُلوسي
اكتشف كيف يوفر حساب فُلوسي الاحترافي المجاني حلاً مصرفيًا رقميًا شاملاً للمبادرين الذاتيين وأصحاب المهن الحرة في تونس، مع ميزات مصممة لتبسيط الإدارة المالية.