WxPusher消息推送工具速通版

详细步骤

使用 WxPusher 进行消息推送的步骤:

1. 注册与登录

  1. 访问 WxPusher 官方网站
  2. 点击“注册”并填写必要的信息完成注册。
  3. 注册完成后,用你的账户信息登录。

2. 创建应用

  1. 登录后,进入后台管理页面。
  2. 点击“应用管理”。
  3. 点击“创建应用”按钮,填写应用名称、应用描述等基本信息。
  4. 创建成功后,可以在应用列表中看到你创建的应用,点击进入应用详情页面。

3. 获取 AppToken

  1. 在应用详情页面,你会看到 AppToken,这是你进行消息推送的重要凭证。
  2. 复制 AppToken,后续发送消息时会用到它。

4. 获取用户 UID

4.1 生成二维码
  1. 在应用详情页面,点击“用户管理”。
  2. 点击“生成二维码”,生成一个用户关注二维码。
  3. 用户扫描该二维码后,会关注你的应用,并生成他们的 UID。
4.2 通过 API 获取 UID

如果你需要通过程序自动获取用户 UID,可以参考官方文档提供的接口。

5. 使用 API 进行消息推送

WxPusher 提供了 RESTful API 接口,你可以通过这些接口向用户发送消息。以下是详细的 API 使用方法:

5.1 API Endpoint

发送消息的 API URL 是 https://wxpusher.zjiecode.com/api/send/message

5.2 请求参数
参数名类型是否必需描述
appTokenString必需在应用详情页面获取到的 AppToken
contentString必需要发送的消息内容
contentTypeInt必需消息类型:1-文本,2-HTML,3-Markdown
uidsList必需用户 UID 列表
topicIdsList可选话题 ID 列表(如果不使用 UID,可以使用话题 ID 推送)
urlString可选点击消息后跳转的链接
verifyPayloadString可选验证消息有效性的随机字符串(增强安全性)
5.3 使用示例

以下是一个使用 curl 命令发送消息的详细示例:

curl -X POST 'https://wxpusher.zjiecode.com/api/send/message' \
-H 'Content-Type: application/json' \
-d '{
    "appToken":"你的AppToken",
    "content":"这是测试消息内容",
    "contentType":1,
    "uids":[
        "用户UID1",
        "用户UID2"
    ],
    "url":"https://example.com"
}'
5.4 使用 Python 发送消息

以下是一个使用 Python 发送消息的详细示例:

import requests
import json

def send_message(app_token, content, uids, url=None):
    api_url = 'https://wxpusher.zjiecode.com/api/send/message'
    headers = {
        'Content-Type': 'application/json'
    }
    data = {
        "appToken": app_token,
        "content": content,
        "contentType": 1,
        "uids": uids,
        "url": url
    }
    response = requests.post(api_url, headers=headers, data=json.dumps(data))
    return response.json()

# 示例调用
app_token = '你的AppToken'
content = '这是测试消息内容'
uids = ['用户UID1', '用户UID2']
url = 'https://example.com'
result = send_message(app_token, content, uids, url)
print(result)

6. 集成到项目中

你可以将上述示例代码集成到你的项目中,根据项目的需求进行修改和扩展。

7. 查看推送结果

  1. 登录 WxPusher 后台,进入“消息管理”页面。
  2. 在消息管理页面,可以查看你发送的消息记录以及发送状态。

8. 官方文档与支持

详细的 API 文档和更多功能介绍,请参考 WxPusher 官方文档

WxPusher 常见的使用场景:

1. 系统通知

示例:

  • 服务器状态通知:当服务器出现异常或恢复正常时,及时推送消息通知管理员。
  • 应用程序错误通知:当应用程序发生错误时,推送错误信息给开发人员。
import requests

def send_server_status_notification(app_token, content, uids):
    api_url = 'https://wxpusher.zjiecode.com/api/send/message'
    headers = {
        'Content-Type': 'application/json'
    }
    data = {
        "appToken": app_token,
        "content": content,
        "contentType": 1,
        "uids": uids
    }
    response = requests.post(api_url, headers=headers, json=data)
    return response.json()

