跳转到主要内容

一个从命令行发送Telegram消息的小工具。

项目描述

cli2telegram

一个从命令行发送Telegram消息的小工具。

这可以用于

  • 快速向您选择的Telegram聊天发送任意消息
  • 用作linux上"mail"程序的替代品
  • 将Telegram用作ZFS事件守护进程(zed)的通知后端

功能

  • 从参数或STDIN读取消息
  • (可选)配置文件
  • 在指定时间内重试发送消息
  • 处理长消息
  • 作为守护进程运行并将消息回显到linux管道中

示例

cli2telegram -h

# From arguments
cli2telegram "This is a message"

cli2telegram "Header" "This is a multiline message."

# From STDIN
echo My Message | cli2telegram

printf "Header\nThis is a multiline message." | cli2telegram

# Config via parameters
printf "Message" | cli2telegram -b "123456789:ABCDEFG" -c "123456789"

# Config via parameters and send message in a code block
printf "Message" | cli2telegram -b "123456789:ABCDEFG" -c "123456789" -C

# as a Daemon
cli2telegram -d -p "/tmp/cli2telegram"
echo "hello world!" > /tmp/cli2telegram

安装

要使用此实用程序,您可以使用以下方法之一进行安装:

pip install cli2telegram

或者 - 如果您不想全局安装它 - 使用例如 venv-install

venv-install cli2telegram cli2telegram

或您选择的任何自定义venv管理器。

配置

为了能够向您发送消息,您必须提供bot令牌聊天id。您可以使用cli参数、配置文件、环境变量或它们的组合来配置cli2telegram。

参数

名称 类型 描述
-b, --bot-token 字符串 Telegram Bot Token
-c, --chat-id 字符串 Telegram Chat ID
-C, --code-block 字符串 在代码块内发送消息
-d, --daemon 标志 作为守护进程运行
-p, --pipe 字符串 以守护进程模式使用的管道文件路径

文件

cli2telegram使用container-app-conf,因此您可以使用YAML、TOML或ENV来设置这些。

查看cli2telegram.toml_example文件以获得灵感。

守护进程

当以守护进程运行cli2telegram时,在接收输入消息之间,管道将关闭一段时间。如果您正在使用脚本(例如)向管道发送多个消息,请确保在发送消息之间等待一段时间(例如0.5秒),否则

  • 多个消息可能会被视为一条
  • 消息可能会丢失
  • 您可能会收到Broken pipe错误

使用案例

ZFS事件守护进程(ZED)

为了使zed调用cli2telegram,我们将欺骗它,并使其将cli2telegram用作电子邮件客户端。

以root身份编辑/etc/zfs/zed.d/zed.rc

sudo nano -w /etc/zfs/zed.d/zed.rc

  • 取消注释ZED_EMAIL_ADDR,由于我们使用自己的电子邮件脚本,所以值不重要,但设置一个值对于ZED发送“电子邮件”是必要的
  • ZED_EMAIL_PROG设置为脚本的路径,例如/usr/bin/cli2telegram
    • 需要注意的是,如果您的命令需要运行时需要参数,zed似乎无法工作
# this must not be empty!
ZED_EMAIL_ADDR="root"

[...]

ZED_EMAIL_PROG="/usr/bin/cli2telegram"

[...]

# this must not be empty!
ZED_EMAIL_OPTS="#zfs #$(hostname)"

[...]

# If you want to receive email no matter the state of your pool, you’ll want to set:
ZED_NOTIFY_VERBOSE=1

[...]

由于zed将以root身份运行您的脚本,如果您想使用配置文件,则必须将其放在例如/root/.config/cli2telegram.toml中。

贡献

GitHub是用于社交编码的:如果您想编写代码,我鼓励您通过从该存储库的分支中发送拉取请求来做出贡献。为错误和新功能创建GitHub问题,并评论您感兴趣的问题。

许可证

cli2telegram by Markus Ressel
Copyright (C) 2018  Markus Ressel

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as
published by the Free Software Foundation, either version 3 of the
License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU Affero General Public License for more details.

You should have received a copy of the GNU Affero General Public License
along with this program.  If not, see <https://gnu.ac.cn/licenses/>.

项目详情


下载文件

下载您平台上的文件。如果您不确定选择哪个,请了解更多关于安装软件包的信息。

源分布

cli2telegram-2.0.1.tar.gz (20.7 kB 查看哈希值)

上传时间:

构建分布

cli2telegram-2.0.1-py3-none-any.whl (23.4 kB 查看哈希值)

上传时间: Python 3

由以下支持

AWS AWS 云计算和安全赞助商 Datadog Datadog 监控 Fastly Fastly CDN Google Google 下载分析 Microsoft Microsoft PSF 赞助商 Pingdom Pingdom 监控 Sentry Sentry 错误日志 StatusPage StatusPage 状态页面