# Set up Slack tokens
slack_bot_token = 'Y'
slack_app_token = 'Z'
# Initialize Slack clients
slack_client = WebClient(token=slack_bot_token)
app = App(token=slack_app_token)
def run_conversation(user_input):
while True:
# Step 1: send the conversation and available functions to the model
messages = [{"role": "system", "content": "You are a helpful assistant that interacts with different APIs. You understand when to call an API versus when to respond without calling an API."},
{"role": "user", "content": user_input}]
response = client.chat.completions.create(
model="gpt-3.5-turbo-1106",
messages=messages,
tools=tools,
tool_choice="auto", # auto is default, but we'll be explicit
)
response_message = response.choices[0].message
tool_calls = response_message.tool_calls
# Step 2: check if the model wanted to call a function
if tool_calls:
# Step 3: call the function
# Note: the JSON response may not always be valid; be sure to handle errors
available_tools = {
"create_event": gcal_client.create_event,
"update_event": gcal_client.update_event,
"delete_event": gcal_client.delete_event,
"send_email": gmail_client.send_email,
}
messages.append(response_message) # extend conversation with assistant's reply
# Step 4: send the info for each function call and function response to the model
for tool_call in tool_calls:
tool_name = tool_call.function.name
tool_to_call = available_tools[tool_name]
tool_args = json.loads(tool_call.function.arguments) #
tool_response = tool_to_call(**tool_args)
# If the tool_response is a tuple, take the first element
if isinstance(tool_response, tuple):
tool_response = tool_response[0]
# Convert the tool_response to a string if it's a dictionary
if isinstance(tool_response, dict):
tool_response = json.dumps(tool_response)
messages.append(
{
"tool_call_id": tool_call.id,
"role": "tool",
"name": tool_name,
"content": tool_response,
}
)
response = client.chat.completions.create(
model="gpt-3.5-turbo-1106",
messages=messages)
return response.choices[0].message.content
else:
# Send user input directly to OpenAI API
''' response = client.chat.completions.create(
model="gpt-3.5-turbo-1106",
messages=[
{"role": "system", "content": "You are a helpful assistant that interacts with different APIs. You understand when to call an API versus when to respond without calling an API."},
{"role": "user", "content": user_input}
]
) '''
messages.append(response_message)
# extend conversation with function response
second_response = client.chat.completions.create(
model="gpt-3.5-turbo-1106",
messages=messages,
) # get a new response from the model where it can see the functionresponse
return second_response.choices[0].message.content
# Event listener for incoming messages from Slack
@app.event("message")
def message(event, say, logger):
text = event["text"]
print(text)
user_id = event.get("user")
print(user_id)
if user_id is not None and text is not None:
response = run_conversation(text) # Pass the message text as user_input
say(response) # Sends the response back to the Slack channel
# Event listener for app mentions
@app.event("app_mention")
def handle_app_mention_events(event, say, logger):
text = event.get("text", "")
user_id = event.get("user")
if user_id is not None and text.strip():
response = run_conversation(text) # Pass the message text as user_input
say(response) # Sends the response back to the Slack channel
# Run the Flask server to listen for incoming Slack events
if __name__ == "__main__":
SocketModeHandler(app, app_token=slack_app_token).start()
When I run the code above (specifically the Slack code), I get the message "Bolt app is running!" in my console. But when I enter a message @Bot "Hey, how are you?" in the relevant channel I get the error:
Failed to run listener function (error: The request to the Slack API failed. (url: https://www.slack.com/api/chat.postMessage) The server responded with: {'ok': False, 'error': 'not_allowed_token_type'})
The bot/app has already been invited to the channel I am sending the message in, the app already has all the relevant permissions (see below) and I have double checked that the app token matches the one on the Slack API settings.
I am not sure what else I could do to troubleshoot this?
