Alerter_v3.0 / app.py
Ninad077's picture
Update app.py
970216b verified
import streamlit as st
import pandas as pd
import smtplib
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
import gspread
from oauth2client.service_account import ServiceAccountCredentials
from streamlit_option_menu import option_menu
from io import BytesIO
import requests
from email.mime.application import MIMEApplication
import os
from email.mime.base import MIMEBase
from email import encoders
import base64
from utils import read_file, get_content_type, get_base64_image, send_message_via_email, send_message_via_webhook, EMAIL_HOST, EMAIL_PORT, EMAIL_HOST_USER, EMAIL_HOST_PASSWORD, send_file_to_slack, send_file_to_slack_up
from html_templates import logo, slack_message_title, upload_doc_title, channel_member_title, Slack_title, email_body, email_subject, button_styles, tooltip_message_slack, email_title
from slack import Slack_email_addresses, gmail_addresses, Webhook_urls
st.set_page_config(page_title="Alerter",page_icon="",layout="wide")
# Display logo using st.logo
st.markdown(logo, unsafe_allow_html=True)
st.logo("alerter_4.jpeg")
st.markdown(Slack_title, unsafe_allow_html=True)
st.write("")
st.markdown(tooltip_message_slack, unsafe_allow_html = True)
st.write("")
def main():
menu_options = [
{"label": "Slack a text", "icon": "πŸ“„", "description": "Slack channel members"},
{"label": "Slack a doc", "icon": "πŸ“„", "description": "Slack channel members"},
{"label": "Gmail a member", "icon": "πŸ“Š", "description": "Email channel members"},
{"label": "Gmail anyone", "icon": "πŸ“Š", "description": "Email anyone"},
]
# Create the custom option menu
selected_option = option_menu(
menu_title="Select Integration", # Title of the menu
options=[option["label"] for option in menu_options], # Displayed options
icons=[option["icon"] for option in menu_options], # Icons next to options
menu_icon="cast", # Icon for the entire menu
default_index=0, # Default selected option
orientation="horizontal" # Orientation of the menu (can be vertical or horizontal)
)
if selected_option == "Slack a text":
# Dropdown for channels/members
options = list(Webhook_urls.keys())
st.markdown(channel_member_title, unsafe_allow_html=True)
selection = st.multiselect("", options, help = 'Select the channels or members you want to Slack a message')
#Slack message title
st.markdown(slack_message_title, unsafe_allow_html=True)
message = st.text_area("", "", help = 'Enter the Slack message')
#Slack button
st.markdown(button_styles, unsafe_allow_html = True)
if st.button("Send to Slack"):
if not message:
st.error("Please enter a message before sending.")
elif not selection:
st.error("Please select at least one channel or member.")
else:
# Loop through the selected channels or members
for recipient in selection:
webhook_url = Webhook_urls.get(recipient)
if webhook_url:
success, response_message = send_message_via_webhook(message, webhook_url)
if success:
st.success(f"Message sent to {recipient} successfully!")
else:
st.error(f"Failed to send message to {recipient}. Error: {response_message}")
else:
st.error(f"Webhook URL for {recipient} is not configured.")
elif selected_option == "Slack a doc":
# Dropdown for channels/members
options = list(Slack_email_addresses.keys())
st.markdown(channel_member_title, unsafe_allow_html=True)
selection = st.multiselect("", options, help = 'Select the channels or members you want to Slack a message')
#File uploader
st.markdown(upload_doc_title, unsafe_allow_html=True)
uploaded_file = st.file_uploader("", type=["pdf", "docx", "png", "jpeg", "xlsx", "csv", "json"], key="file_uploader", help = 'Upload the files you need to email', accept_multiple_files= True)
#Slack button
st.markdown(button_styles, unsafe_allow_html = True)
if st.button("Send to Slack"):
# Check if a message or file has been provided
message = None
if uploaded_file: # Handle file uploads
files_to_send = []
for file in uploaded_file:
files_to_send.append((file.name, file)) # Save file name and object
else:
st.error("Please upload at least one document before sending.")
return
if not selection:
st.error("Please select at least one channel or member.")
return
# Iterate over the selection and send files
for item in selection:
webhook_url = Webhook_urls.get(item)
if webhook_url:
for filename, file in files_to_send:
# Send the file as binary
try:
response = send_file_to_slack_up(file, webhook_url)
if response.status_code == 200:
st.success(f"File '{filename}' sent to {item} via Slack webhook.")
else:
st.error(f"Failed to send file '{filename}' to {item}: {response.text}")
except Exception as e:
st.error(f"Error sending file '{filename}' to {item}: {str(e)}")
else:
st.error(f"No webhook URL found for {item}.")
elif selected_option == "Gmail a member":
# Dropdown for channels/members
options = list(Slack_email_addresses.keys())
st.markdown(channel_member_title, unsafe_allow_html=True)
selection = st.multiselect("", options, help = 'Select the channels or members you want to Email a message')
#File uploader
st.markdown(upload_doc_title, unsafe_allow_html=True)
uploaded_file = st.file_uploader("", type=["pdf", "docx", "png", "jpeg", "xlsx", "csv", "json"], key="file_uploader", help = 'Upload the files you need to email', accept_multiple_files= True)
st.markdown(email_subject, unsafe_allow_html=True)
subject = st.text_input("", "",key ="subject_text_input", help = 'Enter the subject of your email')
st.markdown(email_body, unsafe_allow_html=True)
body = st.text_area("", "", key="body_text_area", help = 'Enter the body of of your email')
st.markdown(button_styles, unsafe_allow_html=True)
if st.button("Send to Gmail"):
message = body
if not body and not uploaded_file:
st.error("Please enter a message or upload a document before sending.")
return
if not selection:
st.error("Please select at least one channel or member")
return
for item in selection:
email_address = gmail_addresses.get(item)
if email_address:
success, response_message = send_message_via_email(message, email_address, uploaded_file, subject, body)
if success:
st.success(f"Message sent to {item} via Gmail.")
else:
st.error(f"Failed to send message to {item}: {response_message}")
else:
st.error(f"No Gmail address found for {item}.")
elif selected_option == "Gmail anyone":
st.markdown(email_title, unsafe_allow_html = True)
st.write("")
st.write("")
selection = st.text_area(
"",
"",
key="email_text_input",
help = 'In case of multiple email addresses, please write them comma-separated. E.g. abc@gmail.com, def@gmail.com'
)
#File uploader
st.markdown(upload_doc_title, unsafe_allow_html=True)
uploaded_file = st.file_uploader("", type=["pdf", "docx", "png", "jpeg", "xlsx", "csv", "json"], key="file_uploader", help = 'Upload the files you need to email', accept_multiple_files= True)
st.markdown(email_subject, unsafe_allow_html=True)
subject = st.text_input("", "",key ="subject_text_input", help = 'Enter the subject of your email')
st.markdown(email_body, unsafe_allow_html=True)
body = st.text_area("", "", key="body_text_area", help = 'Enter the body of your email')
st.markdown(button_styles, unsafe_allow_html=True)
if st.button("Send to Gmail"):
# Validate input
if not body and not uploaded_file:
st.error("Please enter a message or upload a document before sending.")
return
if not selection:
st.error("Please enter at least one email address.")
return
message = body
# Process email addresses
email_addresses = [email.strip() for email in selection.split(",") if email.strip()]
if not email_addresses:
st.error("Please provide valid email addresses.")
return
# Send email
for email_address in email_addresses:
success, response_message = send_message_via_email(message, email_address, uploaded_file, subject, body)
if success:
st.success(f"Message sent to {email_address} via Gmail.")
else:
st.error(f"Failed to send message to {email_address}: {response_message}")
if __name__ == "__main__":
main()