|
1 | 1 | from typing import List |
2 | 2 |
|
3 | | -from aiotg import Bot as BaseBot, API_TIMEOUT, Chat |
| 3 | +from aiotg import Bot as BaseBot, API_TIMEOUT, Chat, asyncio, aiohttp |
4 | 4 | import re |
5 | 5 |
|
6 | 6 |
|
7 | 7 | class Bot(BaseBot): |
8 | | - def __init__(self, api_token: str, moderators: List[int], |
9 | | - api_timeout: int = API_TIMEOUT, |
10 | | - botan_token=None, |
11 | | - name=None): |
| 8 | + healthcheckio_url = 'https://hchk.io/{token}' |
| 9 | + healthcheckio_interval = 150 # seconds |
| 10 | + healthcheckio_token = None |
| 11 | + |
| 12 | + def __init__( |
| 13 | + self, |
| 14 | + api_token: str, moderators: List[int], |
| 15 | + api_timeout: int = API_TIMEOUT, |
| 16 | + botan_token: str = None, |
| 17 | + healthcheckio_token: str = None, |
| 18 | + name=None): |
12 | 19 | super(Bot, self).__init__( |
13 | 20 | api_token=api_token, |
14 | 21 | api_timeout=api_timeout, |
15 | 22 | botan_token=botan_token, |
16 | 23 | name=name |
17 | 24 | ) |
18 | | - |
| 25 | + self.healthcheckio_token = healthcheckio_token |
19 | 26 | self.moderators = moderators |
20 | 27 | self._moderators_commands = [] |
21 | 28 |
|
| 29 | + async def still_alive(self): |
| 30 | + async with aiohttp.ClientSession() as session: |
| 31 | + async with session.get(self.healthcheckio_url.format(token=self.healthcheckio_token)): |
| 32 | + pass |
| 33 | + await asyncio.sleep(self.healthcheckio_interval) |
| 34 | + await self.still_alive() |
| 35 | + |
| 36 | + def run(self, debug=False, reload=None): |
| 37 | + if self.healthcheckio_token: |
| 38 | + loop = asyncio.get_event_loop() |
| 39 | + loop.create_task(self.still_alive()) |
| 40 | + super(Bot, self).run(debug=debug, reload=reload) |
| 41 | + |
22 | 42 | def add_moderators_command(self, regexp, fn): |
23 | 43 | """ |
24 | 44 | Manually register regexp based command |
|
0 commit comments