Skip to content

Bots & Scripting Engines

7. Notification Engines & Scripting Bots

This section covers how to connect your code to external communications, send automated emails, transmit SMS text alerts, interact with social media, and handle security data safely.

Tools & Libraries

  • hashlib: A built-in Python library used to scramble sensitive text strings (like passwords) into secure, unreadable cryptographic hashes.
  • smtplib & email.message: Built-in Python modules used together to format rich emails and securely log into remote mail servers to send them.
  • twilio: A powerful third-party enterprise SDK used to dispatch automated text messages, RCS, and WhatsApp notifications directly to physical cell phones.
  • tweepy: An open-source package designed to simplify connecting your scripts directly to the Twitter/X developer API.
  • instagrapi: A feature-rich, high-performance automation wrapper used to interact directly with Instagram's API to publish posts, upload stories, and manage direct messages.

Core Methods & Complete Code Example

  • hashlib.sha1('text'.encode()).hexdigest(): Converts plain text into a permanent 40-character scrambled code. In password security, developers use this to share only the first 5 characters with breach checking tools (k-Anonymity) to check if a password is leaked without revealing the password itself.
  • EmailMessage(): Sets up a digital envelope object where you can attach metadata (Subject, To, From) and main content text or HTML markup.
  • smtplib.SMTP(host, port) & server.starttls(): Establishes a secure pipeline to a mail server using Transport Layer Security (TLS) encryption so authentication details remain confidential.
  • cl.login(user, pass) & cl.photo_upload(path, caption): Authenticates an active Instagram user session and pushes a local image file directly to your live feed with an accompanying caption.
import hashlib
import smtplib
from email.message import EmailMessage
from pathlib import Path
from instagrapi import Client  # Imported for Instagram bot actions

# --- 1. Cryptographic Security (hashlib) ---
# Turns a string into a safe, irreversible 40-character hexadecimal representation
raw_password = "my_secure_password123"
hashed_string = hashlib.sha1(raw_password.encode()).hexdigest().upper()
print(f"SHA-1 Secure Signature: {hashed_string}")


# --- 2. Constructing the Email Structure (email.message & smtplib) ---
# Prepares the data payload and routes it through an external mail server
msg = EmailMessage()
msg['Subject'] = 'System Security Verification Alert'
msg['From'] = 'system-server@domain.com'
msg['To'] = 'admin@domain.com'
msg.set_content('Automated notification baseline text: Verification checks completed.')

with smtplib.SMTP('smtp.gmail.com', 587) as server:
    server.starttls()  # Upgrade the connection to use secure TLS encryption wrappers
    server.login("user@gmail.com", "my-app-password")
    server.send_message(msg)


# --- 3. Social Bot Automation (instagrapi Quick-Look) ---
# Logs into Instagram programmatically, adds human-like delays, and publishes an image
cl = Client()
cl.delay_range = [10, 20]  # Best practice: Add a random delay range to prevent spam blocks

# Run the automation safely by validating the asset exists first
cl.login("my_instagram_username", "my_secret_password")
image_path = Path("alert_graphic.jpg")
if image_path.exists():
    uploaded_post = cl.photo_upload(image_path, "System Update: Status Green! 🚀 #Python #Automation")
    print(f"Post successful! Code: {uploaded_post.code}")


# --- 4. SMS Alternative Option (twilio Quick-Look) ---
##How you would trigger a real-world text notification using the third-party client:
from twilio.rest import Client as TwilioClient
twilio_client = TwilioClient("YOUR_ACCOUNT_SID", "YOUR_AUTH_TOKEN")
twilio_client.messages.create(
    body="System Alert: Verification checks completed.",
    from_="+15551234567",
    to="+15559876543"
)