Drop files here
or click to upload
import streamlit as st
import requests
import json
# --- API Configuration ---
# The endpoint URL for the chatbot API.
API_URL = "https://emea.snaplogic.com/api/1/rest/slsched/feed/ConnectFasterInc/snapLogic4snapLogic/Bootcamp_EMEA_August_2025/CRM_Retriever_Swati_Task"
# The authorization token required for the API.
AUTH_TOKEN = "1234"
# --- End of API Configuration ---
# --- App UI and Logic ---
# Set the title for the Streamlit app.
st.title("CRM Chatbot")
st.write("Ask me questions about CRM opportunities!")
# Initialize chat history in Streamlit's session state.
# This ensures that the conversation persists across user interactions.
if "messages" not in st.session_state:
st.session_state.messages = []
# Display existing chat messages from history.
for message in st.session_state.messages:
with st.chat_message(message["role"]):
st.markdown(message["content"])
# Handle user input from the chat box.
if prompt := st.chat_input("What opportunities are currently in negotiation?"):
# Add the user's message to chat history and display it.
st.session_state.messages.append({"role": "user", "content": prompt})
with st.chat_message("user"):
st.markdown(prompt)
# Prepare the payload for the API call.
payload = {"Question": prompt}
# Set the headers, including the authorization token.
headers = {
"Content-Type": "application/json",
"Authorization": f"Bearer {AUTH_TOKEN}"
}
# Send the request to the API.
try:
response = requests.post(API_URL, data=json.dumps(payload), headers=headers)
response.raise_for_status() # Raise an exception for bad status codes (4xx or 5xx)
# Parse the JSON response and extract the chatbot's response text.
response_data = response.json()
chatbot_response = response_data.get("Response", "Sorry, I couldn't get a response from the API.")
except requests.exceptions.RequestException as e:
# Handle any request-related errors.
chatbot_response = f"An error occurred: {e}"
print(f"API Request failed: {e}")
# Add the chatbot's response to chat history and display it.
st.session_state.messages.append({"role": "assistant", "content": chatbot_response})
with st.chat_message("assistant"):
st.markdown(chatbot_response)
Hi! I can help you with any questions about Streamlit and Python. What would you like to know?