项目亮点速览
借助 加密货币价格提醒 的开源脚本,无需服务器即可在 Telegram 收到「ETH 破 4,000 美金」或「BTC 跌破 60,000 美金」的即刻通知。整个架构运行于 AWS Lambda,配合 node-telegram-bot-api 与 Terraform,十分钟即可完成部署。👇 查看完整思路,可立即借鉴到你自己的交易系统。
为什么你需要个性化价格通知
手动刷新行情?熬夜盯盘?只要设定一次「价格提醒系统」:
- 省心:把精力放在交易策略而不是刷新键。
- 省钱:Lambda 免费月度 100 万次调用,薅云厂商“羊毛”。
- 省时:当价格突破关键位立刻 Telegram「叮」一声通知,不必再蹲屏幕前。
价格提醒的数据模型
在 DynamoDB 中一条提醒仅需 5 个字段,既简洁又避免重复通知:
| 字段 | 说明 |
|---|---|
id | 唯一主键,便于更新/删除 |
asset | 资产代码,如 "ethereum" |
targetPrice | 触发阈值 |
condition | "more"(大于)或 "less"(小于) |
isNotified | 标记位。已提醒为 true,循环可用 |
TypeScript 定义示例:
type PriceAlertCondition = 'more' | 'less'
export interface PriceAlert extends EntityWithId {
asset: string
targetPrice: number
condition: PriceAlertCondition
isNotified: boolean
}用脚本批量管理提醒:setPriceAlert.ts
const rules = [
{ asset: 'ethereum', targetPrice: 3800, condition: 'less' as const },
{ asset: 'ethereum', targetPrice: 4000, condition: 'more' as const }
]
const priceAlerts: PriceAlert[] = rules.map((r, i) => ({
...r,
id: i.toString(),
isNotified: false,
}))
await deleteAllPriceAlerts()
await Promise.all(priceAlerts.map(putPriceAlert))执行 ts-node setPriceAlert.ts 一步到位——先清空旧规则,再写入新规则。
从 CoinGecko 拉取实时行情
getAssetPrices 有几行代码即可并发查询多币种:
const url = addQueryParams(baseUrl, {
ids: ids.join(','),
vs_currencies: fiatCurrency,
})
const result = await queryUrl(url)
return recordMap(result, (v) => v[fiatCurrency])recordMap 把嵌套数据结构拍平成 Record<string, number>,方便后续对比阈值。
用 Telegram Bot 发送消息
只需三步:
- 在 @BotFather 创建一个机器人
- 把
TELEGRAM_BOT_TOKEN写入环境变量 - 函数收到价格触发条件后调用:
await bot.sendMessage(
process.env.TELEGRAM_BOT_CHAT_ID,
`🚨 ${asset} 当前价格 ${price} 已满足条件!`
)整个推送链路不到 200 ms,消息即达手机。
核心流程:runPriceWatcher
逻辑一目了然:
- 读取所有未触发过的
PriceAlert→ 2. 调用 CoinGecko 查最新价 → 3. 判断条件若匹配则
a) 推送 Telegram 通知
b) 把isNotified置true,避免重复打扰
const isConditionMet = match(condition, {
more: () => price > targetPrice,
less: () => price < targetPrice
})
if (isConditionMet && !isNotified) {
await sendPriceChangeAlert({ price, asset })
await updatePriceAlert(id, { isNotified: true })
}Terraform 资源一次性拉起
main.tf 含三项基础资源:
- Lambda 函数:指向打包后 zip
- CloudWatch Events:规则每 10 分钟触发一次
- IAM 权限:允许 Lambda 访问 DynamoDB 与 CloudWatch
仅需 terraform init && terraform apply,十分钟构建整条链路。
FAQ:常见疑问一站式解答
Q1:我可以监控其他资产吗?
A:只需在 rules 数组中加入新的 asset ID,如 "bitcoin",系统会自动拉取对应价格。
Q2:免费额度会不会超?
AWS Lambda 每月 100 万次调用 + CloudWatch Events 无额外费用;即使每天跑 144 次,也绰绰有余。
Q3:如何避免涨跌震荡导致重复提醒?
已设定 isNotified,当价格回落再 crosses back 则把 flag 重置为 false,进退皆可。
Q4:能否支持多个用户?
把 TELEGRAM_BOT_CHAT_ID 改为数组并存储在 PriceAlert 即可,每条提醒针对不同用户推送。
Q5:如何本地调试?
在 .env 写入所需环境变量后,用 ts-node runPriceWatcher.ts 本地运行。
Q6:CoinGecko 限速怎么办?
我们的代码一分钟只查一次以上列表,API 无需 key 也足够稳定;如需高频可配置 Pro key 或替换为其他提供商。
安全与环境变量
export const getEnvVar = (name: VariableName): string => {
const value = process.env[name]
if (!value) throw new Error(`Missing ${name}`)
return value
}一处配置,全局调用,避免漏配导致 Slate 抛错。同时用 Sentry 对整体 Lambda 报错兜底,第一时间收到邮件告警。
扩展方向
- 多币种提醒 —— 把
rules设为任意 list,一键适配所有主流币。 - 百分比告警 —— 计算 24 h 涨跌幅并设置阈值。
- 图表快照 —— 调用 TradingView 轻量 Widget,将 K 线图与报警信息一并推送。
- Web UI Dashboard —— 用 AWS DynamoDB Streams + API Gateway 实现实时图形化管理。
感谢阅读,只需半小时即可拥有专属 加密货币价格提醒系统。祝你交易顺利,价格跳动永远第一时间掌握!