Skip to content

Commit

Permalink
Merge pull request #146 from alan-turing-institute/ping-pong-api
Browse files Browse the repository at this point in the history
Add ping/pong to get faster out of office
  • Loading branch information
rchan26 authored Jan 3, 2024
2 parents 062b296 + e644a9f commit 27ed984
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 2 deletions.
4 changes: 2 additions & 2 deletions reginald/models/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,8 @@ def main():

# set up basic root endpoint
@app.get("/")
async def root():
return "Hello World!"
async def ping():
return "pong"

# set up direct_message endpoint
@app.get("/direct_message")
Expand Down
14 changes: 14 additions & 0 deletions reginald/slack_bot/bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -394,6 +394,8 @@ async def _process_request(
# if this is a direct message to Reginald...
if event_type == "message" and event_subtype is None:
await self.react(client, event["channel"], event["ts"])
# check VM is reachable
await self.ping()
model_response = await asyncio.get_running_loop().run_in_executor(
None,
lambda: requests.get(
Expand All @@ -405,6 +407,8 @@ async def _process_request(
# if @Reginald is mentioned in a channel
elif event_type == "app_mention":
await self.react(client, event["channel"], event["ts"])
# check VM is reachable
await self.ping()
model_response = await asyncio.get_running_loop().run_in_executor(
None,
lambda: requests.get(
Expand Down Expand Up @@ -473,6 +477,16 @@ async def _process_request(
else:
logging.info("No reply was generated.")

async def ping(self):
pong = await asyncio.get_running_loop().run_in_executor(
None,
lambda: requests.get(
f"{self.api_url}/",
timeout=5,
),
)
pong.raise_for_status()

async def react(
self, client: SocketModeClient, channel: str, timestamp: str
) -> None:
Expand Down

0 comments on commit 27ed984

Please sign in to comment.