-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbot.py
232 lines (181 loc) · 7.16 KB
/
bot.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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
import logging
from os import environ
from pathlib import Path
from textwrap import dedent
import requests
from telegram import Update, ReplyKeyboardMarkup, ReplyKeyboardRemove
from telegram.ext import Updater, CommandHandler, ConversationHandler, CallbackContext, MessageHandler, Filters, \
PicklePersistence
from rc_modules import Proofing
logging.basicConfig(format='%(asctime)s : %(levelname)s : %(message)s',
level=logging.INFO)
logger = logging.getLogger(__name__)
BOT_TOKEN = environ['BOT_TOKEN']
QA_HOST = environ['QA_HOST']
QA_PORT = environ['QA_PORT']
NUM_RANK = int(environ.get('NUM_RANK', 1))
ENABLE_PROOFING = int(environ.get('ENABLE_PROOFING', 0))
DATA_DIR = '.cache/data/'
ASK_STATE = 1
SETTING_STATE = 101
SUPPORTED_ALGORITHM = ('tfidf', 'word2vec', 'doc2vec')
DEFAULT_ALGORITHM = 'tfidf'
_LIST_COMMAND = """ \
mulai - Mulai menggunakan bot
tanya - Bertanya tentang konstitusi
pengaturan - Mengatur algoritma
bantuan - Menampilkan bantuan
"""
def start(update, context):
welcome = '''\
Selamat datang di Tanya Konstitusi!
Tentang:
Bot ini akan membantu anda menjawab pertanyaan tentang konstitusi Indonesia.
Perintah tersedia:
/mulai - manampilkan pesan ini.
/tanya - bertanya tentang konstitusi.
/pengaturan - mengatur algoritma.
/bantuan - untuk menampilkan bantuan.'''
context.bot.send_message(
chat_id=update.effective_chat.id,
text=dedent(welcome),
reply_markup=ReplyKeyboardRemove())
def help(update: Update, context: CallbackContext):
help_message = '''\
Bantuan
Bot ini akan membantu anda menjawab pertanyaan tentang konstitusi Indonesia.
Perintah tersedia:
/mulai - manampilkan pesan ini.
/tanya - bertanya tentang konstitusi.
/pengaturan - mengatur algoritma.
/bantuan - untuk menampilkan bantuan.'''
context.bot.send_message(
chat_id=update.effective_chat.id,
text=dedent(help_message),
reply_markup=ReplyKeyboardRemove()
)
def verify_question(question: str):
if ENABLE_PROOFING:
p = Proofing()
return p.check_words(question)
else:
return question
def start_ask(update: Update, context: CallbackContext) -> int:
message_list = update.message.text.split(' ')
if len(message_list) > 1:
query = ' '.join(message_list[1:])
return get_answer(query, update, context)
response = '''\
Silahkan kirimkan pertanyaan anda!
Gunakan perintah /batal untuk membatalkan.'''
update.message.reply_text(
text=dedent(response),
reply_markup=ReplyKeyboardRemove())
return ASK_STATE
def cancel_ask(update: Update, context: CallbackContext) -> int:
update.message.reply_text(
'Perintah dibatalkan.',
reply_markup=ReplyKeyboardRemove())
return ConversationHandler.END
def ask(update: Update, context: CallbackContext) -> int:
query = update.message.text
return get_answer(query, update, context)
def get_answer(query: str, update: Update, context: CallbackContext) -> int:
try:
processed_query = verify_question(query)
except ValueError as e:
update.message.reply_text(e.args[0])
update.message.reply_text('Perintah /tanya dibatalkan.')
return ConversationHandler.END
algorithm = DEFAULT_ALGORITHM
if 'algorithm' in context.user_data \
and context.user_data['algorithm'] in SUPPORTED_ALGORITHM:
algorithm = context.user_data['algorithm']
try:
r = requests.get(f'http://{QA_HOST}:{QA_PORT}/{algorithm}',
params={'q': processed_query, 'num_rank': NUM_RANK})
if r.status_code != 200:
raise Exception()
except:
update.message.reply_text(
'Sistem sedang gangguan, silahkan coba lagi nanti')
return ConversationHandler.END
answers = r.json()['data']['answer']
response = [f'Pertanyaan: \n{query}']
for i, answer in enumerate(answers):
response.append(f'Jawaban {i + 1}:\n{answer}')
response.append(f'Algoritma digunakan: {algorithm}')
update.message.reply_text('\n\n'.join(response))
return ConversationHandler.END
def start_setting(update: Update, context: CallbackContext) -> int:
message_list = update.message.text.split(' ')
if len(message_list) == 2 and message_list[1] in SUPPORTED_ALGORITHM:
algorithm = message_list[1]
return set_algorithm(algorithm, update, context)
reply_keyboard = [SUPPORTED_ALGORITHM, ['/batal']]
response = '''\
Pilih algoritma untuk Question Answering!
Gunakan perintah /batal untuk membatalkan.'''
update.message.reply_text(
dedent(response),
reply_markup=ReplyKeyboardMarkup(
reply_keyboard, one_time_keyboard=True, input_field_placeholder='Algoritma?'
))
return SETTING_STATE
def setting(update: Update, context: CallbackContext) -> int:
text = update.message.text.lower()
if text not in SUPPORTED_ALGORITHM:
reply_keyboard = [SUPPORTED_ALGORITHM]
response = '''\
Algoritma tidak diketahui, silahkan pilih ulang!
Gunakan perintah /batal untuk membatalkan.'''
update.message.reply_text(
dedent(response),
reply_markup=ReplyKeyboardMarkup(
reply_keyboard, one_time_keyboard=True, input_field_placeholder='Algoritma?'
))
return SETTING_STATE
else:
return set_algorithm(text, update, context)
def set_algorithm(algorithm: str, update: Update, context: CallbackContext) -> int:
context.user_data['algorithm'] = algorithm
response = f'''\
Algoritma berhasil diubah ke {algorithm}
'''
update.message.reply_text(
dedent(response),
reply_markup=ReplyKeyboardRemove())
return ConversationHandler.END
def error(update, context):
logger.warning(f'Update: {update}, Error: {context.error}')
def main():
Path(DATA_DIR).mkdir(parents=True, exist_ok=True)
persistence = PicklePersistence(filename='.cache/data/bot')
updater = Updater(BOT_TOKEN, persistence=persistence)
dispatcher = updater.dispatcher
start_handler = CommandHandler(['start', 'mulai'], start)
help_handler = CommandHandler(['help', 'bantuan'], help)
ask_handler = ConversationHandler(
entry_points=[CommandHandler(['ask', 'tanya'], start_ask)],
states={
ASK_STATE: [MessageHandler(Filters.text & ~Filters.command, ask)],
},
fallbacks=[CommandHandler(['cancel', 'batal'], cancel_ask)],
)
setting_handler = ConversationHandler(
entry_points=[CommandHandler(
['setting', 'pengaturan'], start_setting)],
states={
SETTING_STATE: [MessageHandler(Filters.text & ~Filters.command, setting)],
},
fallbacks=[CommandHandler(['cancel', 'batal'], cancel_ask)],
)
dispatcher.add_handler(start_handler)
dispatcher.add_handler(help_handler)
dispatcher.add_handler(ask_handler)
dispatcher.add_handler(setting_handler)
dispatcher.add_error_handler(error)
updater.start_polling()
updater.idle()
if __name__ == "__main__":
main()