Skip to content

Latest commit

 

History

History
executable file
·
442 lines (302 loc) · 18 KB

README.md

File metadata and controls

executable file
·
442 lines (302 loc) · 18 KB

image

published Run in Cisco Cloud IDE

WEBEX AND CHAT GPT

ChatGPT-Webex integration script! This basic script allows you to easily establish communication between ChatGPT, the advanced language model created by OpenAI, and Webex, the popular video conferencing and collaboration platform provided by Cisco. By utilizing API keys provided by OpenAI and Cisco, you can quickly generate an integration that enables seamless communication between these two powerful platforms. The ultimate goal of this project is to facilitate easy and efficient communication between ChatGPT and Webex, unlocking new possibilities for collaboration and innovation.

Click on the logo to see the demo!

Proof of Concept

WEBEX.CHAT.GPT.mp4

CHAT GPT

  • Authentication, generate an API Key

Authentication is a process that ensures that the user accessing a service or application is authorized to do so. To enable secure and controlled access to an API, developers often generate an API Key, which is a unique string of characters used to identify and authenticate the user or application accessing the API.

image

Here you have the access:

https://platform.openai.com/account/api-keys

image

  • BOT ACCESS Token, you need an account Cisco developer.

WEBEX

Bots, Give Webex users access to outside services right from their Webex spaces. Bots help users automate tasks, bring external content into the discussion, and gain efficiencies.

  1. In this section you need to create an new application and create a bot. Use this link: https://developer.webex.com/my-apps.

image

  1. Then you need to provide a name to your bot and a Bot username*, The username users will use to add your bot to a space.

image

INSTALL PYTHON 3.10.2

To install python in your development environment. you can follow these steps.

  • Download Python via console.

[opc@jenkins-master WBX_GPT]$ wget https://www.python.org/ftp/python/3.10.2/Python-3.10.2.tgz

  • Extract the downloaded archive by running the following command:

[opc@jenkins-master WBX_GPT]$ tar -xvf Python-3.10.2.tgz

Navigate to the extracted directory by running the following command:

[opc@jenkins-master WBX_GPT]$ cd Python-3.10.2

[opc@jenkins-master WBX_GPT]$ ./configure --enable-optimizations

Build and install Python 3.10 using the following command:

[opc@jenkins-master WBX_GPT]$ make

[opc@jenkins-master WBX_GPT]$ sudo make altinstall

[opc@jenkins-master WBX_GPT]$ python3.10.2 --version

Python 3.10.2

Then you can create your environment normal...

CREATE VENV PYTHON 3.10.2 AND DEPENDENCIES

[opc@jenkins-master 00_AUTOMATING_A_NETWORK_INVENTORY_WITH_PYTHON]$ python3.10 -m venv wbx_gpt

[opc@jenkins-master 00_AUTOMATING_A_NETWORK_INVENTORY_WITH_PYTHON]$ cd wbx_gpt

[opc@jenkins-master 00_AUTOMATING_A_NETWORK_INVENTORY_WITH_PYTHON]$ source wbx_gpt/bin/activate

(wbx_gpt) [opc@jenkins-master 00_AUTOMATING_A_NETWORK_INVENTORY_WITH_PYTHON]$ python --version

Python 3.10.2
  • Install Py VENV with dependencies: webexteamssdk, requests, webex_bot

  • Install webexteamssdk

(wbx_gpt) [opc@jenkins-master WBX_GPT]$ pip install webexteamssdk
Collecting webexteamssdk
  Using cached webexteamssdk-1.6.1-py3-none-any.whl (113 kB)
Collecting PyJWT
  Using cached PyJWT-2.6.0-py3-none-any.whl (20 kB)
Collecting requests>=2.4.2
  Using cached requests-2.28.2-py3-none-any.whl (62 kB)
Collecting requests-toolbelt
  Using cached requests_toolbelt-0.10.1-py2.py3-none-any.whl (54 kB)
Collecting future
  Downloading future-0.18.3.tar.gz (840 kB)
     ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 840.9/840.9 kB 10.8 MB/s eta 0:00:00
  Preparing metadata (setup.py) ... done
Collecting urllib3<1.27,>=1.21.1
  Downloading urllib3-1.26.15-py2.py3-none-any.whl (140 kB)
     ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 140.9/140.9 kB 19.7 MB/s eta 0:00:00
Collecting certifi>=2017.4.17
  Using cached certifi-2022.12.7-py3-none-any.whl (155 kB)
Collecting idna<4,>=2.5
  Using cached idna-3.4-py3-none-any.whl (61 kB)
