def send_api_test_message(telegram: Telegram, chat_id: int): global last_api_message Logger.log_info("Send api message") api_message = f'api_test_{datetime.now()}' sent_api_message = telegram.send_message(chat_id, api_message) sent_api_message.wait(default_timeout_sec) last_api_message = sent_api_message.update
api_hash=args.api_hash, phone=args.phone, database_encryption_key='changeme1234', ) # you must call login method before others tg.login() # if this is the first run, library needs to preload all chats # otherwise the message will not be sent result = tg.get_chats() # `tdlib` is asynchronous, so `python-telegram` always returns you an `AsyncResult` object. # You can wait for a result with the blocking `wait` method. result.wait() if result.error: print(f'get chats error: {result.error_info}') else: print(f'chats: {result.update}') result = tg.send_message( chat_id=args.chat_id, text=args.text, ) result.wait() if result.error: print(f'send message error: {result.error_info}') else: print(f'message has been sent: {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)
print(request.error_info) time.sleep(2000) if 'sending_state' in request.update: if request.update['sending_state'] == 'messageSendingStatePending': print(msg_i, 'messageSendingStatePending') time.sleep(100) elif request.update['sending_state'] == 'messageSendingStateFailed': print(msg_i, 'messageSendingStateFailed') time.sleep(2000) # precautions against flood_wait time.sleep(sleep_time) requests = [] for msg_i, (orig, converted) in enumerate(orig_converted_msgs[:n]): if len(converted['text']) > 20: # 20 means there is only date and newline request = tg.send_message(chat_id, converted['text']) requests.append(request) check_request(request) if 'file' in converted: data = { '@type': 'sendMessage', 'chat_id': chat_id, 'input_message_content': { '@type': 'inputMessageDocument', 'document': { '@type': 'inputFileLocal', 'path': converted['file'] }, }, }