-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
154 lines (124 loc) · 6.73 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
import atexit
import datetime
import logging
import requests
from apscheduler.schedulers.background import BackgroundScheduler
from telegram import Update
from telegram.ext import ApplicationBuilder, CommandHandler, ContextTypes
from commands import FundApi, get_daily_report, list_subscriptions_for_user, subscribe_user_fund, unsubscribe_user_fund
from config import load_config
from tasks import (
sync_send_daily_report_to_subscribers, sync_update_fund_details, sync_update_realtime_fund_details)
config = load_config("config.yml")
# Telegram bot配置
bot_config = config['telegram_bot']
TOKEN = bot_config['token']
BASE_URL = config["fund_api"]["base_url"]
logging.basicConfig(
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
level=logging.INFO
)
async def daily_report(update_ins: Update, context: ContextTypes.DEFAULT_TYPE):
user_id = update_ins.effective_user.id
message, _ = await get_daily_report(user_id, False)
await context.bot.send_message(chat_id=update_ins.effective_chat.id, text=message)
async def subscribe(update_ins: Update, context: ContextTypes.DEFAULT_TYPE):
if len(context.args) < 2:
await context.bot.send_message(chat_id=update_ins.effective_chat.id, text="请提供基金代码和购买份数。")
return
fund_code, shares = context.args
user_id = update_ins.effective_user.id
message = await subscribe_user_fund(user_id, fund_code, shares)
await context.bot.send_message(chat_id=update_ins.effective_chat.id, text=message)
async def search(update: Update, context: ContextTypes.DEFAULT_TYPE):
query = ' '.join(context.args)
if not query:
await context.bot.send_message(chat_id=update.effective_chat.id, text="请提供一个关键词进行搜索。")
return
# 使用FundApi类搜索基金
try:
funds = FundApi.search_funds(query)
matching_funds = funds["data"]
if not matching_funds:
await context.bot.send_message(chat_id=update.effective_chat.id, text="没有找到匹配的基金。")
return
# Get the codes of the matching funds
matching_codes = [fund[0] for fund in matching_funds]
today = datetime.datetime.today().strftime("%Y/%m/%d")
fund_details = FundApi.get_fund_details(matching_codes, start_date=today, end_date=today)
message = ""
for fund in fund_details:
message += f"名称:{fund['name']}\n"
message += f"代码:{fund['code']}\n"
message += f"类型:{fund['type']}\n"
message += f"净值:{fund['netWorth']}\n"
message += f"预期增长:{fund.get('expectGrowth', '0')}\n"
message += "---------------------\n"
# Send the message to the user
await context.bot.send_message(chat_id=update.effective_chat.id, text=message)
# 处理并发送消息...
except requests.RequestException as e:
# 处理API调用中的错误...
await update.message.reply_text(f"抱歉,搜索基金时出错:{str(e)}")
async def list_subscriptions(update: Update, context: ContextTypes.DEFAULT_TYPE):
user_id = update.effective_user.id
message = await list_subscriptions_for_user(user_id)
await context.bot.send_message(chat_id=update.effective_chat.id, text=message)
async def unsubscribe(update: Update, context: ContextTypes.DEFAULT_TYPE):
if not context.args:
await context.bot.send_message(chat_id=update.effective_chat.id, text="请提供要取消订阅的基金代码。")
return
fund_code = context.args[0]
user_id = update.effective_user.id
message = await unsubscribe_user_fund(user_id, fund_code)
await context.bot.send_message(chat_id=update.effective_chat.id, text=message)
async def help_command(update: Update, context: ContextTypes.DEFAULT_TYPE):
help_text = (
"/subscribe 或 /sub <fund_code> <shares> - 订阅一个基金并设置购买的份额。\n"
"/unsubscribe 或 /unsub <fund_code> - 取消订阅一个基金。\n"
"/list - 列出你当前订阅的所有基金。\n"
"/search 或 /s <keyword> - 使用关键字搜索基金。\n"
"/daily_report 或 /repo - 获取你订阅的基金的每日报告。\n"
"/help 或 /h - 显示这个帮助消息。"
)
await context.bot.send_message(chat_id=update.effective_chat.id, text=help_text)
async def start_command(update: Update, context: ContextTypes.DEFAULT_TYPE):
welcome_text = (
"欢迎使用我们的基金订阅Bot!🎉\n\n"
"你可以使用以下命令来与我互动:\n"
"/subscribe 或 /sub <fund_code> <shares> - 订阅一个基金并设置购买的份额。\n"
"/unsubscribe 或 /unsub <fund_code> - 取消订阅一个基金。\n"
"/list - 列出你当前订阅的所有基金。\n"
"/search 或 /s <keyword> - 使用关键字搜索基金。\n"
"/daily_report 或 / repo - 获取你订阅的基金的每日报告。\n"
"/help 或 /h - 显示帮助消息。\n\n"
"如果你有任何问题或建议,随时告诉我们!"
)
await context.bot.send_message(chat_id=update.effective_chat.id, text=welcome_text)
if __name__ == '__main__':
application = ApplicationBuilder().token(TOKEN).build()
search_handler = CommandHandler(['search', 's'], search)
subscribe_handler = CommandHandler(['subscribe', 'sub'], subscribe)
daily_report_handler = CommandHandler(['daily_report', 'repo'], daily_report)
list_subscriptions_handler = CommandHandler('list', list_subscriptions)
unsubscribe_handler = CommandHandler(['unsubscribe', 'unsub'], unsubscribe)
help_handler = CommandHandler(['help', 'h'], help_command)
start_handler = CommandHandler('start', start_command)
application.add_handler(start_handler)
application.add_handler(help_handler)
application.add_handler(search_handler)
application.add_handler(subscribe_handler)
application.add_handler(daily_report_handler)
application.add_handler(list_subscriptions_handler)
application.add_handler(unsubscribe_handler)
# 使用APScheduler来运行定时任务
scheduler = BackgroundScheduler()
# 添加一个定时任务,从早上9点到下午4点,每小时运行一次update_fund_details函数
scheduler.add_job(sync_update_fund_details, 'cron', day_of_week='mon-fri', hour='19-23', minute=0)
scheduler.add_job(sync_update_realtime_fund_details, 'cron', day_of_week='mon-fri', hour='9-16', minute=0)
# 添加一个定时任务,每天下午2点运行 send_daily_report_to_subscribers 函数
scheduler.add_job(sync_send_daily_report_to_subscribers, 'cron', hour=14, minute=00)
# 开始运行调度器
scheduler.start()
application.run_polling()
atexit.register(lambda: scheduler.shutdown())