Blog>
Snippets

Creating a Job Alert System with Node.js and Twilio

Set up a Node.js application that uses Twilio's API to send SMS job alerts based on custom search criteria, helping developers find high-paying job opportunities quickly.
const http = require('http');
const express = require('express');
const bodyParser = require('body-parser');
const Twilio = require('twilio');

const twilioAccountSid = 'YOUR_TWILIO_ACCOUNT_SID'; // Replace with your Twilio account SID
const twilioAuthToken = 'YOUR_TWILIO_AUTH_TOKEN'; // Replace with your Twilio auth token
const twilioPhoneNumber = 'YOUR_TWILIO_PHONE_NUMBER'; // Replace with your Twilio phone number

const app = express();
app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());

const twilioClient = new Twilio(twilioAccountSid, twilioAuthToken);

app.post('/sendJobAlert', (req, res) => {
    const { phoneNumber, message } = req.body;

    twilioClient.messages
        .create({
            body: message,
            from: twilioPhoneNumber,
            to: phoneNumber
        })
        .then((message) => {
            console.log('Job alert sent:', message.sid);
            res.send('Job alert sent successfully.');
        })
        .catch((error) => {
            console.error('Error sending job alert:', error);
            res.status(500).send('Error sending job alert.');
        });
});

const server = http.createServer(app);
const PORT = process.env.PORT || 3000;
server.listen(PORT, () => {
    console.log(`Server is running on port ${PORT}`);
});
This Node.js code sets up an Express server and initializes the Twilio client with Twilio credentials. It then defines an endpoint '/sendJobAlert' that takes a phone number and a message from the request body to send an SMS alert using Twilio's API. Finally, it starts the server on a specified port.
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Job Alert System</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            padding: 20px;
        }
        form {
            margin-bottom: 20px;
        }
        input[type="text"], input[type="tel"] {
            padding: 10px;
            margin: 10px 0;
            width: 100%;
            box-sizing: border-box;
        }
        input[type="submit"] {
            padding: 10px 20px;
            background-color: #007bff;
            color: white;
            border: none;
            cursor: pointer;
        }
    </style>
</head>
<body>
    <form id="alertForm">
        <label for="phoneNumber">Phone Number:</label>
        <input type="tel" id="phoneNumber" name="phoneNumber" placeholder="+1234567890" required>
        <label for="message">Job Alert Message:</label>
        <input type="text" id="message" name="message" placeholder="Enter your custom job alert message" required>
        <input type="submit" value="Send Job Alert">
    </form>
    <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
    <script>
        $(document).ready(function () {
            $('#alertForm').submit(function (event) {
                event.preventDefault();
                const phoneNumber = $('#phoneNumber').val();
                const message = $('#message').val();
                $.post('/sendJobAlert', { phoneNumber, message }, function (data) {
                    alert('Job alert sent!');
                }).fail(function () {
                    alert('Error sending job alert.');
                });
            });
        });
    </script>
</body>
</html>
This HTML code provides a basic form to input a phone number and a custom message for the job alert. It uses jQuery to handle the form submission and make an AJAX POST request to the '/sendJobAlert' endpoint when the form is submitted. The CSS styles simple form elements for better user experience.