|
| 1 | +# This file is part of https://github.com/jainamoswal/BotStatus. |
| 2 | +# Usage covered in < GPL-3.0 License > |
| 3 | +# Jainam Oswal. <jainam.me> |
| 4 | + |
| 5 | +# import modules |
| 6 | +import os, logging, asyncio, re, github, requests, platform |
| 7 | +from pytz import timezone |
| 8 | +from datetime import datetime |
| 9 | +import telethon |
| 10 | + |
| 11 | +print(':::::::::::::::::::::: ⏲️ Action started successfully ⏲️ ::::::::::::::::::::::') |
| 12 | +print() |
| 13 | +logging.basicConfig(format='[%(levelname) 5s/%(asctime)s] %(name)s: %(message)s', level=logging.CRITICAL) |
| 14 | + |
| 15 | +# define configs |
| 16 | +bots = requests.get(os.getenv('BOTS')).json() # the raw link of the gist file having config data in JSON format |
| 17 | +api_id = os.getenv('APP_ID') # API ID from my.telegram.org |
| 18 | +api_hash = os.getenv('API_HASH') # APP hash from my.telegram.org |
| 19 | +session = os.getenv('SESSION') # session string via telethon |
| 20 | +client = telethon.TelegramClient(telethon.sessions.StringSession(session), api_id, api_hash) # none of your bussiness |
| 21 | + |
| 22 | +# print some information |
| 23 | +def display(): |
| 24 | + print() |
| 25 | + print(f"◻️ Telethon version : {telethon.__version__}") # telethon version |
| 26 | + print(f"◻️ Platform : {platform.system()}") # system |
| 27 | + print(f"◻️ Platform release : {platform.release()}") # system version |
| 28 | + print(f"◻️ OS version : {platform.version().split()[0]}") # OS version |
| 29 | + print() |
| 30 | + |
| 31 | +# updates in ReadMe file at github |
| 32 | +def updateme(old, json_data, first_match, second_match): |
| 33 | + new = '''\n| 🤖 Bot 🤖 | ⭐️ Status ⭐️ |\n| :-: | :-: |\n''' |
| 34 | + for i in json_data: |
| 35 | + new += f"| [{i}](https://t.me/{i}) | {'✔️' if json_data[i] else '❌'} |\n" |
| 36 | + new_string = f"\n{first_match}\n{new}\n`Updated last at ~ {datetime.now(timezone('Asia/Kolkata')).strftime('%H:%M:%S on %Y-%m-%d ')} INR` 🏳️🌈\n\n" |
| 37 | + new_string += f"**Made with ❤️ via [BotStatus](https://github.com/jainamoswal/botstatus)**. \n{second_match}" |
| 38 | + return re.sub(f'\n{first_match}.*?{second_match}', new_string, old, flags=re.DOTALL) |
| 39 | + |
| 40 | +# fetch status of all bots listed in the raw gist file |
| 41 | +async def main(): |
| 42 | + async with client: |
| 43 | + bot_status = {} |
| 44 | + for each_bot in bots: |
| 45 | + async with client.conversation(each_bot, exclusive=False) as conv: |
| 46 | + try: |
| 47 | + sent = await conv.send_message('/' + bots[each_bot]['start']) |
| 48 | + received = await conv.get_response(timeout=bots[each_bot]['sleep']) |
| 49 | + await received.delete() |
| 50 | + bot_status.update({each_bot:True}) |
| 51 | + await sent.delete() |
| 52 | + except Exception as e: |
| 53 | + if type(e).__name__ == "YouBlockedUserError": |
| 54 | + print(f'🚧 You\'ve blocked @{each_bot}. Please unblock it, until next run, I\'ll mark it as down. 🚧') # you blocked the bot :( |
| 55 | + bot_status.update({each_bot:False}) # bot didn't replied back :( |
| 56 | + return bot_status |
| 57 | + |
| 58 | +# edit the message with status at telegram |
| 59 | +async def edit_message(data): |
| 60 | + async with client: |
| 61 | + text = 'Live status of my bots goes following ~\n' |
| 62 | + for i in data: |
| 63 | + text += f"🔅 [{i}](https://t.me/{i}) ~ {'🚀' if data[i] else '❌'}\n" |
| 64 | + text += f"\n**Last Checked:** \n__{datetime.now(timezone('Asia/Kolkata')).strftime('%Y-%m-%d %H:%M:%S')} IST__\n\n**Bots status are auto-updated every 3 hours at random frequency.**\n\n[J Projects](https://t.me/J_Projects)" |
| 65 | + await client.edit_message(int(os.getenv('CHANNEL_ID')), int(os.getenv('MESSAGE_ID')), text, link_preview=False) |
| 66 | + |
| 67 | +# run the script via __main__ style |
| 68 | +if __name__ == '__main__': |
| 69 | + json_data = client.loop.run_until_complete(main()) |
| 70 | + display() |
| 71 | + for each in json_data: |
| 72 | + print(f"🔸 @{each} is {'🟢' if json_data[each] else '🔴'}") |
| 73 | + client.loop.run_until_complete(edit_message(json_data)) |
| 74 | + repo = github.Github(os.getenv('GITHUB_TOKEN')).get_repo(os.getenv('GITHUB_REPOSITORY')) |
| 75 | + contents = repo.get_contents("README.md") |
| 76 | + repo.update_file("README.md", "✨ auto-updated bot status. ✨", updateme(contents.decoded_content.decode(), json_data, '<start>', '<end>'), contents.sha) |
| 77 | + |
| 78 | +print() |
| 79 | +print('::::::::::::::::::::: 🎉 Action completed successfully 🎉 :::::::::::::::::::::') |
| 80 | +print('Mind joining @j_projects at Telegram and do follow me on github.com <jainamoswal>') |
| 81 | + |
| 82 | + |
0 commit comments