Collecting charset-normalizer<4,>=2
  Downloading charset_normalizer-3.1.0-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (199 kB)
     ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 199.3/199.3 kB 22.9 MB/s eta 0:00:00
Installing collected packages: urllib3, PyJWT, idna, future, charset-normalizer, certifi, requests, requests-toolbelt, webexteamssdk
  DEPRECATION: future is being installed using the legacy 'setup.py install' method, because it does not have a 'pyproject.toml' and the 'wheel' package is not installed. pip 23.1 will enforce this behaviour change. A possible replacement is to enable the '--use-pep517' option. Discussion can be found at https://github.com/pypa/pip/issues/8559
  Running setup.py install for future ... done
Successfully installed PyJWT-2.6.0 certifi-2022.12.7 charset-normalizer-3.1.0 future-0.18.3 idna-3.4 requests-2.28.2 requests-toolbelt-0.10.1 urllib3-1.26.15 webexteamssdk-1.6.1
  • Install requests
(wbx_gpt) [opc@jenkins-master WBX_GPT]$ pip install requests
Requirement already satisfied: requests in ./wbx_gpt/lib/python3.10/site-packages (2.28.2)
Requirement already satisfied: certifi>=2017.4.17 in ./wbx_gpt/lib/python3.10/site-packages (from requests) (2022.12.7)
Requirement already satisfied: urllib3<1.27,>=1.21.1 in ./wbx_gpt/lib/python3.10/site-packages (from requests) (1.26.15)
Requirement already satisfied: idna<4,>=2.5 in ./wbx_gpt/lib/python3.10/site-packages (from requests) (3.4)
Requirement already satisfied: charset-normalizer<4,>=2 in ./wbx_gpt/lib/python3.10/site-packages (from requests) (3.1.0)
  • Install webex_bot
(wbx_gpt) [opc@jenkins-master WBX_GPT]$ pip install webex_bot
Collecting webex_bot
  Using cached webex_bot-0.3.4-py2.py3-none-any.whl (18 kB)
Collecting backoff
  Using cached backoff-2.2.1-py3-none-any.whl (15 kB)
Requirement already satisfied: webexteamssdk==1.6.1 in ./wbx_gpt/lib/python3.10/site-packages (from webex_bot) (1.6.1)
Collecting coloredlogs
  Using cached coloredlogs-15.0.1-py2.py3-none-any.whl (46 kB)
Collecting websockets==10.2
  Using cached websockets-10.2-cp310-cp310-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_12_x86_64.manylinux2010_x86_64.whl (110 kB)
Requirement already satisfied: requests-toolbelt in ./wbx_gpt/lib/python3.10/site-packages (from webexteamssdk==1.6.1->webex_bot) (0.10.1)
Requirement already satisfied: requests>=2.4.2 in ./wbx_gpt/lib/python3.10/site-packages (from webexteamssdk==1.6.1->webex_bot) (2.28.2)
Requirement already satisfied: PyJWT in ./wbx_gpt/lib/python3.10/site-packages (from webexteamssdk==1.6.1->webex_bot) (2.6.0)
Requirement already satisfied: future in ./wbx_gpt/lib/python3.10/site-packages (from webexteamssdk==1.6.1->webex_bot) (0.18.3)
Collecting humanfriendly>=9.1
  Using cached humanfriendly-10.0-py2.py3-none-any.whl (86 kB)
Requirement already satisfied: certifi>=2017.4.17 in ./wbx_gpt/lib/python3.10/site-packages (from requests>=2.4.2->webexteamssdk==1.6.1->webex_bot) (2022.12.7)
Requirement already satisfied: charset-normalizer<4,>=2 in ./wbx_gpt/lib/python3.10/site-packages (from requests>=2.4.2->webexteamssdk==1.6.1->webex_bot) (3.1.0)
Requirement already satisfied: urllib3<1.27,>=1.21.1 in ./wbx_gpt/lib/python3.10/site-packages (from requests>=2.4.2->webexteamssdk==1.6.1->webex_bot) (1.26.15)
Requirement already satisfied: idna<4,>=2.5 in ./wbx_gpt/lib/python3.10/site-packages (from requests>=2.4.2->webexteamssdk==1.6.1->webex_bot) (3.4)
Installing collected packages: websockets, humanfriendly, backoff, coloredlogs, webex_bot
Successfully installed backoff-2.2.1 coloredlogs-15.0.1 humanfriendly-10.0 webex_bot-0.3.4 websockets-10.2
(wbx_gpt) [opc@jenkins-master WBX_GPT]$ 
  • As well you can isntall all the requeriments using the file "requirements.txt" (optional)
