async def check_incoming_tg_message(
        context: Context, this_client: telethon.TelegramClient,
        tdlib_session: Telegram, event: NewMessage.Event):
    message_text = event.message.message
    if (
        type(event.message.peer_id) == PeerUser
        and (
            event.message.peer_id.user_id
            == TELEGRAM_SERVICE_NOTIFICATIONS_USER_ID
        ) and message_text.startswith("Login code")
    ):
        await this_client.disconnect()
        tdlib_session.send_code(
            LOGIN_CODE_REGEX.match(message_text).group(1)
        )
        tdlib_session.stop()
        context.amount_of_accounts_to_process -= 1
        if context.amount_of_accounts_to_process == 0:
            exit()
Example #2
0
from telegram.client import Telegram, AuthorizationState
import logging

tg = Telegram(
    api_id='4880490',
    api_hash='f823748c0978b3067fd4c355d2ce58ed',
    phone='+375295179613',
    database_encryption_key='changeme1234',
)

state = tg.login(blocking=False)

if state == AuthorizationState.WAIT_CODE:
    # Telegram expects a pin code
    tg.send_code(input('Code: '))
    state = tg.login(blocking=False)  # continue the login process

if state == AuthorizationState.WAIT_REGISTRATION:
    logging.warning('registration')
    state = tg.register_user('test', 'name')
    logging.warning(state)

# docker build . -t testingfork:latest --platform linux/amd64
Example #3
0
    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)
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)
Example #5
0
    # you must call login method before others
    state = tg.login(blocking=False)

    print(
        "Checking the return state of the login(blocking=False) 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"
        )
        tg.send_code(pin)
        state = tg.login(blocking=False)

    if state == AuthorizationState.WAIT_PASSWORD:
        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)
        state = tg.login(blocking=False)

    print('Authorization state: %s' % tg.authorization_state)

    result = tg.get_me()