Esempio n. 1
0
def main():
    storage = MemoryStorage()

    client = TelethonClient(api_id=TELETHON_API_ID,
                            api_hash=TELETHON_API_HASH,
                            session=TELETHON_SESSION_NAME)

    deeplink_handler, job_handler = create_callbacks(storage, client)
    bot = PythonTelegramBot(token=TELEGRAM_BOT_TOKEN,
                            deeplink_handler=deeplink_handler,
                            job_handler=job_handler,
                            interval=4)
    bot_name = bot.get_name()

    payload = {
        "channel_id": TEST_CHANNEL_ID,
        "joinchat_key": TEST_JOINCHAT_KEY,
    }

    link, token = generate_invite_link(bot_name)
    storage.insert(token, max_uses=1, payload=payload)
    print(link)

    link, token = generate_invite_link(bot_name, short=False)
    storage.insert(token, max_uses=1, payload=payload)
    print(link)

    link, token = generate_invite_link(bot_name, proto="tg")
    storage.insert(token, max_uses=1, payload=payload)
    print(link)

    bot.start()
    def test_random_deeplinks(self):
        https = f"https://t.me/{self.bot_name}?start="

        link1, token1 = generate_invite_link(self.bot_name)
        self.assertTrue(link1.startswith(https))

        link2, token2 = generate_invite_link(self.bot_name)
        self.assertTrue(link1.startswith(https))

        self.assertNotEqual(token1, token2)
        self.assertNotEqual(link1, link2)
    def test_proto_exist(self):
        with self.assertRaises(ValueError):
            generate_invite_link(self.bot_name, proto="qwe")

        generate_invite_link(self.bot_name, token=self.token, proto="tg")
        generate_invite_link(self.bot_name, token=self.token, proto="http")
        generate_invite_link(self.bot_name, token=self.token, proto="https")
    def test_generate_tg_proto_deeplink(self):
        tg = f"tg://resolve?domain={self.bot_name}&start={self.token}"

        link, _ = generate_invite_link(self.bot_name,
                                       token=self.token,
                                       proto="tg")
        self.assertEqual(link, tg)

        link, _ = generate_invite_link(self.bot_name,
                                       token=self.token,
                                       proto="tg",
                                       short=True)
        self.assertEqual(link, tg)

        link, _ = generate_invite_link(self.bot_name,
                                       token=self.token,
                                       proto="tg",
                                       short=False)
        self.assertEqual(link, tg)
    def test_generate_full_deeplink(self):
        http = f"http://telegram.me/{self.bot_name}?start={self.token}"
        https = f"https://telegram.me/{self.bot_name}?start={self.token}"

        link, _ = generate_invite_link(self.bot_name,
                                       token=self.token,
                                       short=False)
        self.assertEqual(link, https)

        link, _ = generate_invite_link(self.bot_name,
                                       token=self.token,
                                       short=False,
                                       proto="https")
        self.assertEqual(link, https)

        link, _ = generate_invite_link(self.bot_name,
                                       token=self.token,
                                       short=False,
                                       proto="http")
        self.assertEqual(link, http)
    def test_generate_short_deeplink(self):
        http = f"http://t.me/{self.bot_name}?start={self.token}"
        https = f"https://t.me/{self.bot_name}?start={self.token}"

        link, _ = generate_invite_link(self.bot_name, token=self.token)
        self.assertEqual(link, https)

        link, _ = generate_invite_link(self.bot_name,
                                       token=self.token,
                                       short=True)
        self.assertEqual(link, https)

        link, _ = generate_invite_link(self.bot_name,
                                       token=self.token,
                                       proto="http")
        self.assertEqual(link, http)

        link, _ = generate_invite_link(self.bot_name,
                                       token=self.token,
                                       proto="https")
        self.assertEqual(link, https)
    def test_return_token(self):
        link, token = generate_invite_link(self.bot_name)

        self.assertTrue(link.endswith(token))
    def test_keywords_param_only(self):
        with self.assertRaises(TypeError):
            generate_invite_link(self.bot_name, self.token, 2)

        generate_invite_link(self.bot_name, max_uses=2)