Repository navigation
Expand file tree
/
Copy pathtelegram_bot.py
More file actions
258 lines (197 loc) · 9.04 KB
/
Copy pathtelegram_bot.py
File metadata and controls
258 lines (197 loc) · 9.04 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
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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
import atexit
import logging
from typing import Optional
from algosdk.encoding import is_valid_address
from telegram import Update
from telegram.constants import ParseMode
from telegram.ext import CallbackContext, CommandHandler
from bot.background import start_bg_tasks
from bot.context import app_context
from bot.db.model import BotUser
from bot.db.users import bot_users, create_user, get_user_by_tg
from bot.env import FEEDBACK_COMMAND, MESSAGE_ALL_COMMAND, SUPPORT_COMMAND, bot_settings
from bot.formatting import format_user_pool
from bot.log import LOG_DATE_FORMAT, LOG_FORMAT
from bot.phrase_manager import Phrases
from core.db.cometa_users import cometa_users, filter_compoundable_pools, filter_ended_pools, filter_no_action_pools
async def start(update: Update, context: CallbackContext):
await update.message.reply_html(
f"🤖 I'm glad to see you, {update.message.from_user.name}, let's be friends!👀\n\n"
"I will try to make your DeFi experience on Cometa as easy as possible😉\n\n"
"Please register first with:\n"
"<code>/register YOUR_ALGO_ADDRESS</code>\n\n"
"To know all the cool stuff I can do use:\n"
"/help"
)
async def check_registration(update: Update) -> Optional[BotUser]:
tg_user = update.message.from_user
user = get_user_by_tg(tg_user.id)
if user is None:
await update.message.reply_html(
"🤖 Please register first with:\n"
"<code>/register YOUR_ALGO_ADDRESS</code>\n\n"
"To know more about me please use:\n"
"/help"
)
return None
return user
async def show_pools(update: Update, context: CallbackContext):
user = await check_registration(update)
if user is None:
return
pools = cometa_users.get_by_primary_key(user.algo_address).pools
if pools:
reply_text = f"🤖 <i>{Phrases.check_pools()}</i>\n"
compound_pools = filter_compoundable_pools(pools)
if compound_pools:
reply_text += "\n\n✅ <b>Need compounding:</b>\n\n"
reply_text += "\n".join([format_user_pool(pool) for pool in compound_pools])
ended_pools = filter_ended_pools(pools)
if ended_pools:
reply_text += "\n\n❌ <b>Need withdraw:</b>\n\n"
reply_text += "\n".join([format_user_pool(pool) for pool in ended_pools])
no_action_pools = filter_no_action_pools(pools)
if no_action_pools:
reply_text += "\n\n💎 <b>No action needed:</b>\n\n"
reply_text += "\n".join([format_user_pool(pool) for pool in no_action_pools])
reply_text += "\n\n<i>Manage at https://app.cometa.farm/</i>"
else:
reply_text = (
"🤖 You don't have any pools, that's strange..."
"\n\n"
"Check out https://app.cometa.farm/ to get decent APRs with the best UX on Algorand😏"
)
await update.message.reply_html(reply_text)
async def track_address(update: Update, context: CallbackContext):
tg_user = update.message.from_user
if not context.args:
await update.message.reply_text("🤖 Please provide address!")
return
address = context.args[0]
if not is_valid_address(address):
await update.message.reply_text(f"🤖 Oh no... Please {tg_user.name}! Provide your Algorand address👆")
return
user = get_user_by_tg(tg_user.id)
if user is None:
create_user(address, tg_user.id)
new_user_msg = f"New #user {tg_user.name}!\nAlgo address {address}"
await context.bot.send_message(bot_settings.feedback_chat_id, new_user_msg)
else:
user.pools = []
user.algo_address = address
await update.message.reply_html(f"🤖 Great, {tg_user.name}!\nTracking <code>{address}</code>.")
await show_pools(update, context)
async def get_feedback(update: Update, context: CallbackContext):
if not context.args:
await update.message.reply_text("🤖 Please provide the feedback!")
return
tg_user = update.message.from_user
text_title = f"New #feedback from {tg_user.name}"
logging.info(text_title)
feedback_text = update.message.text_markdown[len(FEEDBACK_COMMAND) + 2 :]
feedback = f"{text_title}:\n\n{feedback_text}"
await context.bot.send_message(bot_settings.feedback_chat_id, feedback)
await update.message.reply_text(f"🤖 Thank you, {tg_user.name}, your feedback is submitted!❤")
async def get_support(update: Update, context: CallbackContext):
if not context.args:
await update.message.reply_text("🤖 Please describe your problem!")
return
tg_user = update.message.from_user
text_title = f"New #ticket from {tg_user.name}"
logging.info(text_title)
support_text = update.message.text_html[len(SUPPORT_COMMAND) + 2 :]
support = f"{text_title}:\n\n{support_text}"
await context.bot.send_message(bot_settings.support_chat_id, support, parse_mode=ParseMode.HTML)
await update.message.reply_text(f"🤖 Thank you, {tg_user.name}, one of our admins will contact you ASAP!❤")
async def message_all(update: Update, context: CallbackContext):
tg_user = update.message.from_user
if tg_user.id not in bot_settings.telegram_admin_ids:
logging.warning("Rejected message_all command from Telegram user %s", tg_user.id)
await update.message.reply_text("🤖 This command is restricted to administrators.")
return
if not context.args:
await update.message.reply_text("🤖 Provide the message!")
return
text = update.message.text_markdown[len(MESSAGE_ALL_COMMAND) + 2 :]
users = bot_users.get_all()
for user in users:
try:
await context.bot.send_message(user.telegram_id, text)
except Exception as e:
logging.error(f"Failed to send message to {user.telegram_id}: {e}")
await update.message.reply_text(f"🤖 I messaged all {len(users)} users!")
async def register(update: Update, context: CallbackContext):
tg_user = update.message.from_user
user = get_user_by_tg(tg_user.id)
if user is not None:
await update.message.reply_html(
f"🤖 You are already registered!\n"
f"For now I am tracking <code>{user.algo_address}</code> for you😉"
f"To know more about me please use:\n"
f"/help"
)
return
logging.info(f"Registering {tg_user.name}.")
await track_address(update, context)
async def change_address(update: Update, context: CallbackContext):
if await check_registration(update) is None:
return
await track_address(update, context)
async def show_help(update: Update, context: CallbackContext):
text = (
f"🤖 <i>Hello, {update.message.from_user.name}, it is a pleasure to assist you!</i>☺️"
f"\n\n"
f"✅ I will remind you <b>to withdraw from ended pools</b>."
f"\n\n"
f"✅ I will remind you <b>to compound</b> in pools where you farmed <b>more than 1% of your stake</b> "
f"(to get decent APY)."
f"\n\n"
f"<i>I will notify only once per day. "
f"Soon you will be able to manage the frequency as well as best compounding interest!</i>"
f"\n\n"
f"<i>What else can I do?</i>😏"
f"\n\n"
f"✏️To change the address to track:"
f"\n"
f"<code>/change_address NEW_ADDRESS</code>"
f"\n\n"
f"🚀To show your current Cometa pools:"
f"\n"
f"/my_pools"
f"\n\n"
f"💁♀️To share any feedback about Cometa:"
f"\n"
f"<code>/feedback YOUR_FEEDBACK</code>"
f"\n\n"
f"<b>🚨 If you have any problems with bot or Cometa</b>, please describe it and <b>our team will contact you ASAP</b>:"
f"\n"
f"<code>/support DESCRIPTION</code>"
f"\n\n"
f"<i>And don't forget to farm</i>😉\n\n"
f"https://app.cometa.farm/"
)
await update.message.reply_html(text, disable_web_page_preview=True)
def start_bot():
app_context.application.add_handler(CommandHandler("start", start))
app_context.application.add_handler(CommandHandler("register", register))
app_context.application.add_handler(CommandHandler("change_address", change_address))
app_context.application.add_handler(CommandHandler("my_pools", show_pools))
app_context.application.add_handler(CommandHandler(FEEDBACK_COMMAND, get_feedback))
app_context.application.add_handler(CommandHandler(SUPPORT_COMMAND, get_support))
app_context.application.add_handler(CommandHandler("help", show_help))
# Admin
app_context.application.add_handler(CommandHandler(MESSAGE_ALL_COMMAND, message_all))
app_context.application.run_polling()
logging.info("Bot started!")
def tear_down():
logging.info("EXIT BOT\n\nBye!\n")
def setup_logging():
logging.basicConfig(format=LOG_FORMAT, datefmt=LOG_DATE_FORMAT, level=bot_settings.logging_level)
setup_logging()
if __name__ == "__main__":
atexit.register(tear_down)
try:
with start_bg_tasks():
start_bot()
except Exception as ex:
logging.exception(ex)