Skip to content

Latest commit

 

History

History
 
 

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 
 
 
 
 
 
 
 
 

Module 1 - Build a Basic Chat Bot

In this module you'll build a basic bot for Google Chat that repeats back whatever message is received from a user.

Prerequisites

  1. Google Workspace.
  2. Google Cloud account, with billing enabled, or free trial.
  3. Experience with Python.

Steps

1. Create a Google Cloud project

Create a new project in your Google Cloud Console New Google Cloud project

Name the project chatgpt-bot and click CREATE.

New Google Cloud project setup

2. Create a Google Cloud Function

Select your new project as the active project and create a new function in Google Cloud Functions by clicking the CREATE FUNCTION button.

Create Cloud Function

When prompted, enable the required APIs by clicking ENABLE.

Enable required APIs

3. Configure the Cloud Function

Complete the configuration as shown below and click SAVE.

Configure Cloud Function

4. Choose the runtime

Click NEXT and then choose Python 3.11 as the Runtime for the function.

Select the runtime

5. Set the Entry point

Set the "Entry point" to handle_chat.

Set entry point

When the URL endpoint for this function is triggered, the function name listed as the "Entry point" will be run.

6. Update main.py

Replace the default code in main.py with the following code, which replies to the user when the bot is added to a space, or sent a message:

import flask
import functions_framework
import logging
import google.cloud.logging

logging_client = google.cloud.logging.Client()
logging_client.setup_logging(log_level=logging.INFO)

@functions_framework.http
def handle_chat(request):
    """Handles incoming messages from Google Chat."""
    event_data = request.get_json()
    logging.info("received event_data %s" % event_data)
    event_type = event_data['type']

    # Bot added
    if event_type == 'ADDED_TO_SPACE':
        
        # Added to a room
        if event_data['space']['type'] == 'ROOM':
            return { "text" : f"Thanks for adding me to the room. "\
                    "Mention me in a conversation whenever you need help." }

        # Added to a DM
        elif event_data['space']['type'] == 'DM':
            user_display_name = event_data['user']['displayName']
            return { "text" : f"Hi {user_display_name}! I'm here to help "\
                     "whenever you need it."}

    # Bot removed
    elif event_type == 'REMOVED_FROM_SPACE':
        return {}

    # A normal message event
    elif event_type == 'MESSAGE':
        return process_message_event(event_data)


def process_message_event(event_data):
    """Processes message event."""

    incoming_message = event_data.get('message', {})
    user_text = incoming_message.get('argumentText', "")

    return {"text" : f"You said: {user_text}"}

The bot sends a JSON object of data to the Cloud Function for every event that occurs.

View example JSON for the ADDED_TO_SPACE event and MESSAGE event.

7. Update requirements.txt

Open requirements.txt in the editor and add the lines below to specify the dependencies for the function.

functions-framework==3.*
google-cloud-logging==3.0.0

Update requirements

8. Deploy the function

Click DEPLOY to push the function live.

Deploy function

8. Enable the Google Chat API

While the Function is deploying, in a new browser tab open the Google Chat API for your project and click ENABLE.

Enable Chat API

9. Configure the Chat app

Once the API is enabled, click on the Configuration tab.

In the Application info section enter:

  • App name: ChatGPT Bot
  • Avatar URL: https://developers.google.com/chat/images/quickstart-app-avatar.png
  • Description: Bot powered by ChatGPT

Add App Info

In the Interactive features section:

  • Activate Enable Interactive features
  • Check Receive 1:1 messages
  • Check Join spaces and group conversations

Chat interactive features

In Connection settings:

Trigger URL

Go back to your browser tab for the Chat API and:

  • Select App URL
  • Paste the URL into the App URL field

Connection settings

In the Visibility section:

  • Check the box to make the app available to specific people
  • Enter your Google Workspace @colorado.edu email address.

Visibility

In the Logs section:

  • Check the box to Log errors to Logging

Logs

Click SAVE to configure the chat app.

Test the bot

Open Gmail, click on Chat, New chat and type chatgpt and click on your bot.

Select chat bot

The bot should send you a welcome message and repeat back anything you type.

Chat bot test

Next Steps

With your basic chat bot set up, you can now start making it smarter!

Continue with Module 2 to integrate the ChatGPT API with your bot.