Exemplo n.º 1
0
def bot_get_me(api_id, api_hash, token):
    tg = Telegram(
        api_id=api_id,
        api_hash=api_hash,
        bot_token=token,
        database_encryption_key='changeme1234',
    )
    # you must call login method before others
    tg.login()

    result = tg.get_me()
    result.wait()
    print(result.update)
    tg.stop()
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()
Exemplo n.º 3
0
def main():
    utils.setup_logging()

    parser = argparse.ArgumentParser()
    utils.add_api_args(parser)
    utils.add_proxy_args(parser)
    args = parser.parse_args()

    tg = Telegram(api_id=args.api_id,
                  api_hash=args.api_hash,
                  phone=args.phone,
                  database_encryption_key='changeme1234',
                  proxy_server=args.proxy_server,
                  proxy_port=args.proxy_port,
                  proxy_type=utils.parse_proxy_type(args))
    # you must call login method before others
    tg.login()

    result = tg.get_me()
    result.wait()
    pprint(result.update)

    tg.stop()
Exemplo n.º 4
0
                        help='Messages to retrieve',
                        type=int,
                        default=1000)
    parser.add_argument('--most-common',
                        help='Most common count',
                        type=int,
                        default=30)
    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
    tg.login()

    stats_data = retreive_messages(
        telegram=tg,
        chat_id=args.chat_id,
        receive_limit=args.limit,
    )

    print_stats(
        stats_data=stats_data,
        most_common_count=args.most_common,
    )

    tg.stop()
Exemplo n.º 5
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)