Ibitial commit

This commit is contained in:
blank X 2021-06-04 15:52:18 +07:00
commit 0f1a3bc404
Signed by: blankie
GPG Key ID: CC15FC822C7F61F5
5 changed files with 66 additions and 0 deletions

1
.gitignore vendored Normal file
View File

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

21
LICENSE Normal file
View File

@ -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 Normal file
View File

@ -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 Normal file
View File

@ -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 Normal file
View File

@ -0,0 +1,2 @@
telethon
pyyaml