[opc@jenkins-master 00_AUTOMATING_A_NETWORK_INVENTORY_WITH_PYTHON]$ cd wbx_gpt

[opc@jenkins-master 00_AUTOMATING_A_NETWORK_INVENTORY_WITH_PYTHON]$ source wbx_gpt/bin/activate

(wbx_gpt) [opc@jenkins-master WBX_GPT]$ pip install -r requirements.txt
  • Create requirements.txt to have all the library and version about your project, permt to specify the dependencies of a project, making it easier for others to recreate the same development environment and install the required packages.
(wbx_gpt) [opc@jenkins-master WBX_GPT]$  pip install -r requirements.txt.
  • Assign file wbx_gpt.py as executable.
    (wbx_gpt) [opc@jenkins-master WBX_GPT]$ ls
    wbx_gpt
    (wbx_gpt) [opc@jenkins-master WBX_GPT]$ touch wbx_gpt.py
    (wbx_gpt) [opc@jenkins-master WBX_GPT]$ ls
    wbx_gpt  wbx_gpt.py
    (wbx_gpt) [opc@jenkins-master WBX_GPT]$ chmod +x wbx_gpt.py
    (wbx_gpt) [opc@jenkins-master WBX_GPT]$ ls
    wbx_gpt  wbx_gpt.py
    (wbx_gpt) [opc@jenkins-master WBX_GPT]$ which python
    ~/DEVNET/WBX_GPT/wbx_gpt/bin/python
  • Store your credentials as variable of environment
(wbx_gpt) [opc@jenkins-master WBX_GPT]$ export WEBEX_TOKEN=NTc1ZTY1ZDQxxxxxxxxxxxxxxxxxiNmE5MGQ5ZDUtZjxxxxxxxxxxxxxx83-4c54-8097-2c1xxxxx >> ~/.bashrc
(wbx_gpt) [opc@jenkins-master WBX_GPT]$ export GPT_TOKEN=sk-q7HxxVxXQxxxxxxxxxxxxxxkFJxxxxxxxxxxxxxw >> ~/.bashrc
(wbx_gpt) [opc@jenkins-master WBX_GPT]$ export [email protected] >> ~/.bashrc
(wbx_gpt) [opc@jenkins-master WBX_GPT]$ source ~/.bashrc
(wbx_gpt) [opc@jenkins-master WBX_GPT]$ echo $WEBEX_TOKEN
  • Now we need to set this variable in your code wbx_gpt.py.
import os
WEBEX_TOKEN = os.environ.get('WEBEX_TOKEN')
GPT_TOKEN = os.environ.get('GPT_TOKEN')
WEBEX_DOMAIN = os.environ.get('WEBEX_DOMAIN')
  • Add this code in order to check if all is working fine... testing
  1. Create this code:
#!/home/opc/DEVNET/WEBEX-CHAT-GPT/wbx_gpt/bin/python
import os
import requests
from urllib3 import disable_warnings, exceptions
# Import the library
from webexteamssdk import WebexTeamsAPI
from webex_bot.webex_bot import WebexBot

disable_warnings(exceptions.InsecureRequestWarning)

WEBEX_TOKEN = os.environ.get('WEBEX_TOKEN')
GPT_TOKEN = os.environ.get('GPT_TOKEN')
WEBEX_DOMAIN = os.environ.get('WEBEX_DOMAIN')

"""

That is an script basic that permit generate an integration between ChatGPT and Webex in this scenario 
need a API key provided for OPENAI (https://platform.openai.com/account/api-keys) and Cisco as well,
provide the Bot token in the page (https://developer.webex.com/).

Goal:

 - Create a comunication between ChatGPT and Webex.

[email protected]
Token_Webex:"credentials"
Token_ChatGPT:"Bearer credentials"

"""


def receive_messages_webex():
    # Capture all messages received from webex
    bot = WebexBot(teams_bot_token=WEBEX_TOKEN, approved_domains=[WEBEX_DOMAIN])
    bot.run()
    print()

if __name__ == "__main__":

    receive_messages_webex()
  • Result:

image

FINAL INTEGRATION

Clone the repository: git clone https://github.com/ERICK-ZABALA/WEBEX-CHAT-GPT.git and

Just run: (wbx_gpt) [opc@jenkins-master WBX_GPT]$ ./wbx_gpt.py

  1. Here you can see how to work your integration between Chat GPT and Webex Bot and in the middle you have your application wbx_gpt.py

