Drop files here
or click to upload
import streamlit as st
import requests
import json
# App title
st.title("CRM Retriever Chatbot")
st.markdown("Ask me about opportunities in the CRM system! 🚀")
# API details
API_URL = "https://emea.snaplogic.com/api/1/rest/slsched/feed/ConnectFasterInc/snapLogic4snapLogic/Bootcamp_EMEA_August_2025/CRM_Retriever_Anjana%20Task"
AUTH_TOKEN = "Bearer 12345"
# Initialize chat history
if "messages" not in st.session_state:
st.session_state.messages = []
# Display chat messages from history on app rerun
for message in st.session_state.messages:
with st.chat_message(message["role"]):
st.markdown(message["content"])
# Function to send a request to the API
def call_api(question):
headers = {
"Authorization": AUTH_TOKEN,
"Content-Type": "application/json"
}
payload = json.dumps([{"Question": question}])
try:
response = requests.post(API_URL, headers=headers, data=payload)
response.raise_for_status() # Raise an exception for bad status codes
return response.json()
except requests.exceptions.RequestException as e:
return {"error": str(e)}
# React to user input
if prompt := st.chat_input("What is up?"):
# Display user message in chat message container
with st.chat_message("user"):
st.markdown(prompt)
# Add user message to chat history
st.session_state.messages.append({"role": "user", "content": prompt})
# Call the API with the user's question
api_response = call_api(prompt)
# Display assistant response in chat message container
with st.chat_message("assistant"):
if "error" in api_response:
st.error(f"Error: {api_response['error']}")
response_content = f"Sorry, there was an error processing your request: {api_response['error']}"
else:
# The API returns a list, so we access the first element
response_data = api_response[0]
response_content = response_data.get("Response", "No response found.")
st.markdown(response_content)
# Add assistant response to chat history
st.session_state.messages.append({"role": "assistant", "content": response_content})
Hi! I can help you with any questions about Streamlit and Python. What would you like to know?