WechatTools.acceptFriendRequest(event)这段源码贴一下
时间: 2023-08-01 11:12:32 浏览: 44
以下是`WechatTools.acceptFriendRequest(event)`方法的源代码:
```java
/**
* 接受好友请求
*
* @param event 好友请求事件
* @return 新的好友对象
*/
public static User acceptFriendRequest(FriendRequestEvent event) throws WechatException {
User user = null;
try {
// 获取好友请求的详细信息
RecommendInfo recommendInfo = event.getRecommendInfo();
// 接受好友请求
String ticket = recommendInfo.getTicket();
String fromUserName = recommendInfo.getUserName();
String verifyContent = recommendInfo.getContent();
String type = recommendInfo.getType() + "";
user = wechatClient.acceptFriendRequest(ticket, fromUserName, verifyContent, type);
// 发送欢迎消息
String welcomeMsg = "您好,我是您的微信机器人。感谢您添加我为好友!";
wechatClient.sendMsg(user.getUserName(), welcomeMsg);
} catch (WechatException e) {
throw e;
} catch (Exception e) {
throw new WechatException("接受好友请求失败!", e);
}
return user;
}
```
在这段代码中,通过好友请求事件对象`FriendRequestEvent`获取好友请求的详细信息`RecommendInfo`,然后调用`wechatClient.acceptFriendRequest()`方法接受好友请求,并返回新的好友对象`User`。接受好友请求成功后,可以通过`wechatClient.sendMsg()`方法给新的好友发送欢迎消息。如果接受好友请求失败,会抛出`WechatException`异常。
相关推荐
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)