image

image

Note:

At this moment you can clone this repository to run the integration between webex and chat GPT. However, if you want to know what changes I did in the file webex_bot.py you can continue reading...

  • Step 1, Step 2 and Step 3.

Step 1

  • We are going to modify part of the library webex_bot.py

image

You should add this code in the file webex_bot.py

from webexteamssdk import WebexTeamsAPI

WEBEX_TOKEN = os.environ.get('WEBEX_TOKEN')
GPT_TOKEN = os.environ.get('GPT_TOKEN')
WEBEX_DOMAIN = os.environ.get('WEBEX_DOMAIN')

# Start library WebexTeamsAPI with token stored
api = WebexTeamsAPI(access_token=WEBEX_TOKEN)

image

Step 2

Replace this function in file webex_bot.py per:

    def process_incoming_message(self, teams_message, activity):
        """
        Process an incoming message, determine the command and action,
        and determine reply.
        :param teams_message: The teams_message object
        :param activity: The websocket activity object
        :return:
        """
        user_email = teams_message.personEmail
        raw_message = teams_message.text
        is_one_on_one_space = 'ONE_ON_ONE' in activity['target']['tags']

        if activity['actor']['type'] != 'PERSON':
            log.debug('message is from a bot, ignoring')
            return
        
        log.info(f"Message from {user_email}: {teams_message} --log")
        
        ############## Capture the message Webex ############################

        self.capture_message(teams_message)    
        
        #####################################################################
        
        if not self.check_user_approved(user_email=user_email, approved_rooms=self.approved_rooms):
            return

        # Remove the Bots display name from the message if this is not a 1-1
        if not is_one_on_one_space:
            raw_message = raw_message.replace(self.bot_display_name, '').strip()

        self.process_raw_command(raw_message, teams_message, user_email, activity)

image

Step 3

We are going to create a function send_message_webex(), message_gpt(), capture_message(teams_message).

        
  def send_message_webex(self, webex_message, person_email):
      # Envía un mensaje instantáneo
      message = api.messages.create(toPersonEmail=person_email, markdown=webex_message)
      print("Sent Message!")
 
  def message_gpt(self, message_gpt, person_email):
      """Retrieve Authentication Token from ACI Controller"""
      # The API send a request message to ChatGPT
      url = "https://api.openai.com/v1/chat/completions"
      person_email = person_email
      header = {
      "Content-Type" : "application/json",
      "Authorization" : f"Bearer {GPT_TOKEN}"
      }
      
      payload = {
      "model": "gpt-3.5-turbo",
      "messages": [{"role": "user", "content": message_gpt}],
      "temperature": 0.7
      }
      
  
      # Send the request to the GPT
      try:
          response = requests.post(url, headers=header, json=payload, verify=False)
          # If the request succeeded, return the token

          if response.status_code == 200:
              print(f'\nReceived Message from Chat GPT4: {response.status_code}')
              
              print(f'\nMessage Chat GPT : {response.json()["choices"][0]["message"]["content"]}')

              self.send_message_webex(response.json()["choices"][0]["message"]["content"], person_email)
              return response.json()
          else:
              return False
      
      except Exception as error_auth:
          print(" Error: Unable to send message to ChatGPT")
          print(error_auth)
          return False

  def capture_message(self, teams_message):
      
      print(type(teams_message)) # <class 'webexteamssdk.models.immutable.Message'>

      # Convert message to dict
      """
      {
          "id": "Y2lzY29zcGFyazovL3VzL01FU1NBR0UvNGM1Yzg5NDAtYzVlYy0xMWVkLWFkNmEtMmQyMzNkMGNjNmQw",
          "roomId": "Y2lzY29zcGFyazovL3VzL1JPT00vMTMzNzY2ODAtYzU2NS0xMWVkLTg1ZjQtNWQzMjAyYjlmM2Y3",
          "roomType": "direct",
          "text": "Cisco",
          "personId": "Y2lzY29zcGFyazovL3VzL1BFT1BMRS8xNTQ4MzZmOC03YmI2LTRlOGUtYTdiMy0yMWI5YmE2NWQ0OTY",
          "personEmail": "[email protected]",
          "created": "2023-03-19T00:23:40.244Z"
      }
      
      """
      message_dict = teams_message.to_dict()
      print(message_dict["text"]) # Cisco

      # Send message to GPT
      self.message_gpt(message_dict["text"], message_dict["personEmail"] )

      return teams_message

Note: The other function below just maintain and that's all :)

FINAL INTEGRATION

image

image

REFERENCES