Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add keyboard chat request example #14

Merged
merged 1 commit into from
Jun 30, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 62 additions & 0 deletions examples/keyboard_chat_request.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
import logging

from tgram import TgBot, filters
from tgram.types import (
KeyboardButtonRequestChat,
KeyboardButtonRequestUsers,
Message,
ReplyKeyboardMarkup,
KeyboardButton,
)

bot = TgBot("API_TOKEN_HERE", parse_mode="Markdown")

logging.basicConfig(level=logging.INFO)


@bot.on_message(filters.regex("^/start$") & filters.private)
async def on_start_message(_, m: Message) -> Message:
return await m.reply_text(
"Hi, [{}](tg://user?id={})".format(m.from_user.first_name, m.from_user.id),
reply_markup=ReplyKeyboardMarkup(
[
[
KeyboardButton(
"Channel",
request_chat=KeyboardButtonRequestChat(chat_is_channel=True),
),
KeyboardButton(
"Group",
request_chat=KeyboardButtonRequestChat(chat_is_channel=False),
),
],
[
KeyboardButton(
"User",
request_users=KeyboardButtonRequestUsers(user_is_bot=False),
),
KeyboardButton(
"Bot",
request_users=KeyboardButtonRequestUsers(user_is_bot=True),
),
],
],
resize_keyboard=True,
input_field_placeholder="Choose chat type",
),
)


@bot.on_message(filters.chat_shared | filters.users_shared)
async def get_id(_, m: Message) -> Message:
if m.chat_shared:
text = f"Chat ID: `{m.chat_shared.chat_id}`"
else:
text = (
f"Users ID: `" + " ".join([i.user_id for i in m.users_shared.users]) + "`"
)

return await m.reply_text(text)


bot.run_for_updates()
Loading