From 0f1a3bc404944f810d567fd4a1026734fea3dfe2 Mon Sep 17 00:00:00 2001 From: blank X Date: Fri, 4 Jun 2021 15:52:18 +0700 Subject: [PATCH] Ibitial commit --- .gitignore | 1 + LICENSE | 21 +++++++++++++++++++++ example-config.yaml | 8 ++++++++ herokulock.py | 34 ++++++++++++++++++++++++++++++++++ requirements.txt | 2 ++ 5 files changed, 66 insertions(+) create mode 100644 .gitignore create mode 100644 LICENSE create mode 100644 example-config.yaml create mode 100644 herokulock.py create mode 100644 requirements.txt diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..5b6b072 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +config.yaml diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..211185a --- /dev/null +++ b/LICENSE @@ -0,0 +1,21 @@ +MIT License + +Copyright (c) 2021 blank X + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. diff --git a/example-config.yaml b/example-config.yaml new file mode 100644 index 0000000..20ebeb9 --- /dev/null +++ b/example-config.yaml @@ -0,0 +1,8 @@ +telegram: + api_id: https://my.telegram.org + api_hash: https://my.telegram.org + bot_token: https://t.me/BotFather +config: + lock_chat_id: -1001289824958 + lock_wait_delay: 10 + command_when_unlocked: sh /run.sh diff --git a/herokulock.py b/herokulock.py new file mode 100644 index 0000000..a43b38e --- /dev/null +++ b/herokulock.py @@ -0,0 +1,34 @@ +import asyncio +import logging +import yaml +from telethon import TelegramClient, events + +logging.basicConfig(level=logging.INFO) +with open('config.yaml') as file: + config = yaml.safe_load(file.read()) +api_id = config['telegram']['api_id'] +api_hash = config['telegram']['api_hash'] +bot_token = config['telegram'].get('bot_token') +lock_chat_id = config['config']['lock_chat_id'] +lock_wait_delay = config['config']['lock_wait_delay'] +command_when_unlocked = config['config']['command_when_unlocked'] + +client = TelegramClient(None, api_id, api_hash) +@client.on(events.NewMessage(lock_chat_id, outgoing=True, pattern='^herokulock check$')) +async def herokulock_check(e): + await e.reply('herokulock online') + +@client.on(events.NewMessage(lock_chat_id, outgoing=True, pattern='^herokulock online$')) +async def herokulock_online(e): + logging.error('A herokulock instance is running, exiting') + exit(1) + +async def main(): + await client.start(bot_token=bot_token) + await client.send_message(lock_chat_id, 'herokulock check') + await asyncio.sleep(lock_wait_delay) + proc = await asyncio.create_subprocess_shell(command_when_unlocked) + await proc.communicate() + exit(proc.returncode) + +client.loop.run_until_complete(main()) diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..bc7e695 --- /dev/null +++ b/requirements.txt @@ -0,0 +1,2 @@ +telethon +pyyaml