Added new bot endpoints
This commit is contained in:
parent
8deac7f66e
commit
412c279949
@ -21,22 +21,29 @@ Hangouts Chat bot that responds to events and messages from a room asynchronousl
|
|||||||
|
|
||||||
|
|
||||||
import logging
|
import logging
|
||||||
|
import random
|
||||||
|
|
||||||
from flask import Blueprint, render_template, request, json
|
from flask import Blueprint, render_template, request, json
|
||||||
from google.oauth2 import service_account
|
from google.oauth2 import service_account
|
||||||
from googleapiclient.discovery import build
|
from googleapiclient.discovery import build
|
||||||
|
from priceybot2 import models, db
|
||||||
|
|
||||||
logging.basicConfig(filename='example.log', level=logging.DEBUG)
|
logging.basicConfig(filename='example.log', level=logging.DEBUG)
|
||||||
|
|
||||||
chatbot = Blueprint('chatbot', __name__)
|
chatbot = Blueprint('chatbot', __name__)
|
||||||
|
|
||||||
scopes = ['https://www.googleapis.com/auth/chat.bot']
|
scopes = [
|
||||||
|
'https://www.googleapis.com/auth/chat.messages',
|
||||||
|
'https://www.googleapis.com/auth/chat.bot',
|
||||||
|
]
|
||||||
credentials = service_account.Credentials.from_service_account_file('./priceybot2/config/service-acct.json')
|
credentials = service_account.Credentials.from_service_account_file('./priceybot2/config/service-acct.json')
|
||||||
# credentials, project_id = google.auth.default()
|
# credentials, project_id = google.auth.default()
|
||||||
credentials = credentials.with_scopes(scopes=scopes)
|
credentials = credentials.with_scopes(scopes=scopes)
|
||||||
chat = build('chat', 'v1', credentials=credentials)
|
chat = build('chat', 'v1', credentials=credentials)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@chatbot.route('/bot/', methods=['POST'])
|
@chatbot.route('/bot/', methods=['POST'])
|
||||||
def home_post():
|
def home_post():
|
||||||
"""Respond to POST requests to this endpoint.
|
"""Respond to POST requests to this endpoint.
|
||||||
@ -130,3 +137,65 @@ def home_get():
|
|||||||
"""
|
"""
|
||||||
|
|
||||||
return render_template('home.html')
|
return render_template('home.html')
|
||||||
|
|
||||||
|
|
||||||
|
@chatbot.route('/bot/members/<space>', methods=['GET'])
|
||||||
|
def members(space):
|
||||||
|
if space == "":
|
||||||
|
return {'text': "Please specify a space"}
|
||||||
|
try:
|
||||||
|
members = chat.spaces().members().list(parent=f'spaces/{space}').execute()
|
||||||
|
except Exception:
|
||||||
|
return {'text': "Space not found"}
|
||||||
|
|
||||||
|
for member in members['memberships']:
|
||||||
|
uid = int(member['member']['name'].split('/')[-1])
|
||||||
|
display_name = member['member']['displayName']
|
||||||
|
|
||||||
|
|
||||||
|
user = models.User.query.filter_by(google_id=uid).first()
|
||||||
|
|
||||||
|
if user:
|
||||||
|
print(f"User with UID of {uid} already exists in the DB")
|
||||||
|
continue
|
||||||
|
|
||||||
|
new_user = models.User(name=display_name, google_id=uid)
|
||||||
|
db.session.add(new_user)
|
||||||
|
print(f"Adding user {display_name}")
|
||||||
|
db.session.commit()
|
||||||
|
|
||||||
|
return {'text': "x"}
|
||||||
|
|
||||||
|
@chatbot.route('/bot/messages/<space>')
|
||||||
|
def get_messages(space):
|
||||||
|
messages = dir(chat.spaces().messages())
|
||||||
|
print(messages)
|
||||||
|
return "x"
|
||||||
|
|
||||||
|
@chatbot.route('/bot/randomquote/<space>', methods=['GET'])
|
||||||
|
def send_random_quote(space):
|
||||||
|
if space == "":
|
||||||
|
return {'text': "Please specify a space"}
|
||||||
|
try:
|
||||||
|
quote = random.choice(models.Quote.query.all())
|
||||||
|
except Exception:
|
||||||
|
return "Failed to get random quote"
|
||||||
|
|
||||||
|
chat.spaces().messages().create(
|
||||||
|
parent=f'spaces/{space}',
|
||||||
|
body={'text': quote.quote}
|
||||||
|
).execute()
|
||||||
|
|
||||||
|
return f"Sent random quote to space {space}"
|
||||||
|
|
||||||
|
@chatbot.route('/bot/sendquote/<space>', methods=['POST'])
|
||||||
|
def send_quote(space):
|
||||||
|
if request.json.get('quote_id') is None:
|
||||||
|
return "Fuck off"
|
||||||
|
quote = models.Quote.query.filter_by(id=request.json.get('quote_id')).first()
|
||||||
|
chat.spaces().messages().create(
|
||||||
|
parent=f'spaces/{space}',
|
||||||
|
body={'text': quote.quote}
|
||||||
|
).execute()
|
||||||
|
|
||||||
|
return "Sent quote"
|
||||||
|
|||||||
Reference in New Issue
Block a user