# 示例调用
app_token = '你的AppToken'
content = '服务器状态:异常'
uids = ['用户UID1']
send_server_status_notification(app_token, content, uids)

2. 事务提醒

示例:

  • 会议提醒:在会议开始前推送会议通知。
  • 待办事项提醒:提醒用户待办事项的截止时间。
def send_reminder(app_token, content, uids, url=None):
    api_url = 'https://wxpusher.zjiecode.com/api/send/message'
    headers = {
        'Content-Type': 'application/json'
    }
    data = {
        "appToken": app_token,
        "content": content,
        "contentType": 1,
        "uids": uids,
        "url": url
    }
    response = requests.post(api_url, headers=headers, json=data)
    return response.json()

# 示例调用
app_token = '你的AppToken'
content = '会议提醒:明天上午10点'
uids = ['用户UID1']
url = 'https://example.com/meeting'
send_reminder(app_token, content, uids, url)

3. 营销推广

示例:

  • 新产品发布:通知用户新产品的上线信息。
  • 促销活动:推送促销活动和优惠信息。
def send_promotion(app_token, content, uids, url=None):
    api_url = 'https://wxpusher.zjiecode.com/api/send/message'
    headers = {
        'Content-Type': 'application/json'
    }
    data = {
        "appToken": app_token,
        "content": content,
        "contentType": 1,
        "uids": uids,
        "url": url
    }
    response = requests.post(api_url, headers=headers, json=data)
    return response.json()

# 示例调用
app_token = '你的AppToken'
content = '新产品发布:最新款智能手表'
uids = ['用户UID1', '用户UID2']
url = 'https://example.com/new-product'
send_promotion(app_token, content, uids, url)

4. 用户互动

示例:

  • 问卷调查:发送问卷调查链接给用户。
  • 活动报名:推送活动报名的通知和链接。
def send_interaction(app_token, content, uids, url=None):
    api_url = 'https://wxpusher.zjiecode.com/api/send/message'
    headers = {
        'Content-Type': 'application/json'
    }
    data = {
        "appToken": app_token,
        "content": content,
        "contentType": 1,
        "uids": uids,
        "url": url
    }
    response = requests.post(api_url, headers=headers, json=data)
    return response.json()

# 示例调用
app_token = '你的AppToken'
content = '请填写问卷调查'
uids = ['用户UID1', '用户UID2']
url = 'https://example.com/survey'
send_interaction(app_token, content, uids, url)

5. 新闻通知

示例:

  • 每日新闻:推送每日新闻摘要。
  • 紧急新闻:推送突发新闻和重要事件通知。
def send_news(app_token, content, uids, url=None):
    api_url = 'https://wxpusher.zjiecode.com/api/send/message'
    headers = {
        'Content-Type': 'application/json'
    }
    data = {
        "appToken": app_token,
        "content": content,
        "contentType": 1,
        "uids": uids,
        "url": url
    }
    response = requests.post(api_url, headers=headers, json=data)
    return response.json()

