Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dev #14

Closed
wants to merge 5 commits into from
Closed

Dev #14

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
170 changes: 9 additions & 161 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,172 +1,20 @@
# pushbot
# PushBot

该项目是基于 Telegram Bot 的消息推送 bot,可将消息通过 GET/POST 网络请求经由 Telegram Bot 推送至用户。可用于服务器监控报警、抢票通知、每日天气推送等应用场景。

提供了 [Docker 镜像](https://hub.docker.com/r/pupilcc/pushbot),便于自建,也可直接使用我提供的 Bot,欢迎品尝~([点击此处添加 Bot](https://t.me/notification_me_bot))

## 一、功能

* [推送消息](#推送消息)
* [推送模板消息](#推送模板消息)
* [推送 Docker Hub 自动构建成功消息](#DockerHub)

## 二、食用指南

### Bot 命令说明

* `/start`:生成/显示推送链接
* `/end`:删除推送链接

生成推送链接后,请将下列链接中的`{chatToken}`替换成自己推送链接末尾专属的随机字符串,即可使用。

### 推送消息

接口地址:`/sendMessage/{chatToken}`

**参数说明:**

参数|类型|必须|说明
-|-|-|-
text|String|是|文字内容(当 photo 存在时可为空)
photo|String|否|图片文件或者图片外链
parseMode|String|否|发送文字内容的样式,可以是 Markdown 或 HTML

```bash
# using get
# 推送消息
curl -X GET https://pb.pupilcc.app/sendMessage/{chatToken}?text=HelloWorld
# 推送图片
curl -X GET https://pb.pupilcc.app/sendMessage/{chtToken}?photo=https://xxx.com/xxxxx.jpg

# using post
# 推送消息
curl -d "text=HelloWorld" -X POST https://pb.pupilcc.app/sendMessage/{chatToken}
# 推送图片
curl -d "photo=https://xxx.com/xxxxx.jpg" -X POST https://pb.pupilcc.app/sendMessage/{chatToken}
```

### 推送模板消息

接口地址:`/sendTemplate/{chatToken}`

**参数说明:**

参数|类型|必须|说明
-|-|-|-
templateId|String|是|模板id
title|String|是|标题
content|String|否|内容

[查看现有模板样式](https://github.com/pupilcc/pushbot/blob/master/docs/template.md)

### <span id="DockerHub">推送 Docker Hub 自动构建成功消息</span>

接口地址:`/webhook/docker/{chatToken}`

将该 URL 填写至 Docker Hub 仓库的 Webhooks URL 内即可,当 Docker Hub 自动构建成功后就会推送消息。
#### English

![docker-webhook](https://vip1.loli.io/2022/01/20/RLbr24NPfUc19Gv.png)
The project is a message push bot based on Telegram Bot, which can push messages to users via GET/POST network requests through Telegram Bot. It can be used in applications such as server monitoring alarms, ticket notification, and daily weather push.

### 隐私相关
A [Docker image](https://hub.docker.com/r/pupilcc/pushbot) is provided for easy self-building. You can also directly use the Bot I provide. Feel free to give it a try! ([Click here to add the Bot](https://t.me/notification_me_bot))

Bot 不会识别和储存任何用户推送的消息,只会将推送消息发送给 Telegram 服务器。Bot 只会记录用户回话 id,此 id 是向 Telegram 推送消息的凭据。
[Document](https://pushbot-docs.vercel.app)

#### 中文

## 三、自建指南

### 食材

* 一个域名
* 一台可访问 Telegram 的服务器
* Linux 基础知识

### 菜谱

#### 创建 Bot Token

点击 [@BotFather](https://telegram.me/BotFather),按步骤创建自己的 Bot 并获取 Token

#### 创建 sqlite3 数据库 pushbot.db

```
# 进入存放数据库文件的目录
cd /opt
# pushbot.db
sqlite3 pushbot.db

sqlite> CREATE TABLE users (chatId int unique, chatToken text unique);
sqlite> .quit
```

#### 使用 Docker 部署

需要替换 `BOT_TOKEN` 和 `BOT_DOMAIN` 的内容,以及数据库的路径。

docker-compose

```yaml
version: '3.7'
services:
pushbot:
image: pupilcc/pushbot
container_name: pushbot
restart: unless-stopped
volumes:
# 创建好的数据库绝对路径
- /opt/pushbot.db:/app/pushbot/pushbot.db
ports:
- "25701:25701"
environment:
# Bot Token
- BOT_TOKEN=<TOKEN>
# 自己的域名
- BOT_DOMAIN=<DOMAIN>
```

docker run

```bash
docker run -d \
--name pushbot \
--restart unless-stopped \
-p 25701:25701 \
-e BOT_TOKEN=<TOKEN> \
-e BOT_DOMAIN=<DOMAIN> \
-v /opt/pushbot.db:/app/pushbot/pushbot.db \
pupilcc/pushbot
```

#### 使用 Nginx 反向代理

在域名对应的 nginx 配置文件 443 端口处添加反向代理:

```
# 反向代理 Docker 指定端口
location / {
proxy_pass http://127.0.0.1:25701;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
```

#### 设置 WebHook

请将以下链接中的 `{botToken}` 替换为 Bot Token,`{domian}` 替换为自己设置的域名,替换好后在浏览器访问该链接。

```
https://api.telegram.org/bot{botToken}/setWebhook?url={domain}/webhook/{botToken}
```
该项目是基于 Telegram Bot 的消息推送 bot,可将消息通过 GET/POST 网络请求经由 Telegram Bot 推送至用户。可用于服务器监控报警、抢票通知、每日天气推送等应用场景。

提示如下信息,则代表设置成功,即可尽情享用。
提供了 [Docker 镜像](https://hub.docker.com/r/pupilcc/pushbot),便于自建,也可直接使用我提供的 Bot,欢迎品尝~([点击此处添加 Bot](https://t.me/notification_me_bot))

```json
{
"ok": true,
"result": true,
"description": "Webhook is already set"
}
```
[文档](https://pushbot-docs.vercel.app)

## 感谢

Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
</parent>
<groupId>com.pupilcc</groupId>
<artifactId>pushbot</artifactId>
<version>0.3.0</version>
<version>0.4.0</version>
<name>pushbot</name>
<description>telegram notification bot</description>

Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
package com.pupilcc.pushbot.controller;

import com.pupilcc.common.rest.ApiResult;
import com.pupilcc.pushbot.entity.MessageDTO;
import com.pupilcc.pushbot.entity.SendMessageDTO;
import com.pupilcc.pushbot.entity.TemplateMessageDTO;
import com.pupilcc.pushbot.dto.MessageDTO;
import com.pupilcc.pushbot.service.MessageService;
import org.springframework.web.bind.annotation.*;

Expand All @@ -13,6 +11,7 @@
* @author pupilcc
*/
@RestController
@RequestMapping("/message")
public class MessageController {
private final MessageService messageService;

Expand Down Expand Up @@ -43,56 +42,4 @@ public ApiResult<Object> message(MessageDTO dto, @PathVariable String chatToken)
public ApiResult<Object> sendMessageByJson(@RequestBody MessageDTO dto, @PathVariable String chatToken) {
return messageService.sendMessage(dto, chatToken);
}

/**
* 给用户发送消息
*
* @param dto 消息体
* @param chatToken 用户token
* @return 响应消息
*/
@Deprecated
@GetMapping("/sendMessage/{chatToken}")
public ApiResult<Object> sendMessage(SendMessageDTO dto, @PathVariable String chatToken) {
return messageService.sendMessage(dto, chatToken);
}

/**
* 给用户发送消息
*
* @param dto 消息体
* @param chatToken 用户token
* @return 响应消息
*/
@Deprecated
@PostMapping("/sendMessage/{chatToken}")
public ApiResult<Object> sendMessageByJson(@RequestBody SendMessageDTO dto, @PathVariable String chatToken) {
return messageService.sendMessage(dto, chatToken);
}

/**
* 给用户发送模板消息
*
* @param message 请求参数
* @param chatToken 用户token
* @return 响应消息
*/
@Deprecated
@GetMapping("/sendTemplate/{chatToken}")
public ApiResult<Object> sendTemplateMessage(TemplateMessageDTO message, @PathVariable String chatToken) {
return messageService.sendTemplate(message, chatToken);
}

/**
* 给用户发送模板消息
*
* @param message 请求参数
* @param chatToken 用户token
* @return 响应消息
*/
@Deprecated
@PostMapping("/sendTemplate/{chatToken}")
public ApiResult<Object> sendTemplateMessageByJson(@RequestBody TemplateMessageDTO message, @PathVariable String chatToken) {
return messageService.sendTemplate(message, chatToken);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
package com.pupilcc.pushbot.controller;

import com.pupilcc.common.rest.ApiResult;
import com.pupilcc.pushbot.dto.MessageDTO;
import com.pupilcc.pushbot.dto.TemplateMessageDTO;
import com.pupilcc.pushbot.service.MessageService;
import org.springframework.web.bind.annotation.*;

/**
* 消息控制器
*
* @author pupilcc
*/
@RestController
@Deprecated
public class OldMessageController {
private final MessageService messageService;

public OldMessageController(MessageService messageService) {
this.messageService = messageService;
}

/**
* 给用户发送消息
*
* @param dto 消息体
* @param chatToken 用户token
* @return 响应消息
*/
@GetMapping("/sendMessage/{chatToken}")
public ApiResult<Object> sendMessage(MessageDTO dto, @PathVariable String chatToken) {
return messageService.sendMessage(dto, chatToken);
}

/**
* 给用户发送消息
*
* @param dto 消息体
* @param chatToken 用户token
* @return 响应消息
*/
@PostMapping("/sendMessage/{chatToken}")
public ApiResult<Object> sendMessageByJson(@RequestBody MessageDTO dto, @PathVariable String chatToken) {
return messageService.sendMessage(dto, chatToken);
}

/**
* 给用户发送模板消息
*
* @param message 请求参数
* @param chatToken 用户token
* @return 响应消息
*/
@GetMapping("/sendTemplate/{chatToken}")
public ApiResult<Object> sendTemplateMessage(TemplateMessageDTO message, @PathVariable String chatToken) {
return messageService.sendTemplate(message, chatToken);
}

/**
* 给用户发送模板消息
*
* @param message 请求参数
* @param chatToken 用户token
* @return 响应消息
*/
@PostMapping("/sendTemplate/{chatToken}")
public ApiResult<Object> sendTemplateMessageByJson(@RequestBody TemplateMessageDTO message, @PathVariable String chatToken) {
return messageService.sendTemplate(message, chatToken);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package com.pupilcc.pushbot.controller;

import com.pupilcc.common.rest.ApiResult;
import com.pupilcc.pushbot.dto.TemplateMessageDTO;
import com.pupilcc.pushbot.service.MessageService;
import org.springframework.web.bind.annotation.*;

/**
* 模板消息控制器
*
* @author pupilcc
*/
@RestController
@RequestMapping("/template")
public class TemplateController {
private final MessageService messageService;

public TemplateController(MessageService messageService) {
this.messageService = messageService;
}

/**
* 给用户发送消息
*
* @param dto 消息体
* @param chatToken 用户token
* @return 响应消息
*/
@GetMapping("/{chatToken}")
public ApiResult<Object> message(TemplateMessageDTO dto, @PathVariable String chatToken) {
return messageService.sendTemplate(dto, chatToken);
}

/**
* 给用户发送消息
*
* @param dto 消息体
* @param chatToken 用户token
* @return 响应消息
*/
@PostMapping("/{chatToken}")
public ApiResult<Object> sendMessageByJson(@RequestBody TemplateMessageDTO dto, @PathVariable String chatToken) {
return messageService.sendTemplate(dto, chatToken);
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.pupilcc.pushbot.controller;

import com.pupilcc.pushbot.entity.DockerWebHookDTO;
import com.pupilcc.pushbot.entity.WorkflowDTO;
import com.pupilcc.pushbot.dto.docker.DockerWebHookDTO;
import com.pupilcc.pushbot.dto.WorkflowDTO;
import com.pupilcc.pushbot.service.WebhookService;
import org.springframework.web.bind.annotation.*;
import org.telegram.telegrambots.meta.api.objects.Update;
Expand Down
Loading
Loading