Automating Notifications and Multimedia with Jenkins: Sending Emails, SMS, YouTube Downloads, and WhatsApp Messages

Ayushmaan Srivastav
3 min readOct 25, 2024

--

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

  1. Jenkins Installed: Ensure Jenkins is up and running with admin access.
  2. Required Plugins: Install necessary plugins in Jenkins for email notifications.
  3. 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).
  4. 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:

  1. Go to Manage JenkinsManage Plugins.
  2. Search and install Email Extension Plugin for email notifications.

Python Libraries:

  1. Ensure Python is installed on the Jenkins server.
  2. 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

  1. In Jenkins, go to Manage JenkinsConfigure System.
  2. 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

  1. In Jenkins, go to New ItemFreestyle Project, name it Automated Notification and Downloads, and click OK.
  2. In the Build Environment section, check Use secret text(s) or file(s) if using credentials.
  3. 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

  1. Click Build Now.
  2. 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

  1. API Errors: Ensure Twilio credentials are accurate.
  2. SMTP Issues: Use app-specific passwords if using Gmail and 2FA is enabled.
  3. 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.

--

--

No responses yet