Automating Notifications and Multimedia with Jenkins: Sending Emails, SMS, YouTube Downloads, and WhatsApp Messages
Jenkins is a powerful automation tool, and beyond CI/CD, it can be configured to perform a variety of tasks like sending notifications via email, SMS, and WhatsApp, or even downloading YouTube videos. In this guide, we’ll set up a Jenkins job that will automate all four actions.
Prerequisites
- Jenkins Installed: Ensure Jenkins is up and running with admin access.
- Required Plugins: Install necessary plugins in Jenkins for email notifications.
- APIs and Accounts: Some of these actions require third-party APIs (e.g., Twilio for SMS and WhatsApp, and Pytube or youtube-dl for YouTube downloads).
- Python Installed: For SMS, WhatsApp, and YouTube downloads, we’ll use Python scripts with relevant packages.
Step 1: Install Required Plugins and Libraries
Jenkins Plugins:
- Go to Manage Jenkins → Manage Plugins.
- Search and install Email Extension Plugin for email notifications.
Python Libraries:
- Ensure Python is installed on the Jenkins server.
- Install the following Python packages:
pip install smtplib twilio pytube
3. If using youtube-dl
, install it as well:
pip install youtube-dl
Step 2: Configure Email Notifications
- In Jenkins, go to Manage Jenkins → Configure System.
- Under the Extended E-mail Notification section, configure SMTP server details:
- SMTP server: e.g.,
smtp.gmail.com
- Default user e-mail suffix: e.g.,
@gmail.com
- SMTP port:
465
for SSL - Authentication: Provide your email and password (or app-specific password for Gmail).
3. Save the configuration.
Step 3: Write Python Scripts for Each Task
1. Send Email Script:
# send_email.py
import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
def send_email():
sender = 'your-email@gmail.com'
receiver = 'receiver-email@gmail.com'
password = 'your-password'
msg = MIMEMultipart()
msg['From'] = sender
msg['To'] = receiver
msg['Subject'] = "Jenkins Notification"
body = "This is an automated email from Jenkins."
msg.attach(MIMEText(body, 'plain'))
try:
with smtplib.SMTP_SSL('smtp.gmail.com', 465) as server:
server.login(sender, password)
server.sendmail(sender, receiver, msg.as_string())
print("Email sent successfully!")
except Exception as e:
print("Error:", e)
send_email()
2. Send SMS via Twilio
Create a script called send_sms.py
:
# send_sms.py
from twilio.rest import Client
def send_sms():
account_sid = 'your_twilio_account_sid'
auth_token = 'your_twilio_auth_token'
client = Client(account_sid, auth_token)
message = client.messages.create(
body="Hello from Jenkins!",
from_='+1234567890', # Twilio number
to='+0987654321' # Recipient's phone number
)
print("SMS sent successfully:", message.sid)
send_sms()
3. Download YouTube Video:
# download_youtube.py
from pytube import YouTube
def download_video():
video_url = 'https://www.youtube.com/watch?v=dQw4w9WgXcQ' # URL of the video
yt = YouTube(video_url)
video = yt.streams.get_highest_resolution()
video.download('/path/to/save')
print("Video downloaded successfully!")
download_video()
4. Send WhatsApp Message via Twilio:
# send_whatsapp.py
from twilio.rest import Client
def send_whatsapp():
account_sid = 'your_twilio_account_sid'
auth_token = 'your_twilio_auth_token'
client = Client(account_sid, auth_token)
message = client.messages.create(
body="Hello from Jenkins on WhatsApp!",
from_='whatsapp:+14155238886', # Twilio's WhatsApp sandbox number
to='whatsapp:+1234567890' # Recipient's WhatsApp number
)
print("WhatsApp message sent:", message.sid)
send_whatsapp()
Step 4: Configure the Jenkins Job
- In Jenkins, go to New Item → Freestyle Project, name it Automated Notification and Downloads, and click OK.
- In the Build Environment section, check Use secret text(s) or file(s) if using credentials.
- Add each script as a Build Step using the Execute Shell option:
- For Email, SMS, WhatsApp, and YouTube, add:
python3 /path/to/send_email.py
python3 /path/to/send_sms.py
python3 /path/to/download_youtube.py
python3 /path/to/send_whatsapp.py
4. Save the job.
Step 5: Run and Monitor the Jenkins Job
- Click Build Now.
- Go to Console Output to monitor the job. Each Python script will execute in sequence:
- Email: Sends a test email.
- SMS: Sends a test SMS.
- WhatsApp: Sends a message on WhatsApp.
- YouTube: Downloads a video from YouTube to the specified path.
Troubleshooting
- API Errors: Ensure Twilio credentials are accurate.
- SMTP Issues: Use app-specific passwords if using Gmail and 2FA is enabled.
- YouTube Download Errors: Check YouTube video availability and verify the download path.
Conclusion
This Jenkins job allows you to perform a variety of multimedia and notification tasks effortlessly. Whether for regular notifications, multimedia downloads, or specialized tasks, Jenkins’ ability to automate via scripts gives it powerful versatility beyond CI/CD.