11#!/usr/bin/env python
22# -*- coding: utf-8 -*-
33#
4- # Simple Bot to reply to Telegram messages
4+ # Sample bot to show proof of concept menu system
55# This program is dedicated to the public domain under the CC0 license.
66
77import logging
88
99import telegram .ext
1010from telegram .ext import CommandHandler
11- from telegram .ext .menu import Menu , MenuHandler , Button , BackButton , ToggleButton , RadioButton
11+ from telegram .ext .menu import MenuHandler , Button , BackButton , ToggleButton , RadioButton , \
12+ make_menu
1213
1314logging .basicConfig (format = '%(asctime)s - %(name)s - %(levelname)s - %(message)s' ,
1415 level = logging .DEBUG )
1516
1617logger = logging .getLogger (__name__ )
1718
18- updater = telegram .ext .Updater ('225497476:AAGzbYE3aAYJFmOtRNqIL0qEBisdzx2xdSc ' )
19+ updater = telegram .ext .Updater ('225497476:AAFHMga1uS470y49vUxyutvUbpyZXxnvvnQ ' )
1920dp = updater .dispatcher
2021
2122
@@ -24,7 +25,6 @@ def error(bot, update, e):
2425
2526
2627def start (bot , update ):
27- update .callback_query .answer ()
2828 update .effective_message .reply_text ('Hello!' )
2929
3030
@@ -33,55 +33,48 @@ def submit(bot, update, menu_data):
3333 update .effective_message .reply_text (str (menu_data ))
3434
3535
36- class MainMenu (Menu ):
37- def text (self , update ):
38- return 'Hello {}. This is the main menu!' .format (update .effective_user .first_name )
39-
40- def buttons (self ):
41- return [
42- [Button ('Test' , menu = sub_menu1 ), Button ('Test2' , menu = sub_menu2 )],
43- [Button ('Exit' , callback = start )]
44- ]
45-
46-
47- class SubMenu1 (Menu ):
48- text = 'This is sub menu 1'
49-
50- def buttons (self ):
51- return [
52- [Button ('URL' , url = 'https://google.com' )],
53- [Button ('Recursion' , menu = sub_menu1 ), Button ('Other menu!' , menu = sub_menu2 )],
54- [BackButton ('Back' )]
55- ]
56-
57-
58- class SubMenu2 (Menu ):
59- text = 'This is sub menu 2'
60-
61- buttons = [
62- [
63- ToggleButton ('test' , 'Test' ),
64- ToggleButton ('count' , states = ((1 , '1' ), (2 , '2' ), (3 , '3' )), default = 2 )
65- ],
66- [
67- RadioButton ('options' , 1 , 'Option 1' ),
68- RadioButton ('options' , 2 , 'Option 2' , enabled = True ),
69- RadioButton ('options' , 3 , 'Option 3' )
70- ],
71- [
72- RadioButton ('custom' , 1 , ('[ ] Custom' , '[x] Custom' )),
73- RadioButton ('custom' , 2 , ('[ ] Custom 2' , '[x] Custom 2' ), enabled = True )
74- ],
75- [
76- Button ('Submit' , submit , pass_menu_data = True ),
77- BackButton ('Back' )
78- ]
79- ]
80-
81-
82- main_menu = MainMenu ()
83- sub_menu1 = SubMenu1 ()
84- sub_menu2 = SubMenu2 ()
36+ main_menu = make_menu ('main' , lambda update : 'Hello {}. This is the main menu!' .format (
37+ update .effective_user .first_name ), buttons = lambda update : [
38+ [Button ('Test' , menu = sub_menu1 ), Button ('Test2' , menu = sub_menu2 )]
39+ ])
40+
41+
42+ def sub_menu1_buttons (update ):
43+ # NOTE: Doesn't work yet... This is because telegram doesn't send language_code for other
44+ # than messages. this means we are required to safe that data when we first get a message
45+ # and then use it afterwards. But menus don't currently pass user_data. Waiting for #1080
46+ lang_default = 'en_GB'
47+ if update and update .effective_user .language_code :
48+ lang_default = update .effective_user .language_code
49+ return [
50+ [Button ('URL' , url = 'https://google.com' )],
51+ [Button ('Recursion' , menu = sub_menu1 ), Button ('Other menu!' , menu = sub_menu2 )],
52+ [ToggleButton ('lang' , states = (('en_GB' , 'English (GB)' ), ('en_US' , 'English (US)' )),
53+ default = lang_default )],
54+ [BackButton ('Back' )]
55+ ]
56+
57+
58+ sub_menu1 = make_menu ('sub_1' , 'This is sub menu 1' , buttons = sub_menu1_buttons )
59+ sub_menu2 = make_menu ('sub_2' , 'This is sub menu 2' , buttons = [
60+ [
61+ ToggleButton ('test' , 'Test' ),
62+ ToggleButton ('count' , states = ((1 , '1' ), (2 , '2' ), (3 , '3' )), default = 2 )
63+ ],
64+ [
65+ RadioButton ('options' , 1 , 'Option 1' ),
66+ RadioButton ('options' , 2 , 'Option 2' , enabled = True ),
67+ RadioButton ('options' , 3 , 'Option 3' )
68+ ],
69+ [
70+ RadioButton ('custom' , 1 , ('[ ] Custom' , '[x] Custom' )),
71+ RadioButton ('custom' , 2 , ('[ ] Custom 2' , '[x] Custom 2' ), enabled = True )
72+ ],
73+ [
74+ Button ('Submit' , submit , pass_menu_data = True ),
75+ BackButton ('Back' )
76+ ]
77+ ])
8578
8679dp .add_handler (MenuHandler (main_menu ))
8780dp .add_handler (CommandHandler ('menu' , main_menu .start ))
0 commit comments