-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBot.py
More file actions
170 lines (113 loc) · 5.03 KB
/
Bot.py
File metadata and controls
170 lines (113 loc) · 5.03 KB
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
import credentials as keys
import responses as rep
import os
from telegram.ext import *
# conversation handler status
CURRENCY = 0
VALUE = 1
HISTORY_CURRENCY = 0
CLEAN = 2
ERROR = 3
print("Bot initialized")
def start_command(update, context):
update.message.reply_text('Choose anything from available options: \n /list\n /exchange\n /history')
def help_command(update, context):
update.message.reply_text('No support available in this Bot')
def handle_message(update, context):
text = str(update.message.text).lower()
response = rep.default_response(text)
update.message.reply_text(response)
# def error(update, context):
# print(f'Update {update} caused error {context.error}')
def error(bot, update):
if not (context.error.message == "Message is not modified"):
logger.warning('Update "%s" caused error "%s"' % (update, context.error))
def list_command(update, context):
response = rep.list_response()
for key, value in response.items():
update.message.reply_text(key+" : {:.2f}".format(value))
def exchange_input_give_currency(update, context):
update.message.reply_text("Provide currency to exchange in format e.g.'CAD'...")
# write response from the user to the conversation handler [0]
return CURRENCY
# CURRENCY state
def exchange_input_amount_USD(update, context):
if update.message.text.upper() in rep.currency_base:
# a response that user give in first question
context.user_data['currency'] = update.message.text.upper()
update.message.reply_text("Give amount in USD...")
# write response from the user to the conversation handler [1]
return VALUE
else:
# wrong currency name...
# update.message.reply_text(f"Ups, there's no currency with that name {update.message.text.upper()}.\nPlease, select from the list: {", ".join.rep.currency_base}")
update.message.reply_text("Ups, there's no currency with that name")
return ERROR
# VALUE state
def exchange_command(update, context):
context.user_data['amount'] = update.message.text.upper()
# result of exchanging
response = rep.exchange_response(context.user_data['currency'], context.user_data['amount'])
update.message.reply_text("{:.2f}".format(response))
return CLEAN
def history_input_give_currency(update, context):
update.message.reply_text("Provide currency to see the history in format e.g.'CAD'...")
# write response from the user to the conversation handler [0]
return HISTORY_CURRENCY
def history_command(update, context):
if update.message.text.upper() in rep.currency_base:
currency = update.message.text.upper()
# contains the path to the image of a graph
response = rep.history_response(currency)
context.bot.sendPhoto(chat_id=update.message.chat_id, photo=open(response, 'rb'))
if os.path.exists(response):
os.remove(response)
else:
pass
return CLEAN
else:
update.message.reply_text("Ups, there's no currency with that name")
return ERROR
def clean(update, context):
context.user_data.clear()
return ConversationHandler.END
def stop(update, context):
update.message.reply_text("I hope the information was helpful")
return ConversationHandler.END
conversation_handler_exchange = ConversationHandler(
# start point
entry_points=[
CommandHandler('exchange', exchange_input_give_currency)],
# dict that will store the answers from the user
states = {
# read the answer for the first question (currency)
CURRENCY: [MessageHandler(Filters.text, exchange_input_amount_USD)],
# read the answer for the second question (USD amount to exchange)
VALUE: [MessageHandler(Filters.text, exchange_command)],
CLEAN: [MessageHandler(Filters.text, clean)],
ERROR: [MessageHandler(Filters.text, exchange_input_give_currency)]},
# exit point
fallbacks = [CommandHandler('stop', stop)]
)
conversation_handler_history = ConversationHandler(
entry_points=[
CommandHandler('history', history_input_give_currency)],
states = {
HISTORY_CURRENCY: [MessageHandler(Filters.text, history_command)],
CLEAN: [MessageHandler(Filters.text, clean)],
ERROR: [MessageHandler(Filters.text, history_input_give_currency)]},
fallbacks = [CommandHandler('stop', stop)]
)
def main():
updater = Updater(keys.BOT_TOKEN, use_context=True)
dp = updater.dispatcher
dp.add_handler(CommandHandler('start', start_command))
dp.add_handler(CommandHandler('list', list_command))
dp.add_handler(conversation_handler_exchange)
dp.add_handler(conversation_handler_history)
dp.add_handler(CommandHandler('help', help_command))
dp.add_handler(MessageHandler(Filters.text, handle_message))
dp.add_error_handler(error)
updater.start_polling() # check all the time for the message
updater.idle()
main()