# 示例调用
app_token = '你的AppToken'
content = '今日新闻摘要'
uids = ['用户UID1', '用户UID2']
url = 'https://example.com/news'
send_news(app_token, content, uids, url)
### 如何在Spring Boot项目中集成WxPusher以实现微信消息的定时推送功能 要在Spring Boot项目中集成WxPusher并实现微信消息的定时推送功能,可以按照以下方法完成。 #### 1. 添加依赖项 首先,在`pom.xml`文件中引入必要的依赖项。这些依赖项包括Spring Web支持以及可能需要用到的日志库或其他工具库。 ```xml <dependencies> <!-- Spring Boot Starter Web --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!-- WxPusher Java SDK (如果存在官方SDK) --> <dependency> <groupId>com.github.qiuxj</groupId> <artifactId>wxpusher-java-sdk</artifactId> <version>最新本号</version> </dependency> <!-- 如果没有官方SDK,则可以过RestTemplate自行封装请求逻辑 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-rest</artifactId> </dependency> </dependencies> ``` 上述代码片段展示了如何过Maven管理项目的外部依赖[^2]。 --- #### 2. 创建配置类 创建一个用于存储WxPusher API密钥和其他必要参数的配置类。该类常会加载来自`application.yml`或`application.properties`中的属性。 ```java import org.springframework.beans.factory.annotation.Value; import org.springframework.context.annotation.Configuration; @Configuration public class WxPusherConfig { @Value("${wxpusher.appToken}") private String appToken; // 应用令牌 @Value("${wx.pusher.uids}") private String uids; // 接收者UID列表 public String getAppToken() { return appToken; } public String getUids() { return uids; } } ``` 此部分代码定义了一个简单的Java Bean来读取配置文件中的敏感数据[^1]。 --- #### 3. 编写服务层代码 编写一个专门负责处理消息推送的服务类。这个类将调用WxPusher的API接口并将结果返回给调用方。 ```java import com.fasterxml.jackson.databind.ObjectMapper; import org.springframework.http.HttpEntity; import org.springframework.http.HttpHeaders; import org.springframework.stereotype.Service; import org.springframework.web.client.RestTemplate; @Service public class WxPusherService { private static final String WX_PUSHER_URL = "https://wxpusher.zjiecode.com/api/send/message"; public void sendMessage(String content, String title, String appToken, String uids) throws Exception { RestTemplate restTemplate = new RestTemplate(); HttpHeaders headers = new HttpHeaders(); headers.add("Content-Type", "application/json"); ObjectMapper objectMapper = new ObjectMapper(); String requestBody = "{ \"appToken\": \"" + appToken + "\", \"content\": \"" + content.replace("\n", "<br>") + "\", \"summary\": \"" + title + "\", \"uidList\": [" + uids + "] }"; HttpEntity<String> requestEntity = new HttpEntity<>(requestBody, headers); String response = restTemplate.postForObject(WX_PUSHER_URL, requestEntity, String.class); System.out.println(response); // 可选:打印响应日志以便调试 } } ``` 这段代码实现了向指定用户发送自定义消息的功能。 --- #### 4. 设置定时任务 利用Spring框架内置的支持机制——@Scheduled注解,可以在固定时间间隔或者特定时刻触发消息推送操作。 ```java import org.springframework.scheduling.annotation.Scheduled; import org.springframework.stereotype.Component; @Component public class ScheduledTasks { private final WxPusherService wxPusherService; private final WxPusherConfig wxPusherConfig; public ScheduledTasks(WxPusherService wxPusherService, WxPusherConfig wxPusherConfig) { this.wxPusherService = wxPusherService; this.wxPusherConfig = wxPusherConfig; } @Scheduled(cron = "0 0/1 * * * ?") // 每分钟执行一次 public void sendPeriodicMessage() { try { String messageTitle = "测试标题"; String messageContent = "这是每分钟自动发送的一条测试消息。"; wxPusherService.sendMessage(messageContent, messageTitle, wxPusherConfig.getAppToken(), wxPusherConfig.getUids()); } catch (Exception e) { e.printStackTrace(); // 错误处理逻辑可根据实际需求调整 } } } ``` 这里设置了一种基于Cron表达式的调度策略,允许开发者灵活控制运行频率。 --- #### 5. 配置文件样例 最后一步是在资源目录下的`application.yml`里补充相应的键值对: ```yaml wxpusher: appToken: your_app_token_here # 替换为自己的App Token uids: '"your_uid"' # 替换为目标用户的唯一ID ``` 以上即完成了整个流程的设计与编码工作。 --- ### 注意事项 - **安全性**:切勿硬编码任何机密信息到源码当中;建议采用环境变量注入方式保护隐私。 - **异常捕获**:对于网络信过程中可能出现的各种状况做好充分预案。 - **性能优化**:当目标群体较大时考虑分批提交以免超出服务器负载限制。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

今晚务必早点睡

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值