Drop files here
or click to upload
# Import the required libraries for creating the Streamlit app
import streamlit as st
import requests
import json
# Set the title and icon for the Streamlit page
st.set_page_config(page_title="CRM Chatbot", page_icon="🤖")
# --- API Configuration ---
# Define the API endpoint URL and the authorization token
API_ENDPOINT = "https://emea.snaplogic.com/api/1/rest/slsched/feed/ConnectFasterInc/snapLogic4snapLogic/Bootcamp_EMEA_August_2025/CRM_Retriever_Tom%20Task"
AUTH_TOKEN = "12345"
# --- Main App Title and Header ---
st.title("🤖 CRM Retriever Chatbot")
st.header("Ask me questions about CRM opportunities.")
# --- Session State Management ---
# Initialize chat history in Streamlit's session state if it doesn't exist.
# Session state is a dictionary-like object that persists across reruns.
if "messages" not in st.session_state:
st.session_state.messages = []
# --- Function to call the API ---
def get_api_response(question):
"""
Sends a question to the Snaplogic API and returns the response.
Args:
question (str): The user's question.
Returns:
str: The response from the API, or an error message if the call fails.
"""
try:
# Create the headers for the API request, including the authorization token.
headers = {
"Authorization": f"Bearer {AUTH_TOKEN}",
"Content-Type": "application/json"
}
# Create the JSON payload with the user's question.
# The API expects a list containing a dictionary with the key "Question".
payload = [{"Question": question}]
# Make the POST request to the API.
response = requests.post(API_ENDPOINT, headers=headers, data=json.dumps(payload))
# Raise an exception for bad status codes (4xx or 5xx).
response.raise_for_status()
# Parse the JSON response.
response_data = response.json()
# The API returns a list of dictionaries. We need the "Response" value.
if response_data and isinstance(response_data, list) and "Response" in response_data[0]:
return response_data[0]["Response"]
else:
return "Error: Unexpected API response format."
except requests.exceptions.RequestException as e:
# Catch network or HTTP-related errors and provide a user-friendly message.
return f"Error connecting to the API: {e}"
except Exception as e:
# Catch any other unexpected errors.
return f"An unexpected error occurred: {e}"
# --- Display Chat History ---
# Loop through the messages stored in session state and display them.
for message in st.session_state.messages:
with st.chat_message(message["role"]):
st.markdown(message["content"])
# --- Handle User Input ---
# Create a chat input field for the user to type their question.
if prompt := st.chat_input("What opportunities are currently in negotiation?"):
# Add the user's message to chat history.
st.session_state.messages.append({"role": "user", "content": prompt})
# Display the user's message in the chat container.
with st.chat_message("user"):
st.markdown(prompt)
# Display the assistant's message in the chat container.
with st.chat_message("assistant"):
with st.spinner("Thinking..."):
# Get the response from the API.
response = get_api_response(prompt)
# Display the bot's response.
st.markdown(response)
# Add the bot's response to chat history.
st.session_state.messages.append({"role": "assistant", "content": response})
Hi! I can help you with any questions about Streamlit and Python. What would you like to know?