1
- from telegram .ext import ConversationHandler , CommandHandler , MessageHandler , Filters , CallbackContext
2
- from telegram import Update , CallbackQuery
1
+ from telegram .ext import ConversationHandler , CommandHandler , MessageHandler , Filters , CallbackContext , CallbackQueryHandler , DispatcherHandlerStop
2
+ from telegram import Update , CallbackQuery , InlineKeyboardMarkup , InlineKeyboardButton
3
+ import osm .osm_util
4
+ import osm .osm_api
3
5
import logging
6
+ import io
4
7
5
8
# Enable logging
6
9
logging .basicConfig (format = '%(asctime)s - %(name)s - %(levelname)s - %(message)s' ,
7
10
level = logging .INFO )
8
11
9
12
logger = logging .getLogger (__name__ )
10
13
11
- CHOOSING , TYPING_CHOICE , TYPING_REPLY = range (3 )
14
+ CHOOSING , TAG_CHOICE , VALUE_REPLY , TYPING_REPLY , LOCATION , TEXT , DESCRIPTION , NAME , GPX_SAVE , SAVE = range (10 )
12
15
13
16
14
17
class ElemEditor :
15
18
def __init__ (self ):
19
+ self .osmapi = osm .osm_api .OsmApi ()
16
20
pass
17
21
18
22
def get_conversation (self ):
19
23
return ConversationHandler (
20
- entry_points = [CommandHandler ('_edit' , self .start )],
24
+ entry_points = [CallbackQueryHandler (self .start ),
25
+ MessageHandler (Filters .document , self .gpx_up )],
21
26
22
27
states = {
23
28
CHOOSING : [MessageHandler (Filters .regex ('tag' ), self .tag ),
24
29
MessageHandler (Filters .regex ('cancel' ), self .cancel )
25
30
],
26
31
27
- TYPING_CHOICE : [MessageHandler (Filters .text , self .value )
28
- ],
29
32
30
- TYPING_REPLY : [MessageHandler (Filters .text , self .edit_info ),
33
+ TAG_CHOICE : [MessageHandler (Filters .text , self .tag )],
34
+
35
+ TYPING_REPLY : [MessageHandler (Filters .text , self .value ),
36
+ MessageHandler (Filters .location , self .location )
31
37
],
38
+ NAME : [MessageHandler (Filters .text , self .gpx_name )],
39
+
40
+ DESCRIPTION : [CommandHandler ('skip' , self .gpx_up_content ),
41
+ MessageHandler (Filters .text , self .gpx_desc )],
42
+
43
+ GPX_SAVE :[CommandHandler ('save' , self .gpx_up_content ),
44
+ CallbackQueryHandler (self .gpx_toggles , 0 )
45
+ ]
32
46
},
33
47
34
- fallbacks = [MessageHandler (Filters .regex ('^Done$' ), self .cancel )]
48
+ fallbacks = [MessageHandler (Filters .regex ('^Done$' ), self .cancel ),
49
+ MessageHandler (Filters .command , self .invalid , 0 ),
50
+ CallbackQueryHandler (self .invalid )]
35
51
)
36
52
37
- def start (self , update , context ):
38
- print ('hello here conversation start' )
39
- return CHOOSING
53
+ def start (self , update : Update , context : CallbackContext ):
54
+ if update .callback_query .data == 'edit' :
55
+ update .callback_query .answer ('enter edit conversation' )
56
+ try :
57
+ thing = context .user_data ['edit' ]
58
+ # send message containing all tags and values of object
59
+ return TAG_CHOICE
60
+ except KeyError :
61
+ context .bot .send_message (update .effective_chat .id , 'select action' ) # for now only Note create
62
+ context .user_data ['subject' ] = osm .osm_util .Note
63
+ context .bot .send_message (update .effective_chat .id , 'please send location.' )
64
+ return TYPING_REPLY
65
+ return # handled by bot
66
+
67
+ def gpx_up (self , update : Update , context ):
68
+ data = open (update .effective_message .document .get_file ().download ()).read ()
69
+ context .user_data ['gpx' ] = osm .osm_util .Trace (None , data , None , None , None )
70
+ update .message .reply_text ('please send trace-name.' )
71
+ return NAME
72
+
73
+ def gpx_name (self , update , context ):
74
+ name = update .message .text
75
+ context .user_data ['gpx' ].name = name
76
+ update .message .reply_text ('please write a Description, or use /skip .' )
77
+ return DESCRIPTION
78
+
79
+ def gpx_desc (self , update , context ):
80
+ context .user_data ['gpx' ].desc = update .message .text
81
+ return GPX_SAVE
82
+
83
+ def gpx_up_content (self , update , context ):
84
+ gpx : osm .osm_util .Trace = context .user_data ['gpx' ]
85
+ markup = InlineKeyboardMarkup ([[InlineKeyboardButton (gpx .visibility , callback_data = 'vis' )],
86
+ [InlineKeyboardButton ('Upload' , callback_data = 'save' )]])
87
+ context .bot .send_message (update .effective_chat .id , str (gpx ), reply_markup = markup )
88
+ return GPX_SAVE
89
+
90
+ def gpx_toggles (self , update , context ):
91
+ gpx = context .user_data ['gpx' ]
92
+ if update .callback_query .data == 'save' :
93
+ self .gpx_save (update , context )
94
+ del context .user_data ['gpx' ]
95
+ self .cancel (update , context )
96
+ elif update .callback_query .data == 'public' :
97
+ gpx .public = not gpx .public
98
+ context .user_data ['gpx' ] = gpx
99
+ elif update .callback_query .data == 'vis' :
100
+ vis = ['identifiable' , 'trackable' , 'public' , 'private' ]
101
+ gpx .visibility = vis [vis .index (gpx .visibility ) - 1 ]
102
+ context .user_data ['gpx' ] = gpx
103
+ self .gpx_up_content (update , context )
104
+
105
+ def gpx_save (self , update , context ):
106
+ gpx = context .user_data ['gpx' ]
107
+ tid = self .osmapi .upload_gpx (gpx .gpx , gpx .name , gpx .desc , gpx .tags , gpx .public , gpx .visibility )
108
+ context .bot .send_message (update .effective_chat .id , str (tid ))
109
+
110
+ def invalid (self , update , context ):
111
+ logger .error ('invalid command' )
112
+
113
+ def location (self , update , context ):
114
+ loc = update .message .location
115
+ update .message .reply_text ('send Note text' )
116
+ return TYPING_REPLY
40
117
41
118
def tag (self , update , context ):
42
- print ('hay here is converseation Tag' )
119
+ print ('hay here is conversation Tag' )
43
120
return CHOOSING
44
121
45
122
def value (self ):
@@ -51,5 +128,9 @@ def edit_info(self, update: Update, context: CallbackContext):
51
128
update .callback_query .edit_message_text ()
52
129
return CHOOSING
53
130
54
- def cancel (self ):
131
+ def note (self ):
55
132
pass
133
+
134
+ def cancel (self , update , context ):
135
+ update .callback_query .answer ('exit edit conversation' )
136
+ return ConversationHandler .END
0 commit comments