forked from bolychevanton/Quotes-Telegram-Bot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtelegram_bot.py
162 lines (133 loc) · 5.16 KB
/
telegram_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
import argparse
from telegram.ext import Application, Updater, CommandHandler
from quoter import Quoter
import telegram
import asyncio
import numpy as np
import json
from plotter import Plot
class NumpyEncoder(json.JSONEncoder):
"""Special json encoder for numpy types"""
def default(self, obj):
if isinstance(obj, np.integer):
return int(obj)
elif isinstance(obj, np.floating):
return float(obj)
elif isinstance(obj, np.ndarray):
return obj.tolist()
return json.JSONEncoder.default(self, obj)
parser = argparse.ArgumentParser(
prog="Quotes Telegram Bot",
)
parser.add_argument("--bot-token", type=str, default=None, required=True)
parser.add_argument("--alerts-db", type=str, default=None, required=True)
parser.add_argument("--provider-uri", type=str, default=None, required=True)
args = parser.parse_args()
quoter = Quoter(args.provider_uri)
async def regname(update, context):
if len(context.args) != 2:
await update.message.reply_text("Error wrong input")
return
token_name, token_address = context.args
quoter.regname(token_name, token_address)
# YOUR CODE GOES HERE
await update.message.reply_text("Successfully registered the name for token")
async def start(update, context):
await update.message.reply_text(
'Wake the F"ck up, Samurai.\nWe have a crypto to earn.'
)
await update.message.reply_text(
f"Next commands are available in this bot:\n \
/start - Repeat all this bullshit for you again\n \
/regname <TOKEN NAME> <TOKEN ADDRESS> - Register token name for specified token adress\n \
/quote <FOREIGN TOKEN ADDRESS/NAME> <DOMESTIC TOKEN ADDRESS/NAME> [<BLOCK INDENTIFIER>] [<BLOCK IDENTIFIER>] [<DELTA BLOCKS>] - Get information about price of foreign token in domestic token\n \
/alert <FOREIGN TOKEN ADDRESS/NAME> <DOMESTIC TOKEN ADDRESS/NAME> <QUOTE LEVEL> - Get information when price will overcome threshold you have designated"
)
async def alert(update, context):
if len(context.args) != 3:
await update.message.reply_text("Error wrong input")
return
chat_id = update.message.chat_id
foreign, domestic, level = context.args
quoter.add_alert(foreign, domestic, level, chat_id)
# YOUR CODE GOES HERE
await update.message.reply_text("Successfully registered alert!!!!")
async def quote(update, context):
if len(context.args) < 2:
await update.message.reply_text("Error wrong input")
return
# returning current level
elif len(context.args) == 2:
foreign, domestic = context.args
block_start = quoter.w3.eth.block_number
block_end = int(block_start) + 1
step = 1
result = quoter.quote(
foreign, domestic, int(block_start), int(block_end), int(step)
)
msg = (
"Current level in block "
+ str(block_start)
+ " of "
+ foreign
+ " / "
+ domestic
+ " "
+ str(result[0])[0:10]
)
await update.message.reply_text(msg)
return
elif len(context.args) == 3:
foreign, domestic, block_start = context.args
block_end = int(block_start) + 1
step = 1
result = quoter.quote(
foreign, domestic, int(block_start), int(block_end), int(step)
)
msg = (
"Level in block "
+ str(block_start)
+ " of "
+ foreign
+ " / "
+ domestic
+ " "
+ str(result[0])[0:10]
)
await update.message.reply_text(msg)
return
elif len(context.args) == 4:
foreign, domestic, block_start, block_end = context.args
step = 1
elif len(context.args) == 5:
foreign, domestic, block_start, block_end, step = context.args
else:
await update.message.reply_text("AUCHTUNG!!!! TO MUCH ARGUMENTS")
return
await update.message.reply_text("Quote request registered. Wait for response.")
plotter = Plot() ###
print("STEPS AMOUNT = " + str((int(block_end) - int(block_start)) // int(step)))
if (int(block_end) - int(block_start)) // int(step) > 30:
step = str((int(block_end) - int(block_start)) // 30 + 1)
print("NEW STEP" + step)
result = quoter.quote(
foreign, domestic, int(block_start), int(block_end), int(step)
)
block_list = range(int(block_start), int(block_end), int(step))
data = [price for price in result]
plotter.draw_data(block_list, data)
await context.bot.send_photo(update.effective_chat.id, "quote_plot.png")
plotter.delete_picture()
# await update.message.reply_photo(dumped)
# for i in range(len(result)):
# await update.message.reply_text(float(result[i]))
def main():
app = Application.builder().token(args.bot_token).build()
# Adding commands
app.add_handler(CommandHandler("regname", regname))
app.add_handler(CommandHandler("alert", alert))
app.add_handler(CommandHandler("quote", quote))
app.add_handler(CommandHandler("start", start))
app.run_polling()
if __name__ == "__main__":
main()