mcpField
M
makefile-for-telegram

makefile-for-telegram

github.com/htlin222云与 DevOps7835 次安装3 周前更新
代码仓库

关于 makefile-for-telegram

为使用 Bun 的 Telegram Bot 项目生成 Makefile,包含初始化、开发、测试和 macOS LaunchAgent 服务命令,适用于配置本地及后台运行工作流。

主要功能

  1. 01生成 setup、run 和 dev 目标
  2. 02添加 LaunchAgent 启停命令
  3. 03包含状态和日志命令
  4. 04提供 install、test 和 typecheck 目标
  5. 05使用项目专属名称和路径

适用场景

  1. 01初始化 Telegram Bot 项目
  2. 02将 Bot 作为 macOS 后台服务运行
  3. 03添加 Bot 开发的快捷控制命令

安装

复制下面的命令,在终端中运行。

$npx skills add https://github.com/htlin222/dotfiles --skill makefile-for-telegram

安装后在 Claude Code 中输入/makefile-for-telegram确认已加载。

详细介绍

Telegram Bot 的 Makefile

为在 macOS 上运行的 Telegram bot 项目生成可用于生产环境的 Makefile,并支持 LaunchAgent 后台服务。

适用场景

  • 设置新的 Telegram bot 项目
  • 添加 macOS LaunchAgent 服务管理
  • 需要快速启动、停止和重启 bot 的命令
  • 想要将 bot 作为后台服务管理

生成的功能

快速开始
命令说明
make setup首次设置(安装依赖、创建 .env)
make run在前台运行 bot
make dev以自动重新加载模式运行(监视模式)
后台服务(macOS LaunchAgent)
命令说明
make start安装并启动后台服务
make stop停止后台服务
make restart重启后台服务
make status检查服务是否正在运行
make logs持续查看 stdout 日志
make logs-err持续查看 stderr 日志
make uninstall移除后台服务
开发
命令说明
make install安装依赖
make test运行测试
make typecheck运行 TypeScript 类型检查
make clean删除临时文件和日志

要求

生成之前,请确保项目包含:

  1. LaunchAgent plist 模板,路径为 launchagent/com.{project-name}.plist.template
  2. 环境文件模板,路径为 .env.example
  3. Bun 作为运行时(或针对 npm/node 进行修改)

说明

第 1 步:收集项目信息

询问用户:

  • 项目名称(用于 plist 标签,例如 claude-telegram-ts)
  • 运行时命令(默认值:bun run src/index.ts)
  • 日志文件路径(默认值:/tmp/{project-name}.log)
第 2 步:生成 Makefile

创建 Makefile,其中包含:

# {Project Name} - Makefile
# Usage: make <target>

SHELL := /bin/bash

PLIST_NAME := com.{project-name}
PLIST_PATH := ~/Library/LaunchAgents/$(PLIST_NAME).plist
PLIST_TEMPLATE := launchagent/$(PLIST_NAME).plist.template
LOG_FILE := /tmp/{project-name}.log
ERR_FILE := /tmp/{project-name}.err

.PHONY: help install setup run dev stop start restart status logs logs-err clean typecheck test uninstall

help:
	@echo "{Project Name}"
	@echo ""
	@echo "Quick start:"
	@echo "  make setup      - First-time setup (install deps, create .env)"
	@echo "  make run        - Run bot in foreground"
	@echo "  make dev        - Run with auto-reload (watch mode)"
	@echo ""
	@echo "Background service (macOS LaunchAgent):"
	@echo "  make start      - Install and start background service"
	@echo "  make stop       - Stop background service"
	@echo "  make restart    - Restart background service"
	@echo "  make status     - Check if service is running"
	@echo "  make logs       - Tail stdout logs"
	@echo "  make logs-err   - Tail stderr logs"
	@echo "  make uninstall  - Remove background service"
	@echo ""
	@echo "Development:"
	@echo "  make install    - Install dependencies"
	@echo "  make test       - Run tests"
	@echo "  make typecheck  - Run TypeScript type check"
	@echo "  make clean      - Remove temp files and logs"

install:
	bun install

setup: install
	@if [ ! -f .env ]; then \
		cp .env.example .env; \
		echo "Created .env from template"; \
		echo ">>> Edit .env with your credentials"; \
	else \
		echo ".env already exists"; \
	fi

run:
	bun run start

dev:
	bun run dev

test:
	bun test

typecheck:
	bun run typecheck

start:
	@if [ ! -f .env ]; then \
		echo "Error: .env not found. Run 'make setup' first."; \
		exit 1; \
	fi
	@echo "Installing LaunchAgent..."
	@mkdir -p ~/Library/LaunchAgents
	@export $$(grep -v '^#' .env | xargs) && \
	sed -e "s|/Users/USERNAME/.bun/bin/bun|$$(command -v bun)|g" \
	    -e "s|/Users/USERNAME/Dev/{project-path}|$$(pwd)|g" \
	    -e "s|USERNAME|$$(whoami)|g" \
	    -e "s|your-bot-token-here|$${TELEGRAM_BOT_TOKEN}|g" \
	    -e "s|<string>123456789</string>|<string>$${TELEGRAM_ALLOWED_USERS}</string>|g" \
	    $(PLIST_TEMPLATE) > $(PLIST_PATH)
	@echo "Created $(PLIST_PATH) with values from .env"
	@launchctl unload $(PLIST_PATH) 2>/dev/null || true
	@launchctl load $(PLIST_PATH)
	@echo "Service started. Check 'make logs' for output."

stop:
	@launchctl unload $(PLIST_PATH) 2>/dev/null || echo "Service not running"
	@echo "Service stopped"

restart:
	@launchctl kickstart -k gui/$$(id -u)/$(PLIST_NAME) 2>/dev/null || \
		(echo "Service not loaded. Run 'make start' first." && exit 1)
	@echo "Service restarted"

status:
	@if launchctl list | grep -q $(PLIST_NAME); then \
		echo "Service: RUNNING"; \
		launchctl list $(PLIST_NAME); \
	else \
		echo "Service: NOT RUNNING"; \
	fi

uninstall: stop
	@rm -f $(PLIST_PATH)
	@echo "Service uninstalled"

logs:
	@if [ -f $(LOG_FILE) ]; then \
		tail -f $(LOG_FILE); \
	else \
		echo "No log file yet. Start the service first."; \
	fi

logs-err:
	@if [ -f $(ERR_FILE) ]; then \
		tail -f $(ERR_FILE); \
	else \
		echo "No error log yet."; \
	fi

clean:
	rm -f $(LOG_FILE) $(ERR_FILE) 2>/dev/null || true
	@echo "Cleaned temp files"
第 3 步:自定义 sed 替换规则

start 目标需要与 plist 模板匹配的 sed 模式。常见模式:

  • your-bot-token-here → ${TELEGRAM_BOT_TOKEN}
  • 123456789 → ${TELEGRAM_ALLOWED_USERS}
  • /Users/USERNAME → 展开后的 $(whoami) 路径
  • 为其他环境变量添加更多 -e "s|pattern|replacement|g"
第 4 步:验证
make help
make setup
make run  # Test in foreground first
make start  # Then as service
make status

示例

输入:“为我的 Telegram bot 项目创建 Makefile”

**输出:**生成完整的 Makefile,询问项目专属信息(名称、路径),并根据其 plist 模板自定义 sed 模式。

评论

支持 Markdown