Skip to content

Commit 5c0ca18

Browse files
committed
Add webapp example
1 parent 1a47edf commit 5c0ca18

File tree

2 files changed

+82
-0
lines changed

2 files changed

+82
-0
lines changed

examples/webapp/bot.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
from telebot import TeleBot
2+
from telebot.types import ReplyKeyboardMarkup, KeyboardButton, WebAppInfo
3+
4+
BOT_TOKEN = '<bot_token>'
5+
WEB_URL = 'https://pytelegrambotminiapp.vercel.app/' # https://github.com/eternnoir/pyTelegramBotAPI/blob/master/examples/webapp/webapp.html
6+
7+
bot = TeleBot(BOT_TOKEN)
8+
9+
@bot.message_handler(commands=["start"])
10+
def start(message):
11+
reply_markup = ReplyKeyboardMarkup(resize_keyboard=True)
12+
reply_markup.row(KeyboardButton("Start MiniApp", web_app=WebAppInfo(WEB_URL)))
13+
bot.reply_to(message, "Click the button to start MiniApp", reply_markup=reply_markup)
14+
15+
@bot.message_handler(content_types=['web_app_data'])
16+
def web_app(message):
17+
bot.reply_to(message, f'Your message is "{message.web_app_data.data}"')
18+
19+
bot.infinity_polling()

examples/webapp/webapp.html

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="UTF-8">
5+
<meta name="viewport" content="width=device-width, initial-scale=1">
6+
<title> MiniApp example </title>
7+
<script src="https://telegram.org/js/telegram-web-app.js"></script>
8+
</head>
9+
10+
<body>
11+
<div>
12+
<input class="input-message" type="text" value="" placeholder="Type your message here">
13+
<button onclick="sendMessage()"> Send the message </button>
14+
</div>
15+
</body>
16+
17+
<script type="text/javascript">
18+
Telegram.WebApp.ready();
19+
function sendMessage(){
20+
Telegram.WebApp.sendData(document.querySelector('.input-message').value);
21+
}
22+
</script>
23+
24+
<style>
25+
body {
26+
background: var(--tg-theme-bg-color);
27+
}
28+
29+
div {
30+
height: 98vh;
31+
display: flex;
32+
flex-direction: column;
33+
justify-content: center;
34+
align-items: center;
35+
gap: 10px;
36+
}
37+
38+
input {
39+
border: solid 1px;
40+
border-radius: 4px;
41+
border-color: var(--tg-theme-text-color);
42+
background-color: var(--tg-theme-bg-color);
43+
color: var(--tg-theme-button-text-color);
44+
padding: 3px;
45+
width: 300px;
46+
height: 30px;
47+
}
48+
49+
button {
50+
border-radius: 6px;
51+
width: 308px;
52+
height: 40px;
53+
background: var(--tg-theme-button-color);
54+
color: var(--tg-theme-button-text-color);
55+
border: none;
56+
}
57+
58+
p {
59+
color: var(--tg-theme-text-color)
60+
}
61+
</style>
62+
63+
</html>

0 commit comments

Comments
 (0)