Browse Source

Ibitial commit

master
blank X 6 months ago
commit
0f1a3bc404
Signed by: blankie GPG Key ID: CC15FC822C7F61F5
  1. 1
      .gitignore
  2. 21
      LICENSE
  3. 8
      example-config.yaml
  4. 34
      herokulock.py
  5. 2
      requirements.txt

1
.gitignore

@ -0,0 +1 @@
config.yaml

21
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.

8
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

34
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())

2
requirements.txt

@ -0,0 +1,2 @@
telethon
pyyaml
Loading…
Cancel
Save