Skip to content

Commit

Permalink
feat: 推送消息接口支持发送图片
Browse files Browse the repository at this point in the history
  • Loading branch information
pupilcc committed May 9, 2021
1 parent 2911bd5 commit a49d616
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 104 deletions.
28 changes: 7 additions & 21 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
## 功能

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

## 食用指南
Expand All @@ -28,34 +27,21 @@
参数|类型|必须|说明
-|-|-|-
text|String|是|发送的文字内容
parse_mode|String|否|发送文字内容的样式,可以是 Markdown 或 HTML

```bash
// using get
curl -X GET https://pushbot.pupilcc.com/sendMessage/{chatToken}?text=HelloWorld

// using post
curl -d "text=HelloWorld" -X POST https://pushbot.pupilcc.com/sendMessage/{chatToken}
```

### 推送图片

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

**参数说明:**

参数|类型|必须|说明
-|-|-|-
photoUrl|String|是(当有图片文件时,可为否)|发送的图片外链
photoFile|File|是(当有图片外链时,可为否)|发送的图片文件
caption|String|否|发送的文字内容
parse_mode|String|否|发送内容的样式,可以是 Markdown 或 HTML
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}
```

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

import com.pupilcc.pushbot.entity.BotPhotoDTO;
import com.pupilcc.pushbot.extension.ApiResult;
import com.pupilcc.pushbot.service.MessageService;
import com.pupilcc.pushbot.entity.BotMessageDTO;
Expand Down Expand Up @@ -30,15 +29,4 @@ public MessageController(MessageService messageService) {
public ApiResult<Object> sendMessage(BotMessageDTO dto, @PathVariable String chatToken) {
return messageService.sendMessage(dto, chatToken);
}

/**
* 给用户发送图片
* @param dto 消息内容
* @param chatToken 用户Token
* @return 响应消息
*/
@RequestMapping("/sendPhoto/{chatToken}")
public ApiResult<Object> sendPhoto(BotPhotoDTO dto, @PathVariable String chatToken) {
return messageService.sendPhoto(dto, chatToken);
}
}
11 changes: 11 additions & 0 deletions src/main/java/com/pupilcc/pushbot/entity/BotMessageDTO.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

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

/**
* 发送消息的参数
Expand All @@ -14,6 +15,16 @@ public class BotMessageDTO {
*/
private String text;

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

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

/**
* 格式化选项
*/
Expand Down
32 changes: 0 additions & 32 deletions src/main/java/com/pupilcc/pushbot/entity/BotPhotoDTO.java

This file was deleted.

63 changes: 24 additions & 39 deletions src/main/java/com/pupilcc/pushbot/service/MessageService.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
import com.pengrad.telegrambot.request.SendPhoto;
import com.pengrad.telegrambot.response.SendResponse;
import com.pupilcc.pushbot.entity.BotMessageDTO;
import com.pupilcc.pushbot.entity.BotPhotoDTO;
import com.pupilcc.pushbot.extension.ApiErrorCode;
import com.pupilcc.pushbot.extension.ApiResult;
import com.pupilcc.pushbot.users.Users;
Expand Down Expand Up @@ -53,8 +52,15 @@ public ApiResult<Object> sendMessage(BotMessageDTO dto, String chatToken) {
if (!apiResult.ok()) {
return apiResult;
}
boolean isSend;

boolean isSend = sendMessage(dto.getText(), dto.getParseMode(), users.getChatId());
// 发送图片
if (isExistPhoto(dto)) {
isSend = sendPhoto(dto, users.getChatId());
return ApiResult.success(isSend);
}

isSend = sendMessage(dto.getText(), dto.getParseMode(), users.getChatId());
return ApiResult.success(isSend);
}

Expand All @@ -78,26 +84,13 @@ private boolean sendMessage(String text, ParseMode parseMode, Long chatId) {
/**
* 发送图片
* @param dto 消息内容
* @param chatToken 用户Token
* @return 响应信息
* @param chatId 用户id
* @return 是否发送成功
*/
public ApiResult<Object> sendPhoto(BotPhotoDTO dto, String chatToken) {
// 查找用户
Users users = usersRepository.findByChatToken(chatToken);
// 用户不存在
if (ObjectUtils.isEmpty(users)) {
return ApiResult.failed(ApiErrorCode.USER_NOT_EXIST);
}

// 参数校验
ApiResult apiResult = checkParameter(dto);
if (!apiResult.ok()) {
return apiResult;
}

private boolean sendPhoto(BotMessageDTO dto, Long chatId) {
boolean isSend = false;
if (StringUtils.isNotBlank(dto.getPhotoUrl())) {
isSend = sendPhoto(dto.getCaption(), dto.getPhotoUrl(), dto.getParseMode(), users.getChatId());
isSend = sendPhoto(dto.getText(), dto.getPhotoUrl(), dto.getParseMode(), chatId);
} else if (ObjectUtils.isNotEmpty(dto.getPhotoFile())){
FilePart filePart = dto.getPhotoFile();
String fileName = filePart.filename();
Expand All @@ -107,14 +100,14 @@ public ApiResult<Object> sendPhoto(BotPhotoDTO dto, String chatToken) {
try {
file = File.createTempFile(IdUtil.simpleUUID(), prefix);
filePart.transferTo(file);
isSend = sendPhoto(dto.getCaption(), file, dto.getParseMode(), users.getChatId());
isSend = sendPhoto(dto.getText(), file, dto.getParseMode(), chatId);
file.delete();
} catch (IOException e) {
e.printStackTrace();
return ApiResult.failed(ApiErrorCode.FAILED);
return false;
}
}
return ApiResult.success(isSend);
return isSend;
}

/**
Expand All @@ -125,7 +118,7 @@ public ApiResult<Object> sendPhoto(BotPhotoDTO dto, String chatToken) {
* @param chatId 用户id
* @return 响应信息
*/
public boolean sendPhoto(String text, String photoUrl, ParseMode parseMode, Long chatId) {
private boolean sendPhoto(String text, String photoUrl, ParseMode parseMode, Long chatId) {
SendPhoto sendPhoto = new SendPhoto(chatId, photoUrl);
return sendPhoto(sendPhoto, text, parseMode);
}
Expand All @@ -138,7 +131,7 @@ public boolean sendPhoto(String text, String photoUrl, ParseMode parseMode, Long
* @param chatId 用户id
* @return 响应信息
*/
public boolean sendPhoto(String text, File photoFile, ParseMode parseMode, Long chatId) {
private boolean sendPhoto(String text, File photoFile, ParseMode parseMode, Long chatId) {
SendPhoto sendPhoto = new SendPhoto(chatId, photoFile);
return sendPhoto(sendPhoto, text, parseMode);
}
Expand Down Expand Up @@ -170,28 +163,20 @@ private ApiResult checkParameter(BotMessageDTO dto) {
if (ObjectUtils.isEmpty(dto)) {
return ApiResult.failed(ApiErrorCode.PARAMETER_NULL);
}
if (StringUtils.isBlank(dto.getText())) {
// 如果图片不存在,text 参数不能为空。
if (StringUtils.isBlank(dto.getText()) && !isExistPhoto(dto)) {
return ApiResult.failed(ApiErrorCode.TEXT_NULL);
}

return ApiResult.success();
}

/**
* 参数校验
* @param dto 消息内容
* @return 业务码
* 是否存在图片
* @param dto 请求体
* @return true 存在图片; false 不存在图片;
*/
private ApiResult checkParameter(BotPhotoDTO dto) {
if (ObjectUtils.isEmpty(dto)) {
return ApiResult.failed(ApiErrorCode.PARAMETER_NULL);
}

boolean isExistPhoto = ObjectUtils.isNotEmpty(dto.getPhotoFile()) || StringUtils.isNotBlank(dto.getPhotoUrl());
if (!isExistPhoto) {
return ApiResult.failed(ApiErrorCode.IMG_NULL);
}

return ApiResult.success();
private boolean isExistPhoto(BotMessageDTO dto) {
return StringUtils.isNotBlank(dto.getPhotoUrl()) || ObjectUtils.isNotEmpty(dto.getPhotoFile());
}
}

0 comments on commit a49d616

Please sign in to comment.