generated from thoth-station/template-project
-
Notifications
You must be signed in to change notification settings - Fork 13
/
chatbot.py
executable file
·82 lines (58 loc) · 2.3 KB
/
chatbot.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
#!/usr/bin/env python3
# Sefkhet-Abwy
# Copyright(C) 2019,2020 Christoph Görn
#
# This program is free software: you can redistribute it and / or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
"""This is a Google Hangouts Chat Bot."""
import os
import logging
import aiohttp
from aiohttp import web
from thoth.common import init_logging
from aicoe.sesheta.actions.chat import process_user_text
from aicoe.sesheta.utils import hangouts_userid
from aicoe.sesheta import __version__
init_logging()
_LOGGER = logging.getLogger("aicoe.sesheta.chat.bot")
_LOGGER.info(f"AICoE's Chat Bot, Version v{__version__}")
logging.getLogger("googleapiclient.discovery_cache").setLevel(logging.ERROR)
routes = web.RouteTableDef()
app = web.Application()
GITHUB_TOKEN = os.environ["GITHUB_ACCESS_TOKEN"]
@routes.get("/")
async def hello(request):
"""Print just a Hello World."""
return web.Response(text="Hello, world")
@routes.get("/healthz")
async def hello(request):
"""Return readiness and liveness informationg."""
return web.Response(text="Ok!", status=200)
@routes.post("/api/v1alpha1")
async def hangouts_handler(request):
"""Handle Google Hangouts Chat incoming webhooks."""
event = await request.json()
_LOGGER.info(f"Incoming json is - {event}")
if event["type"] == "ADDED_TO_SPACE" and event["space"]["type"] == "ROOM":
text = 'Thanks for adding me to "%s"!' % event["space"]["displayName"]
elif event["type"] == "MESSAGE":
intent = await process_user_text(event["message"]["thread"]["name"], event["message"]["text"])
text = intent
else:
return
return web.json_response({"text": text})
if __name__ == "__main__":
_LOGGER.setLevel(logging.DEBUG)
_LOGGER.debug("Debug mode turned on")
app.add_routes(routes)
web.run_app(app)