-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
240 lines (204 loc) · 7.82 KB
/
main.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
233
234
235
236
237
238
239
240
import telebot
import os
import parse
from user_info import UserInfo
from stocking_info import StockingFetcher
try:
with open("bot.cfg") as f:
bot_token = list(f.readline().split())[0]
except:
bot_token = os.environ.get("bot_token")
print()
userdb = UserInfo()
stocking_fetcher = StockingFetcher()
with open("help.info", "r") as hlp_source:
hlp = "".join(hlp_source.readlines())
bot = telebot.TeleBot(bot_token, parse_mode=None)
@bot.message_handler(commands=["help"])
def send_help(message):
bot.reply_to(message, hlp)
@bot.message_handler(commands=["ping"])
def send_ping(message):
bot.reply_to(message, "pong")
@bot.message_handler(commands=["start"])
def send_start_message(message):
bot.reply_to(message, f"Nice to see you, @{message.from_user.username}")
if not userdb.user(message.from_user.id).exists:
userdb.create_user(message.from_user.id, message.from_user.username)
bot.reply_to(message, "First time? amybe you need /help?")
else:
bot.reply_to(message, "Welcome back")
@bot.message_handler(commands=["about"])
def send_about(message):
print(message.text)
symbol = parse.parse_symbol(message.text)
if symbol is not None:
print(symbol)
ticker = stocking_fetcher.fetch(symbol)
if ticker.exists:
bot.reply_to(message, ticker.about())
else:
bot.reply_to(message, f"Can't find symbol {symbol}.")
else:
bot.reply_to(
message, "Can`t find this symbol, print something like '\\info $TSLA'."
)
@bot.message_handler(commands=["buy"])
def buy(message):
user = userdb.user(message.from_user.id)
if not user.exists:
bot.reply_to(message, "Please log in using /start")
else:
symbol = parse.parse_symbol(message.text)
money = parse.parse_sum(message.text)
if symbol is not None and money is not None:
ticker = stocking_fetcher.fetch(symbol)
if ticker.exists:
bot.reply_to(message, user.buy(ticker, money))
else:
bot.reply_to(message, "This symbol doesn't exist")
else:
bot.reply_to(
message,
"Please follow template '/buy $SYMBOL 0.00USD'. You may add extra symbols.",
)
@bot.message_handler(commands=["sell"])
def sell(message):
user = userdb.user(message.from_user.id)
if not user.exists:
bot.reply_to(message, "Please log in using /start")
else:
symbol = parse.parse_symbol(message.text)
money = parse.parse_sum(message.text)
if symbol is not None and money is not None:
ticker = stocking_fetcher.fetch(symbol)
if ticker.exists:
bot.reply_to(message, user.sell(ticker, money))
else:
bot.reply_to(message, "This symbol doesn't exist")
else:
bot.reply_to(
message,
"Please follow template '/sell $SYMBOL 0.00USD'. You may add extra symbols.",
)
@bot.message_handler(commands=["short"])
def short(message):
user = userdb.user(message.from_user.id)
if not user.exists:
bot.reply_to(message, "Please log in using /start")
else:
symbol = parse.parse_symbol(message.text)
money = parse.parse_sum(message.text)
if symbol is not None and money is not None:
ticker = stocking_fetcher.fetch(symbol)
if ticker.exists:
bot.reply_to(message, user.short(ticker, money))
else:
bot.reply_to(message, "This symbol doesn't exist")
else:
bot.reply_to(
message,
"Please follow template '/short $SYMBOL 0.00USD'. You may add extra symbols.",
)
@bot.message_handler(commands=["close"])
def close(message):
user = userdb.user(message.from_user.id)
if not user.exists:
bot.reply_to(message, "Please log in using /start")
else:
symbol = parse.parse_symbol(message.text)
if symbol is not None:
ticker = stocking_fetcher.fetch(symbol)
if ticker.exists:
bot.reply_to(message, user.close(ticker))
else:
bot.reply_to(message, "This symbol doesn't exist")
else:
bot.reply_to(
message,
"Please follow template '/close $SYMBOL '. You may add extra symbols.",
)
@bot.message_handler(commands=["closeshort"])
def closeshort(message):
user = userdb.user(message.from_user.id)
if not user.exists:
bot.reply_to(message, "Please log in using /start")
else:
symbol = parse.parse_symbol(message.text)
if symbol is not None:
ticker = stocking_fetcher.fetch(symbol)
if ticker.exists:
bot.reply_to(message, user.closeshort(ticker))
else:
bot.reply_to(message, "This symbol doesn't exist")
else:
bot.reply_to(
message,
"Please follow template '/close $SYMBOL '. You may add extra symbols.",
)
@bot.message_handler(commands=["money"])
def send_money(message):
user = userdb.user(message.from_user.id)
if not user.exists:
bot.reply_to(message, "Please log in using /start")
else:
bot.reply_to(message, f"Your cash is {round(user.money(), 2)}USD")
@bot.message_handler(commands=["reset"])
def reset(message):
user = userdb.user(message.from_user.id)
if not user.exists:
bot.reply_to(message, "Please log in using /start")
else:
money = parse.parse_sum(message.text)
if money is not None and money <= 1000:
user.reset(new_sum=money)
else:
bot.reply_to(
message,
"Please follow template '/reset 0.00USD'. (Less than 1000 USD) You may add extra symbols.",
)
@bot.message_handler(commands=["open"])
def send_open(message):
user = userdb.user(message.from_user.id)
if not user.exists:
bot.reply_to(message, "Please log in using /start")
else:
positions = user.open()
answer = "your open positions are:\n\n\n"
total = 0
for position in positions:
answer += f"${position[0]} --- {round(position[1], 2)} = {round(position[1] * stocking_fetcher.fetch(position[0]).price(), 2)}USD\n\n"
total += position[1] * stocking_fetcher.fetch(position[0]).price()
answer += f"\n\nTotal: {round(total, 2)}USD\nCash: {round(user.money(), 2)}USD"
bot.reply_to(
message,
answer
)
@bot.message_handler(commands=["shorts"])
def send_shorts(message):
user = userdb.user(message.from_user.id)
if not user.exists:
bot.reply_to(message, "Please log in using /start")
else:
positions = user.shorts()
answer = "your open shorts are:\n\n\n"
total = 0
for position in positions:
answer += f"${position[0]} --- {round(position[1], 2)} = {round(position[1] * stocking_fetcher.fetch(position[0]).price(), 2)}USD\n\n"
total += position[1] * stocking_fetcher.fetch(position[0]).price()
answer += f"\n\nTotal: {round(total, 2)}USD"
bot.reply_to(
message,
answer
)
@bot.message_handler(commands=["table"])
def send_start_message(message):
contest = userdb.everybody()
answer = "your position is marked with (!)\n"
for pos, user in enumerate(contest):
answer += f"{'(!)' if user.info[0] == message.from_user.id else ''}{pos + 1}) {user.username()} - {round(user.contest_money(), 2)}$\n"
bot.reply_to(message, answer)
@bot.message_handler(func=lambda m: True)
def send_unknown_message(message):
bot.reply_to(message, f"Sorry, I can't understand you")
bot.infinity_polling()