Skip to content

Commit

Permalink
Merge pull request #5 from pupilcc/dev
Browse files Browse the repository at this point in the history
v0.2.4
  • Loading branch information
pupilcc authored Dec 19, 2021
2 parents 8da3d8e + bac0803 commit c55651d
Show file tree
Hide file tree
Showing 8 changed files with 101 additions and 44 deletions.
35 changes: 35 additions & 0 deletions .github/workflows/docker-image.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
name: Docker Image CI

on:
push:
branches:
- master
- dev
tags:
- v*

jobs:
push_to_registry:
name: Push Docker image to Docker Hub
runs-on: ubuntu-latest
steps:
- name: Check out the repo
uses: actions/checkout@v2
- name: Log in to Docker Hub
uses: docker/login-action@v1
with:
username: ${{ secrets.DOCKERHUB_USERNAME }}
password: ${{ secrets.DOCKERHUB_TOKEN }}
- name: Extract metadata (tags, labels) for Docker
id: meta
uses: docker/metadata-action@v2
with:
images: pupilcc/pushbot
- name: Build and push
id: docker_build
uses: docker/build-push-action@v2
with:
context: .
push: true
tags: ${{ steps.meta.outputs.tags }}
labels: ${{ steps.meta.outputs.labels }}
37 changes: 19 additions & 18 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,23 +26,22 @@

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

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

