Carry の Blog Carry の Blog
首页
  • Nginx
  • Prometheus
  • Iptables
  • Systemd
  • Firewalld
  • Docker
  • Sshd
  • DBA工作笔记
  • MySQL
  • Redis
  • TiDB
  • Elasticsearch
  • Python
  • Shell
  • MySQL8-SOP手册
  • 分类
  • 标签
  • 归档
GitHub (opens new window)

Carry の Blog

好记性不如烂键盘
首页
  • Nginx
  • Prometheus
  • Iptables
  • Systemd
  • Firewalld
  • Docker
  • Sshd
  • DBA工作笔记
  • MySQL
  • Redis
  • TiDB
  • Elasticsearch
  • Python
  • Shell
  • MySQL8-SOP手册
  • 分类
  • 标签
  • 归档
GitHub (opens new window)
  • Shell

    • 每多少行添加一行文字
    • Telegram Bot 自动发送消息和文件脚本
      • 功能介绍
      • 使用说明
        • 示例
      • 脚本代码
    • esm全量迁移es数据
  • 蹩脚编程
  • Shell
Carry の Blog
2023-02-26
目录

Telegram Bot 自动发送消息和文件脚本原创

# 功能介绍

这个脚本主要有以下几个功能:

  1. 发送消息:通过 Telegram Bot 发送文本消息到指定的 chat_id。
  2. 发送文件:通过 Telegram Bot 发送文件到指定的 chat_id。
  3. 消息格式化:支持发送 Markdown 或 HTML 格式的消息。
  4. 日志记录:将每次发送的消息和文件记录到日志文件中,方便查看发送记录。
  5. 错误处理:处理文件不存在或参数错误等情况,并给出相应的提示信息。

# 使用说明

脚本的使用方法如下:

发送消息或者文件: ./telegram_bot.sh <file|msg> message
-h, --help 显示帮助信息
-f, --format <Markdown|HTML> 设置消息格式 (仅用于发送消息)
1
2
3

# 示例

  1. 发送文本消息
./telegram_bot.sh msg "Hello, this is a test message."
1
  1. 发送文件
./telegram_bot.sh file /path/to/your/file.txt
1
  1. 发送格式化的消息
./telegram_bot.sh msg -f Markdown "*Hello*, this is a _Markdown_ message."
1

# 脚本代码

以下是完整的脚本代码,你也可以通过 gist 链接 (opens new window) 查看和下载代码。

#!/bin/bash
#在执行脚本前设置环境变量:
#export TELEGRAM_CHAT_ID="your_chat_id"
#export TELEGRAM_BOT_KEY="your_bot_key"


# 从环境变量中读取 chat_id 和 key,如果不存在则使用参考值
chat_id="${TELEGRAM_CHAT_ID:-123456}"
key="${TELEGRAM_BOT_KEY:-981231123:AAFrAaaaaaaaaaaaaaaaaaQdCh1Eq9F0rlvc}"

MSG=$2
LOG_FILE="telegram_bot.log"

function Usage(){
    echo "使用说明:"
    echo ""
    echo -e "\t发送消息或者文件: $0 <file|msg> message"
    echo -e "\t-h, --help 显示帮助信息"
    echo -e "\t-f, --format <Markdown|HTML> 设置消息格式 (仅用于发送消息)"
}

function log(){
    echo "$(date +'%Y-%m-%d %H:%M:%S') - $1" >> $LOG_FILE
}

function send_file(){
    if [[ ! -f "$MSG" ]]; then
        echo "文件不存在: $MSG"
        log "文件不存在: $MSG"
        exit 1
    fi
    curl -F document=@"$MSG" "https://api.telegram.org/bot${key}/sendDocument?chat_id=${chat_id}"
    log "发送文件: $MSG"
}

function send_message(){
    local format=$1
    curl -X POST "https://api.telegram.org/bot${key}/sendMessage" \
        -d "chat_id=${chat_id}" \
        -d "text=$MSG" \
        -d "parse_mode=$format"
    log "发送消息: $MSG (格式: $format)"
}

FORMAT=""

# 解析参数
while [[ "$#" -gt 0 ]]; do
    case $1 in
        -h|--help) Usage; exit 0 ;;
        -f|--format) FORMAT="$2"; shift ;;
        *) ACTION="$1"; MSG="$2"; shift 2 ;;
    esac
done

if [[ -z "$ACTION" || -z "$MSG" ]]; then
    echo -e  "\t [ \033[44m参数错误或缺少参数 \033[0m] "
    Usage
    exit 1
fi

case $ACTION in
    file) send_file ;;
    msg) send_message "$FORMAT" ;;
    *) echo -e  "\t [ \033[44m$ACTION\033[0m  \033[41m参数错误或缺少参数 \033[0m] "; Usage; exit 1 ;;
esac

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
#Bash#Telegram#自动化
上次更新: 4/24/2025

← 每多少行添加一行文字 esm全量迁移es数据→

最近更新
01
tidb fast ddl
04-04
02
TiDB配置文件调优 原创
04-03
03
如何移除TiDB中的表分区 原创
04-03
更多文章>
Theme by Vdoing
  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式