社区所有版块导航
Python
python开源   Django   Python   DjangoApp   pycharm  
DATA
docker   Elasticsearch  
aigc
aigc   chatgpt  
WEB开发
linux   MongoDB   Redis   DATABASE   NGINX   其他Web框架   web工具   zookeeper   tornado   NoSql   Bootstrap   js   peewee   Git   bottle   IE   MQ   Jquery  
机器学习
机器学习算法  
Python88.com
反馈   公告   社区推广  
产品
短视频  
印度
印度  
Py学习  »  Git

如何从GitLab管道向电报发送通知?

Mark • 3 年前 • 1286 次点击  

在我们的小型初创公司中,我们使用GitLab进行开发,并使用Telegram进行开发人员和PO之间的内部通信。由于PO希望立即看到进度,我们设置了GitLab管道,以便在每次提交后在web服务器上部署预览版本。现在我们想扩大管道。因此,在部署之后,通过电报组发送通知。

所以问题是——这可能吗?如果可能,怎么可能?

编辑:因为我已经实现了这一点,这不是一个真正的问题。我想把答案贴在这里,这样其他人也可以使用它。

Python社区是高质量的Python/Django开发社区
本文地址:http://www.python88.com/topic/130907
 
1286 次点击  
文章 [ 2 ]  |  最新文章 3 年前
sytech
Reply   •   1 楼
sytech    3 年前

发送通知的一种简单方法(尤其是当您使用多个服务或聊天时)是使用 apprise .

要发送到一个电报频道:

apprise -vv --body="Notify telegram chat" \
  tgram://bottoken/ChatID1 \

这使得一次从管道中通知多个服务变得很容易,而无需针对每个服务的API编写代码(apprise为您处理)。

image: python:3.9-slim # or :3.9-alpine if you prefer a smaller image
before_script:
  - pip install apprise # consider caching PIP_CACHE_DIR for performance
script: | 
  # Set a notification to multiple telegram chats, a yahoo email account, 
  # Slack, and a Kodi Server with a bit of added verbosity:
  apprise -vv --body="Notify more than one service" \
    tgram://bottoken/ChatID1/ChatID2/ChatIDN \
    mailto://user:password@yahoo.com \
    slack://token_a/token_b/token_c \
    kodi://example.com
Mark
Reply   •   2 楼
Mark    3 年前

所以,我们将一步一步地讲:

  1. 创建一个电报机器人
  2. 将bot添加到电报组
  3. 查找电报组Id
  4. 通过GitLab管道发送消息

1.创建一个电报机器人

电报本身对此有足够好的指示:

https://core.telegram.org/bots#6-botfather

指令没有明确说明任何内容,但要生成指令,您必须与BotFather聊天。 最后你会得到一个机器人令牌,比如 110201543:AAHdqTcvCH1vGWJxfSeofSAs0K5PALDsaw

2.将bot添加到电报组

切换到Telegram组,并将创建的bot添加为成员(按名称查找bot)。

3.查找电报组Id

在浏览器中获取机器人的更新状态: https://api.telegram.org/bot<YourBOTToken>/getUpdates

在响应中查找聊天id: ... "chat": {"id": <YourGroupID>, ...

有关更多详细信息,请参阅: Telegram Bot - how to get a group chat id?

4.通过GitLab管道发送消息

用curl命令发送消息。例如,gitlab管道中的一个现有阶段可以为此目的进行扩展:

upload:
  stage: deploy
  image: alpine:latest
  script:
    - 'apk --no-cache add curl'
    - 'curl -X POST -H "Content-Type: application/json" -d "{\"chat_id\": \"<YourGroupID>\", \"text\": \"CI: new version was uploaded, see: https://preview.startup.com\"}" https://api.telegram.org/bot<YourBOTToken>/sendMessage '
  only:
    - main

记住要适应环境 YourBOTToken YourGroupID ,以及消息的文本。

*)我们在这里使用阿尔卑斯码头的图片,所以必须安装curl - 'apk --no-cache add curl' .对于其他图像,这可能必须以不同的方式完成。