Last active
May 12, 2018 23:15
-
-
Save y1n0/16692e01418bba026d3804222a55f94c to your computer and use it in GitHub Desktop.
[veryactivebot](https://t.me/veryactivebot) backend
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| """ | |
| This is the code running the Telegram bot @veryactivebot | |
| (https://t.me/veryactivebot). It ensure that the bot is | |
| always doing an action: typing, uploading a document, | |
| sending audio.. If anyone, for whatever mysterious reason, | |
| finds this code useful, they can use as they want, as long | |
| as they refer to its source. | |
| In order for the bot to work, its authentification token is | |
| needed. This token can be specified in an environment | |
| variable `BOT_TOKEN`. | |
| """ | |
| import logging | |
| import requests | |
| from os import environ | |
| from time import sleep | |
| from pathlib import Path | |
| from random import choice | |
| from threading import Thread | |
| LOGFORMAT = "%(asctime)s\t%(levelname)s:%(funcName)s:%(lineno)s\t%(message)s" | |
| logging.basicConfig(filename="veryactive.log", level=logging.INFO, format=LOGFORMAT) | |
| TOKEN = environ['BOT_TOKEN'] | |
| API = lambda m: 'https://api.telegram.org/bot'+TOKEN+'/'+m | |
| ACTIONS = ( | |
| 'typing', | |
| 'upload_photo', | |
| 'upload_video', | |
| 'upload_audio', | |
| 'upload_document', | |
| 'find_location', | |
| 'upload_video_note' ) | |
| active = set() | |
| last_update = 0 | |
| def poll(): | |
| """ | |
| polls for updates | |
| """ | |
| global last_update | |
| while True: | |
| logging.info('making poll GET request..') | |
| params = { | |
| 'allowed_updates': 'message', | |
| 'timeout': 600, | |
| 'offset': last_update+1 } | |
| req = requests.get( | |
| API('getUpdates'), | |
| data=params, | |
| timeout=(9, 606) ) | |
| req = req.json() | |
| logging.info( | |
| f'request returned {req["ok"]}, ' + \ | |
| f'got {len(req["result"])} updates ') | |
| for u in req['result']: | |
| try: | |
| chat_id = u['message']['chat']['id'] | |
| message = u['message']['text'] | |
| if message.startswith('/start'): | |
| active.add(chat_id) | |
| params = {'chat_id': chat_id, 'action': choice(ACTIONS) } | |
| requests.get(API("SendChatAction"), data=params ) | |
| logging.info(f"{chat_id} added to active") | |
| elif message.startswith('/stop'): | |
| active.remove(chat_id) | |
| logging.info(f"{chat_id} removed from active") | |
| except Exception as e: | |
| logging.info('an exception happened while processing updates:') | |
| logging.exception(e) | |
| finally: | |
| last_update = u['update_id'] | |
| def maintain(): | |
| """ | |
| reset an action every 5s (expiry timeout) | |
| """ | |
| while True: | |
| Path('iamalive').touch(exist_ok=True) | |
| logging.info('maintaining the active chats') | |
| for i in active: | |
| params = { | |
| 'chat_id': i, | |
| 'action': choice(ACTIONS) | |
| } | |
| req = requests.get( | |
| API("SendChatAction"), | |
| data=params ) | |
| logging.info(f'{i} refreshed') | |
| sleep(5) | |
| def main(): | |
| logging.info("starting app..") | |
| maintainer = Thread(target=maintain) | |
| maintainer.start() | |
| logging.info("maintainer thread started") | |
| logging.info("polling..") | |
| poll() | |
| if __name__ == '__main__': | |
| print('running forever') | |
| while True: | |
| try: | |
| main() | |
| except Exception as e: | |
| logging.exception(e) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment