-
Notifications
You must be signed in to change notification settings - Fork 51
/
main.py
43 lines (31 loc) · 2.65 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
from telebot import types
import telebot
bot = telebot.TeleBot('bottoken')
def webAppKeyboard(): #создание клавиатуры с webapp кнопкой
keyboard = types.ReplyKeyboardMarkup(row_width=1) #создаем клавиатуру
webAppTest = types.WebAppInfo("https://telegram.mihailgok.ru") #создаем webappinfo - формат хранения url
webAppGame = types.WebAppInfo("https://games.mihailgok.ru") #создаем webappinfo - формат хранения url
one_butt = types.KeyboardButton(text="Тестовая страница", web_app=webAppTest) #создаем кнопку типа webapp
two_butt = types.KeyboardButton(text="Игра", web_app=webAppGame) #создаем кнопку типа webapp
keyboard.add(one_butt, two_butt) #добавляем кнопки в клавиатуру
return keyboard #возвращаем клавиатуру
def webAppKeyboardInline(): #создание inline-клавиатуры с webapp кнопкой
keyboard = types.InlineKeyboardMarkup(row_width=1) #создаем клавиатуру inline
webApp = types.WebAppInfo("https://telegram.mihailgok.ru") #создаем webappinfo - формат хранения url
one = types.InlineKeyboardButton(text="Веб приложение", web_app=webApp) #создаем кнопку типа webapp
keyboard.add(one) #добавляем кнопку в клавиатуру
return keyboard #возвращаем клавиатуру
@bot.message_handler(commands=['start']) #обрабатываем команду старт
def start_fun(message):
bot.send_message( message.chat.id, 'Привет, я бот для проверки телеграмм webapps!)\nЗапустить тестовые страницы можно нажав на кнопки.', parse_mode="Markdown", reply_markup=webAppKeyboard()) #отправляем сообщение с нужной клавиатурой
@bot.message_handler(content_types="text")
def new_mes(message):
start_fun(message)
@bot.message_handler(content_types="web_app_data") #получаем отправленные данные
def answer(webAppMes):
print(webAppMes) #вся информация о сообщении
print(webAppMes.web_app_data.data) #конкретно то что мы передали в бота
bot.send_message(webAppMes.chat.id, f"получили инофрмацию из веб-приложения: {webAppMes.web_app_data.data}")
#отправляем сообщение в ответ на отправку данных из веб-приложения
if __name__ == '__main__':
bot.infinity_polling()