Set Up Your Google Cloud Account
- Go to the Google Cloud Console and sign in with your Google account.
- Navigate to "Select a Project" and choose "New Project."
Give your project a name and click "Create."
- Visit the API Library and enable the Google Cloud AI services you need, such as Natural Language API, Vision API, etc.
Generate Service Account Key
- In the Cloud Console, navigate to the "IAM & Admin" section and click on "Service Accounts."
- Select "Create Service Account," enter a name, and click "Create."
- Click "Create Key," choose "JSON" as the key type, and download the file. Keep it secure, as it will be used to authenticate your application.
Set Up a Slack App
- Go to the Slack API page and click "Create New App."
- Select the "From scratch" option and give it a name. Choose the Slack workspace where you want to install the app.
- In the "Bot Users" section, add a Bot User for your app, give it a display name, and save the changes.
Install Slack App to Workspace
- Under "OAuth & Permissions," scroll down to the "Scopes" section and add the necessary permissions (e.g., chat:write, channels:read).
- Click "Install App to Workspace" and allow the permissions.
- Copy the "Bot User OAuth Token" for later use.
Connect Google Cloud AI to Slack
- Create a Python script (you may use the below code) to process Slack events and respond with Google Cloud AI's services.
from slack_sdk import WebClient
from slack_sdk.errors import SlackApiError
from google.cloud import language_v1
import json
# Initialize Slack client
slack_token = 'YOUR_SLACK_BOT_TOKEN'
client = WebClient(token=slack_token)
# Google Cloud client
client_google = language_v1.LanguageServiceClient.from_service_account_json('path/to/service_account.json')
def analyze_text(text):
document = language_v1.Document(content=text, type_=language_v1.Document.Type.PLAIN_TEXT)
response = client_google.analyze_sentiment(request={'document': document})
return response.document_sentiment
def handle_message(event_data):
message = event_data['event']
if 'subtype' not in message:
text = message.get('text', '')
sentiment = analyze_text(text)
try:
client.chat_postMessage(channel=message['channel'], text=f'Sentiment score: {sentiment.score}')
except SlackApiError as e:
assert e.response["error"]
Replace `YOUR_SLACK_BOT_TOKEN` and `path/to/service_account.json` with your actual Slack token and path to the Google Cloud service account key respectively.
Use a web framework like Flask to handle Slack events.
from flask import Flask, request, Response
app = Flask(__name__)
@app.route('YOUR_SLACK_EVENT_ENDPOINT', methods=['POST'])
def slack_events():
event = json.loads(request.data)
if 'challenge' in event:
return Response(event['challenge'], mimetype='text/plain')
if 'event' in event:
handle_message(event)
return Response("Event received", status=200)
if __name__ == '__main__':
app.run(port=3000)
Host your application on a platform that supports Flask, like Google App Engine, Heroku, or AWS Lambda.
Update your Slack app's event subscriptions with your application’s URL to receive event data.