Drop files here
or click to upload
import streamlit as st
import requests
import json
# --- Configuration ---
# Endpoint URL for your triggered task Cloud Function
ENDPOINT_URL = "https://emea.snaplogic.com/api/1/rest/slsched/feed/ConnectFasterInc/snapLogic4snapLogic/Bootcamp_EMEA_August_2025/CRM_Retriever_Priyanka%20Task"
# Bearer token for authorization
AUTHORIZATION_TOKEN = "Bearer 1234"
# --- Streamlit App Setup ---
st.set_page_config(page_title="API Chatbot", page_icon="💬")
st.title("API Chatbot 💬")
# Initialize chat history in session state if it doesn't exist
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 interact with the API ---
def send_message_to_api(prompt):
"""
Sends a message to the external API and returns the bot's response.
"""
headers = {
"Authorization": AUTHORIZATION_TOKEN,
"Content-Type": "application/json"
}
# The API expects a list containing a dictionary with a "Question" key
payload = [
{"Question": prompt}
]
try:
with st.spinner("Thinking..."): # Show a spinner while waiting for API response
response = requests.post(ENDPOINT_URL, headers=headers, json=payload)
response.raise_for_status() # Raise an HTTPError for bad responses (4xx or 5xx)
response_json = response.json()
# The API response is expected to be a list, with the actual response text
# under the "Response" key in the first element of the list.
if isinstance(response_json, list) and len(response_json) > 0 and "Response" in response_json[0]:
bot_message = response_json[0].get("Response", "Error: Could not retrieve message.")
return bot_message
else:
return "Error: Unexpected API response format."
except requests.exceptions.RequestException as e:
st.error(f"Error connecting to API: {e}")
return "Sorry, I'm having trouble connecting right now. Please try again later."
except json.JSONDecodeError:
st.error("Error: Could not decode JSON response from API.")
return "Sorry, I received an unreadable response from the server."
except Exception as e:
st.error(f"An unexpected error occurred: {e}")
return "An unexpected error occurred."
# --- Chat Input ---
if prompt := st.chat_input("Say something..."):
# Add user message to chat history
st.session_state.messages.append({"role": "user", "content": prompt})
# Display user message in chat message container
with st.chat_message("user"):
st.markdown(prompt)
# Get bot response from the API
bot_response = send_message_to_api(prompt)
# Add bot message to chat history
st.session_state.messages.append({"role": "assistant", "content": bot_response})
# Display assistant response in chat message container
with st.chat_message("assistant"):
st.markdown(bot_response)
# To run this application:
# 1. Save the code as a Python file (e.g., `chatbot_app.py`).
# 2. Ensure you have Streamlit and the `requests` library installed:
# `pip install streamlit requests`
# 3. Run the app from your terminal:
# `streamlit run chatbot_app.py`
Hi! I can help you with any questions about Streamlit and Python. What would you like to know?