-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
170 lines (136 loc) · 5.95 KB
/
app.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
from flask import Flask, request
from classes.teleBot import TeleBot
from classes.Db import Db
from classes.models.new_expense import NewExpense
import re
import logging
from telegram import ReplyKeyboardMarkup, ReplyKeyboardRemove, Update
from telegram.ext import (
Updater,
CommandHandler,
MessageHandler,
Filters,
ConversationHandler,
CallbackContext,
)
logging.basicConfig(format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
level=logging.INFO)
logger = logging.getLogger(__name__)
ACTION_PARSE, NEW_TRANSACTION, AMOUNT, BUSINESS, PRODUCT, PAYMENT_METHOD, PAYMENTS, END_TRANSACTION = range(8)
def start(update: Update, _: CallbackContext) -> int:
reply_keyboard = [['new', 'report']]
update.message.reply_text("""
Hi, I am budgetBot, testing purposes, \n
What do ya want, ya twat?
""", reply_markup=ReplyKeyboardMarkup(reply_keyboard))
return ACTION_PARSE
def parse_action(update:Update, _: CallbackContext) ->int:
action = update.message.text.lower()
logger.info(f"action: {action}, context: {_}\n\n update:{update.effective_user}")
user_id = update.effective_user.id
logger.info(f"action: {action}, context: {_}\n\n user_id:{user_id}")
if action == 'report':
pass
elif action == 'new':
expense_model = NewExpense()
expense_model.set_default_values()
expense_model.set_user_id(user_id)
return NEW_TRANSACTION
elif action == 'commit':
return END_TRANSACTION
def cancel(update: Update, _: CallbackContext) -> int:
user = update.message.chat.username
logger.info("User %s canceled the conversation.", user)
update.message.reply_text(
'Bye! I hope we can talk again some day.', reply_markup=ReplyKeyboardRemove()
)
return ConversationHandler.END
def new_transaction(update: Update, _: CallbackContext) -> int:
update.message.reply_text("""
ok, what's the name of the place what robbed yo ass? \n
""")
return BUSINESS
def set_business_name(update: Update, _: CallbackContext) -> int:
business = update.message.text
logger.info("Business name is: %s ", business)
expense_model = NewExpense()
expense_model.set_business_name(business)
update.message.reply_text("""
And what did you buy? \n
""")
return PRODUCT
def set_product(update: Update, _: CallbackContext) -> int:
product = update.message.text
expense_model = NewExpense()
expense_model.set_product(product)
logger.info("Product name is: %s ", product)
update.message.reply_text("""
how much did we part with? \n
""")
return AMOUNT
def set_amount(update: Update, _: CallbackContext) -> int:
amount = update.message.text
logger.info("amount name is: %s ", amount)
expense_model = NewExpense()
expense_model.set_amount(amount)
update.message.reply_text("""
how many payments? \n
""")
return PAYMENTS
def set_payments(update: Update, _: CallbackContext) -> int:
payments = update.message.text
expense_model = NewExpense()
expense_model.set_payments(payments)
methods_arr = [["cash", "credit", "paypal", "check" ,"crypto"]]
logger.info("payments: %s ", payments)
update.message.reply_text("""
what method did you use? \n
""", reply_markup=ReplyKeyboardMarkup(methods_arr, one_time_keyboard=True))
return PAYMENT_METHOD
def set_method(update: Update, _: CallbackContext) -> int:
method = update.message.text
logger.info("method: %s ", method)
expense_model = NewExpense()
expense_model.set_payment_method(method)
expanse_dict = expense_model.to_dict()
expanse_string = "\n".join(["{}: {}".format(key, val) for key, val in expanse_dict.items()])
update.message.reply_text(f"""
Thank You! \n
your transaction lookes like this: \n
{expanse_string} \n
if you want to commit this type 'commit'
else type 'new'
""")
return ACTION_PARSE
def end_transaction(update: Update, _: CallbackContext) -> int:
Database = Db()
expense_model = NewExpense()
if expense_model.product == '':
update.message.reply_text(f"""
not enough data to commit, \n
please start again by typing 'new'
""")
return ACTION_PARSE
Database.insert(tb_name = "tb_expenses", insert_model=expense_model)
pass
def main() -> None:
bot = TeleBot()
conv_handler = ConversationHandler(
entry_points = [CommandHandler('start', start)],
states = {
ACTION_PARSE: [MessageHandler(Filters.text, parse_action)],
NEW_TRANSACTION: [MessageHandler(Filters.text & ~Filters.command, new_transaction)],
BUSINESS: [MessageHandler(Filters.text & ~Filters.command, set_business_name)],
PRODUCT: [MessageHandler(Filters.text & ~Filters.command, set_product)],
AMOUNT: [MessageHandler(Filters.text & ~Filters.command, set_amount)],
PAYMENTS: [MessageHandler(Filters.text & ~Filters.command, set_payments)],
PAYMENT_METHOD: [MessageHandler(Filters.text & ~Filters.command, set_method)],
END_TRANSACTION: [MessageHandler(Filters.text & ~Filters.command, end_transaction)],
}
, fallbacks = [CommandHandler('cancel', cancel)])
bot.add_handler(conv_handler)
bot.init()
bot.idle()
print(bot)
if __name__ == '__main__':
main()