// using post
// 推送消息
curl -d "text=HelloWorld" -X POST https://pushbot.pupilcc.com/sendMessage/{chatToken}
// 推送图片
curl -d "photoUrl=https://xxx.com/xxxxx.jpg" -X POST https://pushbot.pupilcc.com/sendPhoto/{chatToken}
# 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}
```

### <span id="DockerHub">推送 Docker Hub 自动构建成功消息</span>
Expand Down Expand Up @@ -75,7 +74,9 @@ Bot 不会识别和储存任何用户推送的消息,只会将推送消息发
#### 创建 sqlite3 数据库 pushbot.db

```
// pushbot.db
# 进入存放数据库文件的目录
cd /opt
# pushbot.db
sqlite3 pushbot.db
sqlite> CREATE TABLE users (chatId int unique, chatToken text unique);
Expand All @@ -96,8 +97,8 @@ services:
container_name: pushbot
restart: unless-stopped
volumes:
# 创建好的数据库绝对路径 /home/pushbot.db
- /home/pushbot.db:/app/pushbot/pushbot.db
# 创建好的数据库绝对路径
- /opt/pushbot.db:/app/pushbot/pushbot.db
ports:
- "25701:25701"
environment:
Expand All @@ -116,7 +117,7 @@ docker run -d \
-p 25701:25701 \
-e BOT_TOKEN=<TOKEN> \
-e BOT_DOMAIN=<DOMAIN> \
-v /home/pushbot.db:/app/pushbot/pushbot.db \
-v /opt/pushbot.db:/app/pushbot/pushbot.db \
pupilcc/pushbot
```

Expand Down
19 changes: 16 additions & 3 deletions 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.2.3</version>
<version>0.2.4</version>
<name>pushbot</name>
<description>telegram notification bot</description>

Expand Down Expand Up @@ -58,6 +58,19 @@
<optional>true</optional>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>

<!-- 避免 actuator 和 webflux 同时使用缺少 jersey-server 依赖 -->
<!-- https://mvnrepository.com/artifact/org.glassfish.jersey.core/jersey-server -->
<dependency>
<groupId>org.glassfish.jersey.core</groupId>
<artifactId>jersey-server</artifactId>
<version>3.0.3</version>
</dependency>

<dependency>
<groupId>org.xerial</groupId>
<artifactId>sqlite-jdbc</artifactId>
Expand All @@ -77,13 +90,13 @@
<dependency>
<groupId>org.telegram</groupId>
<artifactId>telegrambots</artifactId>
<version>4.4.0.1</version>
<version>5.5.0</version>
</dependency>

<dependency>
<groupId>com.github.pengrad</groupId>
<artifactId>java-telegram-bot-api</artifactId>
<version>4.9.0</version>
<version>5.5.0</version>
</dependency>

<!-- lombok -->
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package com.pupilcc.pushbot.controller;

import com.pupilcc.pushbot.entity.SendMessageDTO;
import com.pupilcc.pushbot.extension.ApiResult;
import com.pupilcc.pushbot.service.MessageService;
import com.pupilcc.pushbot.entity.BotMessageDTO;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
Expand All @@ -26,7 +26,7 @@ public MessageController(MessageService messageService) {
* @return 响应消息
*/
@RequestMapping("/sendMessage/{chatToken}")
public ApiResult<Object> sendMessage(BotMessageDTO dto, @PathVariable String chatToken) {
public ApiResult<Object> sendMessage(SendMessageDTO dto, @PathVariable String chatToken) {
return messageService.sendMessage(dto, chatToken);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,28 +2,22 @@

import com.pengrad.telegrambot.model.request.ParseMode;
import lombok.Data;
import org.springframework.http.codec.multipart.FilePart;

/**
* 发送消息的参数
* @author pupilcc
*/
@Data
public class BotMessageDTO {
public class SendMessageDTO {
/**
* 文本内容
*/
private String text;

/**
* 图片链接
* 图片
*/
private String photoUrl;

/**
* 图片文件
*/
private FilePart photoFile;
private Object photo;

/**
* 格式化选项
Expand Down
27 changes: 17 additions & 10 deletions src/main/java/com/pupilcc/pushbot/service/MessageService.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import com.pengrad.telegrambot.request.SendMessage;
import com.pengrad.telegrambot.request.SendPhoto;
import com.pengrad.telegrambot.response.SendResponse;
import com.pupilcc.pushbot.entity.BotMessageDTO;
import com.pupilcc.pushbot.entity.SendMessageDTO;
import com.pupilcc.pushbot.extension.ApiErrorCode;
import com.pupilcc.pushbot.extension.ApiResult;
import com.pupilcc.pushbot.users.Users;
Expand Down Expand Up @@ -39,7 +39,7 @@ public MessageService(UsersRepository usersRepository, TelegramBot telegramBot)
* @param chatToken 用户Token
* @return 响应信息
*/
public ApiResult<Object> sendMessage(BotMessageDTO dto, String chatToken) {
public ApiResult<Object> sendMessage(SendMessageDTO dto, String chatToken) {
// 查找用户
Users users = usersRepository.findByChatToken(chatToken);
// 用户不存在
Expand Down Expand Up @@ -87,12 +87,19 @@ private boolean sendMessage(String text, ParseMode parseMode, Long chatId) {
* @param chatId 用户id
* @return 是否发送成功
*/
private boolean sendPhoto(BotMessageDTO dto, Long chatId) {
private boolean sendPhoto(SendMessageDTO dto, Long chatId) {
boolean isSend = false;
if (StringUtils.isNotBlank(dto.getPhotoUrl())) {
isSend = sendPhoto(dto.getText(), dto.getPhotoUrl(), dto.getParseMode(), chatId);
} else if (ObjectUtils.isNotEmpty(dto.getPhotoFile())){
FilePart filePart = dto.getPhotoFile();
Object photoObj = dto.getPhoto();

boolean isStr = photoObj instanceof String;
if (isStr) {
String photoUrl = String.valueOf(dto.getPhoto());
isSend = sendPhoto(dto.getText(), photoUrl, dto.getParseMode(), chatId);
}

boolean isFile = photoObj instanceof FilePart;
if (isFile) {
FilePart filePart = (FilePart) dto.getPhoto();
String fileName = filePart.filename();
String prefix= fileName.substring(fileName.lastIndexOf("."));

Expand Down Expand Up @@ -159,7 +166,7 @@ private boolean sendPhoto(SendPhoto sendPhoto, String text, ParseMode parseMode)
* @param dto 消息内容
* @return 业务码
*/
private ApiResult checkParameter(BotMessageDTO dto) {
private ApiResult checkParameter(SendMessageDTO dto) {
if (ObjectUtils.isEmpty(dto)) {
return ApiResult.failed(ApiErrorCode.PARAMETER_NULL);
}
Expand All @@ -176,7 +183,7 @@ private ApiResult checkParameter(BotMessageDTO dto) {
* @param dto 请求体
* @return true 存在图片; false 不存在图片;
*/
private boolean isExistPhoto(BotMessageDTO dto) {
return StringUtils.isNotBlank(dto.getPhotoUrl()) || ObjectUtils.isNotEmpty(dto.getPhotoFile());
private boolean isExistPhoto(SendMessageDTO dto) {
return ObjectUtils.isNotEmpty(dto.getPhoto());
}
}
4 changes: 2 additions & 2 deletions src/main/java/com/pupilcc/pushbot/service/WebhookService.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import com.pengrad.telegrambot.model.request.ParseMode;
import com.pupilcc.pushbot.config.BotProperties;
import com.pupilcc.pushbot.entity.BotMessageDTO;
import com.pupilcc.pushbot.entity.SendMessageDTO;
import com.pupilcc.pushbot.entity.DockerWebHookDTO;
import com.pupilcc.pushbot.users.Users;
import com.pupilcc.pushbot.users.UsersRepository;
Expand Down Expand Up @@ -57,7 +57,7 @@ public void webhookDocker(DockerWebHookDTO dto, String chatToken) {
Users users = usersRepository.findByChatToken(chatToken);
if (ObjectUtils.isNotEmpty(users)) {
// 推送消息
BotMessageDTO messageDTO = new BotMessageDTO();
SendMessageDTO messageDTO = new SendMessageDTO();
messageDTO.setText("Docker Hub 自动构建成功" + "\n\n" +
dto.getRepository().getRepoName() + " 构建于 " + dto.getPushData().getTag() + "\n\n" +
"[查看镜像](" + dto.getRepository().getRepoUrl() + ")");
Expand Down
7 changes: 7 additions & 0 deletions src/main/resources/application.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,13 @@ spring:
naming:
physical-strategy: org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl

management:
endpoints:
enabled-by-default: false
endpoint:
health:
enabled: true

bot:
# BotToken 123456:xxxxxxx
token:
Expand Down

0 comments on commit c55651d

Please sign in to comment.