if __name__ == '__main__': parser = argparse.ArgumentParser() parser.add_argument('api_id', help='API id') # https://my.telegram.org/apps parser.add_argument('api_hash', help='API hash') parser.add_argument('phone', help='Phone') args = parser.parse_args() tg = Telegram( api_id=args.api_id, api_hash=args.api_hash, phone=args.phone, database_encryption_key='changeme1234', ) # you must call login method before others state = tg.start_login() print ("Checking the return state of the start_login() function call") if state == AuthorizationState.WAIT_CODE: print("Pin is required. In this example, the main program is asking it, not the python-telegram client") pin = input("Please insert pin code here: ") print("In this example, the main program is more polite than the python-telegram client") state = tg.send_code(pin) if state == AuthorizationState.WAIT_PWD: print("Password is required. In this example, the main program is asking it, not the python-telegram client") pwd = getpass.getpass('Insert password here (but please be sure that no one is spying on you): ') tg.send_password (pwd) result = tg.get_me() result.wait() print(result.update)
def _sendMessage(self, telegram_text): telegram_api_id = self.config.get("telegram_api_id", None) if telegram_api_id is None: raise self.server.error("telegram_api_id not configured!") telegram_api_hash = self.config.get("telegram_api_hash", None) if telegram_api_hash is None: raise self.server.error("telegram_api_hash not configured!") telegram_bot_token = self.config.get("telegram_bot_token", None) if telegram_bot_token is None: raise self.server.error("telegram_bot_token not configured!") telegram_database_encryption_key = self.config.get( "telegram_database_encryption_key", None) if telegram_database_encryption_key is None: raise self.server.error( "telegram_database_encryption_key not configured!") telegram_chat_id = self.config.get("telegram_chat_id", None) if telegram_chat_id is None: raise self.server.error("telegram_chat_id not configured!") telegram_code = self.config.get("telegram_code", None) if telegram_code is None: raise self.server.error("telegram_code not configured!") telegram_password = self.config.get("telegram_password", None) if telegram_password is None: raise self.server.error("telegram_password not configured!") try: logging.info(f"Login to telegram") tg = Telegram( api_id=telegram_api_id, api_hash=telegram_api_hash, phone=telegram_bot_token, # you can pass 'bot_token' instead database_encryption_key=telegram_database_encryption_key) state = tg.login(blocking=False) if state == AuthorizationState.WAIT_CODE: # Telegram expects a pin code tg.send_code(telegram_code) state = tg.login(blocking=False) # continue the login process if state == AuthorizationState.WAIT_PASSWORD: tg.send_password(telegram_password) state = tg.login(blocking=False) # continue the login process if state != AuthorizationState.READY: raise self.server.error( f"Error at the telegram login. Authorization state: {tg.authorization_state}" ) logging.info(f"Loading chats") # if this is the first run, library needs to preload all chats # otherwise the message will not be sent result = tg.get_chats() result.wait() logging.info(f"Sending message: to chat {telegram_chat_id}") result = tg.send_message( chat_id=telegram_chat_id, text=telegram_text, ) # `tdlib` is asynchronous, so `python-telegram` always returns you an `AsyncResult` object. # You can receive a result with the `wait` method of this object. result.wait() logging.info(result.update) tg.stop() # you must call `stop` at the end of the script except Exception as e: logging.error("Error: unable to send message to channel", e)