mirror of
https://github.com/aiogram/aiogram.git
synced 2026-04-08 16:37:47 +00:00
Added support of "startapp" deep links (#1648)
* Added "startapp" deep link support * Remove link_type param, added create_startapp_link method * Write tests for create_startapp_link method * Refactor with black & isort * Added CHANGELOG
This commit is contained in:
parent
cd4e811856
commit
de240b62ac
3 changed files with 50 additions and 2 deletions
2
CHANGES/1648.feature.rst
Normal file
2
CHANGES/1648.feature.rst
Normal file
|
|
@ -0,0 +1,2 @@
|
||||||
|
Added new method to utils/deep_linking.py module to creating "startapp" deep links, see also
|
||||||
|
https://core.telegram.org/api/links#main-mini-app-links
|
||||||
|
|
@ -3,6 +3,7 @@ from __future__ import annotations
|
||||||
__all__ = [
|
__all__ = [
|
||||||
"create_start_link",
|
"create_start_link",
|
||||||
"create_startgroup_link",
|
"create_startgroup_link",
|
||||||
|
"create_startapp_link",
|
||||||
"create_deep_link",
|
"create_deep_link",
|
||||||
"create_telegram_link",
|
"create_telegram_link",
|
||||||
"encode_payload",
|
"encode_payload",
|
||||||
|
|
@ -77,9 +78,37 @@ async def create_startgroup_link(
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
|
async def create_startapp_link(
|
||||||
|
bot: Bot,
|
||||||
|
payload: str,
|
||||||
|
encode: bool = False,
|
||||||
|
encoder: Optional[Callable[[bytes], bytes]] = None,
|
||||||
|
) -> str:
|
||||||
|
"""
|
||||||
|
Create 'startapp' deep link with your payload.
|
||||||
|
|
||||||
|
If you need to encode payload or pass special characters -
|
||||||
|
set encode as True
|
||||||
|
|
||||||
|
:param bot: bot instance
|
||||||
|
:param payload: args passed with /start
|
||||||
|
:param encode: encode payload with base64url or custom encoder
|
||||||
|
:param encoder: custom encoder callable
|
||||||
|
:return: link
|
||||||
|
"""
|
||||||
|
username = (await bot.me()).username
|
||||||
|
return create_deep_link(
|
||||||
|
username=cast(str, username),
|
||||||
|
link_type="startapp",
|
||||||
|
payload=payload,
|
||||||
|
encode=encode,
|
||||||
|
encoder=encoder,
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
def create_deep_link(
|
def create_deep_link(
|
||||||
username: str,
|
username: str,
|
||||||
link_type: Literal["start", "startgroup"],
|
link_type: Literal["start", "startgroup", "startapp"],
|
||||||
payload: str,
|
payload: str,
|
||||||
encode: bool = False,
|
encode: bool = False,
|
||||||
encoder: Optional[Callable[[bytes], bytes]] = None,
|
encoder: Optional[Callable[[bytes], bytes]] = None,
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,10 @@
|
||||||
import pytest
|
import pytest
|
||||||
|
|
||||||
from aiogram.utils.deep_linking import create_start_link, create_startgroup_link
|
from aiogram.utils.deep_linking import (
|
||||||
|
create_start_link,
|
||||||
|
create_startapp_link,
|
||||||
|
create_startgroup_link,
|
||||||
|
)
|
||||||
from aiogram.utils.payload import decode_payload, encode_payload
|
from aiogram.utils.payload import decode_payload, encode_payload
|
||||||
from tests.mocked_bot import MockedBot
|
from tests.mocked_bot import MockedBot
|
||||||
|
|
||||||
|
|
@ -44,6 +48,10 @@ class TestDeepLinking:
|
||||||
link = await create_startgroup_link(bot, payload)
|
link = await create_startgroup_link(bot, payload)
|
||||||
assert link == f"https://t.me/tbot?startgroup={payload}"
|
assert link == f"https://t.me/tbot?startgroup={payload}"
|
||||||
|
|
||||||
|
async def test_get_startapp_link(self, bot: MockedBot, payload: str):
|
||||||
|
link = await create_startapp_link(bot, payload)
|
||||||
|
assert link == f"https://t.me/tbot?startapp={payload}"
|
||||||
|
|
||||||
async def test_filter_encode_and_decode(self, payload: str):
|
async def test_filter_encode_and_decode(self, payload: str):
|
||||||
encoded = encode_payload(payload)
|
encoded = encode_payload(payload)
|
||||||
decoded = decode_payload(encoded)
|
decoded = decode_payload(encoded)
|
||||||
|
|
@ -85,6 +93,15 @@ class TestDeepLinking:
|
||||||
|
|
||||||
assert link == f"https://t.me/tbot?start={encoded_payload}"
|
assert link == f"https://t.me/tbot?start={encoded_payload}"
|
||||||
|
|
||||||
|
async def test_get_startapp_link_with_encoding(self, bot: MockedBot, wrong_payload: str):
|
||||||
|
# define link
|
||||||
|
link = await create_startapp_link(bot, wrong_payload, encode=True)
|
||||||
|
|
||||||
|
# define reference link
|
||||||
|
encoded_payload = encode_payload(wrong_payload)
|
||||||
|
|
||||||
|
assert link == f"https://t.me/tbot?startapp={encoded_payload}"
|
||||||
|
|
||||||
async def test_64_len_payload(self, bot: MockedBot):
|
async def test_64_len_payload(self, bot: MockedBot):
|
||||||
payload = "p" * 64
|
payload = "p" * 64
|
||||||
link = await create_start_link(bot, payload)
|
link = await create_start_link(bot, payload)
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue