Building a Simple Stock Monitoring Bot with Feishu and Python
This tutorial explains how to create a Python‑based stock monitoring bot that periodically fetches real‑time prices, compares them with preset thresholds, and sends alerts to a Feishu group via a custom webhook, including step‑by‑step setup and scheduling code.
Implementation Idea
Periodically obtain real‑time stock prices, compare them with predefined warning thresholds, and push a message to Feishu when the price crosses the threshold.
Development Steps
1. Create a group bot
Create a Feishu group, go to the group settings, and click the "Group Bot" menu. Add a new custom bot, fill in a name and description, and obtain a webhook URL.
Send messages to the webhook using the following Python function:
def send_to_feishu(content):
url = "https://open.feishu.cn/open-apis/bot/v2/hook/3c6d60b8-xxx" # actual webhook address
msg = {"msg_type": "text", "content": {"text": content}}
res = requests.post(url, json=msg)
print(res.json()) # print result for debugging2. Develop the stock monitor
Define a list of stocks, each with a code, minimum and maximum price. For each stock, fetch the latest price from Sina's API, compare it with the thresholds, and send a Feishu alert if needed.
# Get real‑time price
def get_price(code):
url = f'http://ifzq.gtimg.cn/appstock/app/kline/mkline?param={code},m1,,10'
print(url)
resp = requests.get(url).json()
if resp['code'] != 0:
return "", []
stock_name = resp['data'][code]['qt'][code][1]
latest_prices = resp['data'][code]["m1"][-1]
return stock_name, latest_prices
# Monitoring loop
def monitor():
stocks = [
{"code": "sh688068", "min": 230, "max": 239},
{"code": "sh000001", "min": 3530, "max": 3550}
]
for c in stocks:
print(f'check => {c["code"]}')
stock_name, prices = get_price(c["code"])
print(prices)
if not prices:
continue
if float(prices[3]) < c["min"]:
content = f'{stock_name}({c["code"]})当前价格:{prices[3]}<最低价({c["min"]})'
send_to_feishu(content)
if float(prices[2]) > c["max"]:
content = f'{stock_name}({c["code"]})当前价格:{prices[2]}>监控价({c["max"]})'
send_to_feishu(content)3. Implement the scheduled task
Use the third‑party schedule library to run the monitor every five minutes.
schedule.every(5).minutes.do(monitor)
while True:
schedule.run_pending()
time.sleep(1)The bot will now push Feishu notifications whenever a monitored stock price falls below the minimum or rises above the maximum threshold.
Finally, remember to like the article!
Python Programming Learning Circle
A global community of Chinese Python developers offering technical articles, columns, original video tutorials, and problem sets. Topics include web full‑stack development, web scraping, data analysis, natural language processing, image processing, machine learning, automated testing, DevOps automation, and big data.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.