|
@@ -1,17 +1,24 @@
|
|
package org.jeecg.modules.adweb.marketing.facebook.controller;
|
|
package org.jeecg.modules.adweb.marketing.facebook.controller;
|
|
|
|
|
|
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
|
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
|
|
|
+import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
|
|
|
+import com.google.gson.Gson;
|
|
|
|
+import com.google.gson.JsonObject;
|
|
|
|
+import com.restfb.*;
|
|
|
|
|
|
import io.swagger.v3.oas.annotations.tags.Tag;
|
|
import io.swagger.v3.oas.annotations.tags.Tag;
|
|
|
|
|
|
|
|
+import jakarta.annotation.Resource;
|
|
import lombok.extern.slf4j.Slf4j;
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
|
|
|
import org.jeecg.common.api.vo.Result;
|
|
import org.jeecg.common.api.vo.Result;
|
|
import org.jeecg.modules.adweb.marketing.facebook.entity.AdwebImFacebook;
|
|
import org.jeecg.modules.adweb.marketing.facebook.entity.AdwebImFacebook;
|
|
|
|
+import org.jeecg.modules.adweb.marketing.facebook.mapper.AdwebImFacebookMapper;
|
|
import org.jeecg.modules.adweb.marketing.facebook.service.IFacebookService;
|
|
import org.jeecg.modules.adweb.marketing.facebook.service.IFacebookService;
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
import org.springframework.web.bind.annotation.*;
|
|
import org.springframework.web.bind.annotation.*;
|
|
|
|
|
|
|
|
+import java.util.List;
|
|
import java.util.Optional;
|
|
import java.util.Optional;
|
|
|
|
|
|
@Tag(name = "Facebook - 帐号,广告,报表数据等")
|
|
@Tag(name = "Facebook - 帐号,广告,报表数据等")
|
|
@@ -22,6 +29,8 @@ public class FaceBookController {
|
|
|
|
|
|
@Autowired private IFacebookService facebookService;
|
|
@Autowired private IFacebookService facebookService;
|
|
|
|
|
|
|
|
+ @Resource private AdwebImFacebookMapper facebookMapper;
|
|
|
|
+
|
|
/** 获取站点绑定的Facebook account */
|
|
/** 获取站点绑定的Facebook account */
|
|
@GetMapping("/account/get")
|
|
@GetMapping("/account/get")
|
|
public Result<AdwebImFacebook> getFacebookAccount(@RequestParam String siteCode) {
|
|
public Result<AdwebImFacebook> getFacebookAccount(@RequestParam String siteCode) {
|
|
@@ -49,9 +58,118 @@ public class FaceBookController {
|
|
|
|
|
|
facebook.setSiteCode(siteCode);
|
|
facebook.setSiteCode(siteCode);
|
|
facebook.setCustomerId(customerId);
|
|
facebook.setCustomerId(customerId);
|
|
- facebook.setRefreshToken(refreshToken);
|
|
|
|
|
|
+ facebook.setAccessToken(refreshToken);
|
|
facebookService.saveOrUpdate(facebook);
|
|
facebookService.saveOrUpdate(facebook);
|
|
|
|
|
|
return Result.ok("添加成功");
|
|
return Result.ok("添加成功");
|
|
}
|
|
}
|
|
|
|
+
|
|
|
|
+ /** 获取Facebook聊天列表 */
|
|
|
|
+ @GetMapping("/chat/list")
|
|
|
|
+ public Result<List<String>> getFacebookChatList(@RequestParam String siteCode) {
|
|
|
|
+ log.info("getFacebookChatList: siteCode={}", siteCode);
|
|
|
|
+ String userAccessToken = getUserAccessTokenBySiteCode(siteCode);
|
|
|
|
+ try {
|
|
|
|
+ // 1.获取Page ID
|
|
|
|
+ // 创建一个Facebook客户端实例
|
|
|
|
+ DefaultFacebookClient client = new DefaultFacebookClient(Version.LATEST);
|
|
|
|
+ FacebookEndpoints endpoints =
|
|
|
|
+ new FacebookEndpoints() {
|
|
|
|
+ @Override
|
|
|
|
+ public String getGraphEndpoint() {
|
|
|
|
+ return "http://facebook-proxy.adwebcloud.com";
|
|
|
|
+ }
|
|
|
|
+ };
|
|
|
|
+
|
|
|
|
+ client = (DefaultFacebookClient) client.createClientWithAccessToken(userAccessToken);
|
|
|
|
+ client.setFacebookEndpointUrls(endpoints);
|
|
|
|
+
|
|
|
|
+ String response = client.fetchObject("me/accounts", String.class);
|
|
|
|
+ log.info("me/accounts: response={}", response);
|
|
|
|
+ Gson gson = new Gson();
|
|
|
|
+ // String转json对象
|
|
|
|
+ JsonObject jsonObject = gson.fromJson(response, JsonObject.class);
|
|
|
|
+ String pageId = jsonObject.get("data").getAsJsonArray().get(0).getAsJsonObject().get("id").getAsString();
|
|
|
|
+ String pageAccessToken = jsonObject.get("data").getAsJsonArray().get(0).getAsJsonObject().get("access_token")
|
|
|
|
+ .getAsString();
|
|
|
|
+
|
|
|
|
+ // 2.获取CONVERSATION-ID
|
|
|
|
+ client = (DefaultFacebookClient) client.createClientWithAccessToken(pageAccessToken);
|
|
|
|
+ client.setFacebookEndpointUrls(endpoints);
|
|
|
|
+ response = client.fetchObject(pageId + "/conversations", String.class);
|
|
|
|
+ log.info("pageId/conversations: response={}", response);
|
|
|
|
+ jsonObject = gson.fromJson(response, JsonObject.class);
|
|
|
|
+ String conversationId = jsonObject.get("data").getAsJsonArray().get(0).getAsJsonObject().get("id").getAsString();
|
|
|
|
+ // 3.获取MESSAGES-ID
|
|
|
|
+ response = client.fetchObject(conversationId + "?fields=messages", String.class);
|
|
|
|
+ log.info("conversationId/messages: response={}", response);
|
|
|
|
+ // 4.获取MESSAGE详情
|
|
|
|
+ jsonObject = gson.fromJson(response, JsonObject.class);
|
|
|
|
+ String messageId = jsonObject.get("messages").getAsJsonObject().get("data").getAsJsonArray().get(0).getAsJsonObject().get("id").getAsString();
|
|
|
|
+ response = client.fetchObject(messageId + "?fields=id,created_time,from,to,message", String.class);
|
|
|
|
+ log.info("messageId: response={}", response);
|
|
|
|
+ } catch (Exception e) {
|
|
|
|
+ e.printStackTrace();
|
|
|
|
+ }
|
|
|
|
+ return Result.ok();
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ private String getUserAccessTokenBySiteCode(String siteCode) {
|
|
|
|
+ // 根据siteCode从数据库获取用户的access token
|
|
|
|
+ QueryWrapper<AdwebImFacebook> adwebImFacebookQueryWrapper = new QueryWrapper<>();
|
|
|
|
+ adwebImFacebookQueryWrapper.eq("site_code", siteCode);
|
|
|
|
+ AdwebImFacebook adwebImFacebook = facebookMapper.selectOne(adwebImFacebookQueryWrapper);
|
|
|
|
+ return adwebImFacebook.getAccessToken();
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ public static void main(String[] args) {
|
|
|
|
+ // 替换为你的App ID和App Secret
|
|
|
|
+ String appId = "961285249321302";
|
|
|
|
+ String appSecret = "57b38cafc19b10f23eda72ecfc465810";
|
|
|
|
+ String userAccessToken = "EAANqSKnQSVYBO98rzCotioN4hZBCDOzTFMdhqNuLAGclliUOwdMcuLJKYNfmRsQC8mXxdz1EnDklHduZBMG6QAD3pvKmmmHQYvugMvlAWaCKRZBmA05ZAMuTbdI9GY5hMfANw5wlliW47o1gWEMJUY5b12hNSZCdsEhlQZCwTX8f665yibO14nCtTh79vJcP7mj1MhFNVZCdcFfacuZAzAZDZD";
|
|
|
|
+
|
|
|
|
+ try {
|
|
|
|
+ // 1.获取Page ID
|
|
|
|
+ // 创建一个Facebook客户端实例
|
|
|
|
+ DefaultFacebookClient client = new DefaultFacebookClient(Version.LATEST);
|
|
|
|
+ FacebookEndpoints endpoints =
|
|
|
|
+ new FacebookEndpoints() {
|
|
|
|
+ @Override
|
|
|
|
+ public String getGraphEndpoint() {
|
|
|
|
+ return "http://facebook-proxy.adwebcloud.com";
|
|
|
|
+ }
|
|
|
|
+ };
|
|
|
|
+
|
|
|
|
+ client = (DefaultFacebookClient) client.createClientWithAccessToken(userAccessToken);
|
|
|
|
+ client.setFacebookEndpointUrls(endpoints);
|
|
|
|
+
|
|
|
|
+ String response = client.fetchObject("me/accounts", String.class);
|
|
|
|
+ log.info("me/accounts: response={}", response);
|
|
|
|
+ Gson gson = new Gson();
|
|
|
|
+ // String转json对象
|
|
|
|
+ JsonObject jsonObject = gson.fromJson(response, JsonObject.class);
|
|
|
|
+ String pageId = jsonObject.get("data").getAsJsonArray().get(0).getAsJsonObject().get("id").getAsString();
|
|
|
|
+ String pageAccessToken = jsonObject.get("data").getAsJsonArray().get(0).getAsJsonObject().get("access_token")
|
|
|
|
+ .getAsString();
|
|
|
|
+
|
|
|
|
+ // 2.获取CONVERSATION-ID
|
|
|
|
+ client = (DefaultFacebookClient) client.createClientWithAccessToken(pageAccessToken);
|
|
|
|
+ client.setFacebookEndpointUrls(endpoints);
|
|
|
|
+ response = client.fetchObject(pageId + "/conversations", String.class);
|
|
|
|
+ log.info("pageId/conversations: response={}", response);
|
|
|
|
+ jsonObject = gson.fromJson(response, JsonObject.class);
|
|
|
|
+ String conversationId = jsonObject.get("data").getAsJsonArray().get(0).getAsJsonObject().get("id").getAsString();
|
|
|
|
+ // 3.获取MESSAGES-ID
|
|
|
|
+ response = client.fetchObject(conversationId + "?fields=messages", String.class);
|
|
|
|
+ log.info("conversationId/messages: response={}", response);
|
|
|
|
+ // 4.获取MESSAGE详情
|
|
|
|
+ jsonObject = gson.fromJson(response, JsonObject.class);
|
|
|
|
+ String messageId = jsonObject.get("messages").getAsJsonObject().get("data").getAsJsonArray().get(0).getAsJsonObject().get("id").getAsString();
|
|
|
|
+ response = client.fetchObject(messageId + "?fields=id,created_time,from,to,message", String.class);
|
|
|
|
+ log.info("messageId: response={}", response);
|
|
|
|
+ } catch (Exception e) {
|
|
|
|
+ e.printStackTrace();
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ }
|
|
}
|
|
}
|