Skip to content

Commit de1081a

Browse files
committed
Add the CallbackQuery.open_private_chat method
1 parent afbeacb commit de1081a

File tree

2 files changed

+24
-0
lines changed

2 files changed

+24
-0
lines changed

botogram/context.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,10 @@ def __enter__(self):
3939
def __exit__(self, *_):
4040
_local._botogram_context.pop()
4141

42+
def bot_username(self):
43+
"""Get the username of the bot"""
44+
return self.bot.itself.username
45+
4246
def component_name(self):
4347
"""Get the name of the current component"""
4448
return self.hook.component.component_name

botogram/objects/callbacks.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
# DEALINGS IN THE SOFTWARE.
2020

2121
from .base import BaseObject
22+
from ..context import ctx
2223
from .messages import User, Message
2324
from .mixins import _require_api
2425

@@ -73,6 +74,25 @@ def open_url(self, url, cache=0):
7374
"cache_time": cache,
7475
})
7576

77+
@_require_api
78+
def open_private_chat(self, start_arg, cache=0):
79+
"""Open the bot private chat with the user"""
80+
self._answered = True
81+
82+
# Telegram doesn't support opening private chats with empty parameters,
83+
# so here we present the developer a friendlier message
84+
if not start_arg:
85+
raise ValueError("You must provide a non-empty start argument")
86+
87+
# Construct the correct link
88+
url = "https://t.me/" + ctx().bot_username() + "?start=" + start_arg
89+
90+
self._api.call("answerCallbackQuery", {
91+
"callback_query_id": self.id,
92+
"url": url,
93+
"cache_time": cache,
94+
})
95+
7696
@_require_api
7797
def _maybe_send_noop(self):
7898
"""Internal function to hide the spinner if needed"""

0 commit comments

Comments
 (0)