pablomarin / GPT-Azure-Search-Engine

Azure Cognitive Search + Azure OpenAI Accelerator
https://gptsmartsearchapp.azurewebsites.net
MIT License
377 stars 936 forks source link

Cannot hit the chatbot programmatically using Python #63

Closed ManosL closed 7 months ago

ManosL commented 1 year ago

Hello,

I'm trying to develop the following Python program that would hit the created Bot Service programmatically

import requests
import time

direct_line_secret = "XXXXX"
base_url = "https://directline.botframework.com/v3/directline"

# Start a conversation
start_conversation_url = f"{base_url}/conversations"
headers = {
    "Authorization": f"Bearer {direct_line_secret}"
}

response = requests.post(start_conversation_url, headers=headers)
conversation_id = response.json()["conversationId"]
print('Converstion id:', conversation_id)

# Send a message to the bot
send_message_url = f"{base_url}/conversations/{conversation_id}/activities"
message = {
    "type": "message",
    "from": {"id": "user"},
    "text": "what is Reinforcement Learning?"
}

response = requests.post(send_message_url, headers=headers, json=message)
print(response.status_code)
print(response.text)

# Poll for the bot's response
get_activities_url = f"{base_url}/conversations/{conversation_id}/activities"

while True:
    response = requests.get(get_activities_url, headers=headers)
    activities = response.json()["activities"]
    print(activities)
    for activity in activities[::-1]:
        if activity["from"]["id"] == "bot_id":  # Replace "your_bot_id" with the actual bot's ID
            print("Bot's response:", activity["text"])
            exit()  # or break the loop if you only want the latest message

    # Wait for a while before polling again
    time.sleep(1)

However, it gives me Bot's response: To continue to run this bot, please fix the bot source code. but Test in Web Chat works just fine. Also checking the logs I saw the following:

  File "/tmp/8dbd09dbf228fa9/antenv/lib/python3.10/site-packages/botbuilder/core/middleware_set.py", line 79, in receive_activity_internal
    return await callback(context)
  File "/tmp/8dbd09dbf228fa9/antenv/lib/python3.10/site-packages/botbuilder/core/activity_handler.py", line 70, in on_turn
    await self.on_message_activity(turn_context)
  File "/tmp/8dbd09dbf228fa9/bot.py", line 74, in on_message_activity
    input_text_metadata["local_timestamp"] = turn_context.activity.local_timestamp.strftime("%I:%M:%S %p, %A, %B %d of %Y")
AttributeError: 'NoneType' object has no attribute 'strftime'

Is there anything I'm doing wrong in the code and I do not pass the parameters correctly or there is an issue in bot's code?

ManosL commented 1 year ago

I'll give an update. I fixed it by commenting out the following lines in ./apps/backend/bot.py

input_text_metadata["local_timestamp"] = turn_context.activity.local_timestamp.strftime("%I:%M:%S %p, %A, %B %d of %Y")
input_text_metadata["local_timezone"] = turn_context.activity.local_timezone
input_text_metadata["locale"] = turn_context.activity.locale

thus, the problem might be that my message does not have that information. Was there any way to send the message properly and have the code work without commenting out the aforementioned lines?

pablomarin commented 12 months ago

Hello ManosL, were you able to figure this out? Meaning, were you able to send the metadata ?

pablomarin commented 7 months ago

Check Notebook 13. It is solved.