#WebEx GPT Python Bot - Issues with GPT Accepting and Posting

5 messages · Page 1 of 1 (latest)

harsh ember
#

Here is my full code:

import os

import requests

from flask import Flask, request, jsonify

import openai

import time



app = Flask(__name__)



# Load your bot's access token and OpenAI API key

WEBEX_BOT_ACCESS_TOKEN = 'XXXXXXXXXXXXXXXXXX'

OPENAI_API_KEY = 'XXXXXXXX'

openai.api_key = OPENAI_API_KEY



history = []



import requests



def send_message(message, room_id):

    headers = {

        "Authorization": f"Bearer {WEBEX_BOT_ACCESS_TOKEN}",

        "Content-Type": "application/json"

    }

    data = {

        "roomId": room_id,

        "text": message

    }

    response = requests.post("https://api.ciscospark.com/v1/messages", headers=headers, json=data)

    

    if response.status_code == 200:

        print("Message sent successfully")

    else:

        print(f"Error sending message: {response.status_code}")





# Set up the OpenAI API call function

def generate_gpt_response(prompt, messages):

    

    try:

        response = openai.ChatCompletion.create(



            model="gpt-4",

        

            prompt=prompt,

            

            messages=messages,

        

            temperature=0.5,

        )

        gpt_response = response.choices[0].text.strip()

        print("GPT response:", gpt_response)

        return gpt_response

        

    except Exception as b:

        print("Error in generate_gpt_response():", b)

        return None



def get_message(message_id):

    url = f"https://webexapis.com/v1/messages/{message_id}"

    headers = {

        'Authorization': f'Bearer {WEBEX_BOT_ACCESS_TOKEN}',

        'Content-Type': 'application/json'

    }

    response = requests.get(url, headers=headers)

    if response.status_code == 200:

        return response.json()

    else:

        print(f"Failed to get message. Status code: {response.status_code}")

        return None





def get_bot_person_id():

    url = f"https://webexapis.com/v1/people/me"

    headers = {

        'Authorization': f'Bearer {WEBEX_BOT_ACCESS_TOKEN}',

        'Content-Type': 'application/json'

    }

    response = requests.get(url, headers=headers)

    if response.status_code == 200:

        return response.json().get('id')

    else:

        print(f"Failed to get bot person ID. Status code: {response.status_code}")

        return None



bot_person_id = get_bot_person_id()



# Set up the Flask route to handle incoming webhook events

@app.route('/webhook', methods=['POST'])

def webhook():

    incoming_data = request.get_json()

    print('Incoming webhook data:', incoming_data)



    data = incoming_data.get('data')

    message_id = data.get('id')

    person_id = data.get('personId')

    email = data.get('personEmail')

    room_id = data.get('roomId')



    if person_id != bot_person_id:

        message = get_message(message_id)

        print('Message data:', message)



        user_message = message.get('text').strip()

        history.append({"role": "user", "content": user_message})

        print("Generating GPT response...")



        gpt_response = generate_gpt_response(user_message, history)

        history.append({"role": "assistant", "content": gpt_response})

        

        send_message(gpt_response, room_id)



    return 'ok', 200



if __name__ == '__main__':

    app.run(debug=True)

#

And here is the error I'm getting....


Incoming webhook data: {'id': 'XXXXXXXXXXX', 'name': 'GPTWebhook', 'targetUrl': 'https://e5e6-50-184-201-245.ngrok.io/webhook', 'resource': 'messages', 'event': 'created', 'orgId': 'Y2lzY29zcGFyazovL3VzL09SR0FOSVpBVElPTi8xZWI2NWZkZi05NjQzLTQxN2YtOTk3NC1hZDcyY2FlMGUxMGY', 'createdBy': 'Y2lzY29zcGFyazovL3VzL1BFT1BMRS9hZWU2OGY5Ni0zYzg1LTQ2NmMtOTkwYy1hMGIxNjYxMmU3MmM', 'appId': 'Y2lzY29zcGFyazovL3VzL0FQUExJQ0FUSU9OL0NmMzkyNWU5NDFmMzhhYTc0M2Y0MmFiNzcwZmZhZjFhNTIyMjcxZDI5OTQ4NDhjNjk2YWMwYTEwN2Q2YTg5MjI3', 'ownedBy': 'creator', 'status': 'active', 'created': '2023-03-22T19:28:55.320Z', 'actorId': 'Y2lzY29zcGFyazovL3VzL1BFT1BMRS9hZWU2OGY5Ni0zYzg1LTQ2NmMtOTkwYy1hMGIxNjYxMmU3MmM', 'data': {'id': 'Y2lzY29zcGFyazovL3VzL01FU1NBR0UvODU4Y2VlNDAtYzhmMi0xMWVkLWI1M2QtN2Q4MmFhMWQ5YzU2', 'roomId': 'Y2lzY29zcGFyazovL3VzL1JPT00vYTU2MDI1OTAtYzI3NC0xMWVkLWJiYjUtODdjODNjYTcwZGY2', 'roomType': 'direct', 'personId': 'Y2lzY29zcGFyazovL3VzL1BFT1BMRS9hZWU2OGY5Ni0zYzg1LTQ2NmMtOTkwYy1hMGIxNjYxMmU3MmM', 'personEmail': 'joshking@cisco.com', 'created': '2023-03-22T20:45:46.660Z'}}
Message data: {'id': 'Y2lzY29zcGFyazovL3VzL01FU1NBR0UvODU4Y2VlNDAtYzhmMi0xMWVkLWI1M2QtN2Q4MmFhMWQ5YzU2', 'roomId': 'Y2lzY29zcGFyazovL3VzL1JPT00vYTU2MDI1OTAtYzI3NC0xMWVkLWJiYjUtODdjODNjYTcwZGY2', 'roomType': 'direct', 'text': 'test', 'personId': 'Y2lzY29zcGFyazovL3VzL1BFT1BMRS9hZWU2OGY5Ni0zYzg1LTQ2NmMtOTkwYy1hMGIxNjYxMmU3MmM', 'personEmail': 'joshking@cisco.com', 'created': '2023-03-22T20:45:46.660Z'}
Generating GPT response...
Error in generate_gpt_response(): Unrecognized request argument supplied: prompt
Error sending message: 400
127.0.0.1 - - [22/Mar/2023 11:45:48] "POST /webhook HTTP/1.1" 200 -


#

I've been beating my head against the wall all day today with this. I have a hunch my generate_gpt_response function is wrong.....so I would love some help if anyone knows what's going on

#

For clarity - the bot is not posting in the webex rooms or direct messages as it should

#

I am using an ngrok tunnel, and it is definitely connected properly to the webhook I've created successfully