欧美一区二区三区老妇人-欧美做爰猛烈大尺度电-99久久夜色精品国产亚洲a-亚洲福利视频一区二区

如何使用springboot微信公眾號開發(fā)微信自動回復(fù)

這篇文章將為大家詳細(xì)講解有關(guān)如何使用springboot微信公眾號開發(fā)微信自動回復(fù),小編覺得挺實用的,因此分享給大家做個參考,希望大家閱讀完這篇文章后可以有所收獲。

創(chuàng)新互聯(lián)專注于臨西網(wǎng)站建設(shè)服務(wù)及定制,我們擁有豐富的企業(yè)做網(wǎng)站經(jīng)驗。 熱誠為您提供臨西營銷型網(wǎng)站建設(shè),臨西網(wǎng)站制作、臨西網(wǎng)頁設(shè)計、臨西網(wǎng)站官網(wǎng)定制、微信平臺小程序開發(fā)服務(wù),打造臨西網(wǎng)絡(luò)公司原創(chuàng)品牌,更為您提供臨西網(wǎng)站排名全網(wǎng)營銷落地服務(wù)。

效果圖

如何使用springboot微信公眾號開發(fā)微信自動回復(fù)

1.準(zhǔn)備工作

申請微信訂閱號(個人只能申請訂閱號,而且沒什么功能,也無法認(rèn)證),申請完畢,點擊 開發(fā)=>基本配置,如下圖:

如何使用springboot微信公眾號開發(fā)微信自動回復(fù)

服務(wù)器配置需要有 域名 80端口,我猜你沒有,這里推薦個實用工具,pagekite,下載鏈接,

這個工具需要 python2.7以上環(huán)境,還有郵箱一個,一個郵箱一個月,郵箱這東西大家懂得,

用pagekite申請完域名,就可以用自己的電腦做訂閱號服務(wù)器了.

2.服務(wù)器代碼

創(chuàng)建個springboot工程

pom.xml

<properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
    <java.version>1.8</java.version>
  </properties>
  <parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.5.9.RELEASE</version>
  </parent>


  <dependencies>
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-thymeleaf</artifactId>
    </dependency>
    <!-- 使thymeleaf支持h6標(biāo)簽 -->
    <dependency>
      <groupId>net.sourceforge.nekohtml</groupId>
      <artifactId>nekohtml</artifactId>
      <version>1.9.22</version>
    </dependency>
    <dependency>
			      <groupId>dom4j</groupId>
			      <artifactId>dom4j</artifactId>
			      <version>1.6.1</version>
		    </dependency>
  </dependencies>
  <build>
    <plugins>
      <plugin>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-maven-plugin</artifactId>
      </plugin>
    </plugins>
  </build>

入口類

@SpringBootApplication
public class WeChatApplication {

  public static void main(String[] args) {
    SpringApplication.run(WeChatApplication.class, args);
    System.out.println("====啟動成功====");
  }

}

application.properties,配置server.port=80

Controller,這個是處理微信請求,get方法就是微信公眾平臺 服務(wù)器配置url請求的路徑,post是處理用戶事件

@RestController
public class WeChatController {
  @Autowired
  private WeChatService weChatService;
  /**
   * 處理微信服務(wù)器發(fā)來的get請求,進(jìn)行簽名的驗證
   * 
   * signature 微信端發(fā)來的簽名
   * timestamp 微信端發(fā)來的時間戳
   * nonce   微信端發(fā)來的隨機(jī)字符串
   * echostr  微信端發(fā)來的驗證字符串
   */
  @GetMapping(value = "wechat")
  public String validate(@RequestParam(value = "signature") String signature,
      @RequestParam(value = "timestamp") String timestamp, 
      @RequestParam(value = "nonce") String nonce,
      @RequestParam(value = "echostr") String echostr) {
    return WeChatUtil.checkSignature(signature, timestamp, nonce) ? echostr : null;

  }
  /**
   * 此處是處理微信服務(wù)器的消息轉(zhuǎn)發(fā)的
   */
  @PostMapping(value = "wechat")
  public String processMsg(HttpServletRequest request) {
    // 調(diào)用核心服務(wù)類接收處理請求
    return weChatService.processRequest(request);    
  }
}

Service,處理post請求

/**
 * 核心服務(wù)類
 */
@Service
public class WeChatServiceImpl implements WeChatService{
  @Autowired
  private FeignUtil feignUtil;
  @Autowired
  private redisUtils redisUtils;
  public String processRequest(HttpServletRequest request) {
    // xml格式的消息數(shù)據(jù)
    String respXml = null;
    // 默認(rèn)返回的文本消息內(nèi)容
    String respContent;
    try {
      // 調(diào)用parseXml方法解析請求消息
      Map<String,String> requestMap = WeChatUtil.parseXml(request);
       // 消息類型
      String msgType = (String) requestMap.get(WeChatContant.MsgType);
      String mes = null;
      // 文本消息
      if (msgType.equals(WeChatContant.REQ_MESSAGE_TYPE_TEXT)) {
        mes =requestMap.get(WeChatContant.Content).toString();
        if(mes!=null&&mes.length()<2){
          List<ArticleItem> items = new ArrayList<>();
          ArticleItem item = new ArticleItem();
          item.setTitle("照片墻");
          item.setDescription("阿貍照片墻");
          item.setPicUrl("http://changhaiwx.pagekite.me/photo-wall/a/iali11.jpg");
          item.setUrl("http://changhaiwx.pagekite.me/page/photowall");
          items.add(item);
          
          item = new ArticleItem();
          item.setTitle("哈哈");
          item.setDescription("一張照片");
          item.setPicUrl("http://changhaiwx.pagekite.me/images/me.jpg");
          item.setUrl("http://changhaiwx.pagekite.me/page/index");
          items.add(item);
          
          item = new ArticleItem();
          item.setTitle("小游戲2048");
          item.setDescription("小游戲2048");
          item.setPicUrl("http://changhaiwx.pagekite.me/images/2048.jpg");
          item.setUrl("http://changhaiwx.pagekite.me/page/game2048");
          items.add(item);
          
          item = new ArticleItem();
          item.setTitle("百度");
          item.setDescription("百度一下");
          item.setPicUrl("https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1505100912368&di=69c2ba796aa2afd9a4608e213bf695fb&imgtype=0&src=http%3A%2F%2Ftx.haiqq.com%2Fuploads%2Fallimg%2F170510%2F0634355517-9.jpg");
          item.setUrl("http://www.baidu.com");
          items.add(item);
          
          respXml = WeChatUtil.sendArticleMsg(requestMap, items);
        }else if("我的信息".equals(mes)){
          Map<String, String> userInfo = getUserInfo(requestMap.get(WeChatContant.FromUserName));
          System.out.println(userInfo.toString());
          String nickname = userInfo.get("nickname");
          String city = userInfo.get("city");
          String province = userInfo.get("province");
          String country = userInfo.get("country");
          String headimgurl = userInfo.get("headimgurl");
          List<ArticleItem> items = new ArrayList<>();
          ArticleItem item = new ArticleItem();
          item.setTitle("你的信息");
          item.setDescription("昵稱:"+nickname+" 地址:"+country+" "+province+" "+city);
          item.setPicUrl(headimgurl);
          item.setUrl("http://www.baidu.com");
          items.add(item);
          
          respXml = WeChatUtil.sendArticleMsg(requestMap, items);
        }
      }
      // 圖片消息
      else if (msgType.equals(WeChatContant.REQ_MESSAGE_TYPE_IMAGE)) {
        respContent = "您發(fā)送的是圖片消息!";
        respXml = WeChatUtil.sendTextMsg(requestMap, respContent);
      }
      // 語音消息
      else if (msgType.equals(WeChatContant.REQ_MESSAGE_TYPE_VOICE)) {
        respContent = "您發(fā)送的是語音消息!";
        respXml = WeChatUtil.sendTextMsg(requestMap, respContent);
      }
      // 視頻消息
      else if (msgType.equals(WeChatContant.REQ_MESSAGE_TYPE_VIDEO)) {
        respContent = "您發(fā)送的是視頻消息!";
        respXml = WeChatUtil.sendTextMsg(requestMap, respContent);
      }
      // 地理位置消息
      else if (msgType.equals(WeChatContant.REQ_MESSAGE_TYPE_LOCATION)) {
        respContent = "您發(fā)送的是地理位置消息!";
        respXml = WeChatUtil.sendTextMsg(requestMap, respContent);
      }
      // 鏈接消息
      else if (msgType.equals(WeChatContant.REQ_MESSAGE_TYPE_LINK)) {
        respContent = "您發(fā)送的是鏈接消息!";
        respXml = WeChatUtil.sendTextMsg(requestMap, respContent);
      }
      // 事件推送
      else if (msgType.equals(WeChatContant.REQ_MESSAGE_TYPE_EVENT)) {
        // 事件類型
        String eventType = (String) requestMap.get(WeChatContant.Event);
        // 關(guān)注
        if (eventType.equals(WeChatContant.EVENT_TYPE_SUBSCRIBE)) {
          respContent = "謝謝您的關(guān)注!";
          respXml = WeChatUtil.sendTextMsg(requestMap, respContent);
        }
        // 取消關(guān)注
        else if (eventType.equals(WeChatContant.EVENT_TYPE_UNSUBSCRIBE)) {
          // TODO 取消訂閱后用戶不會再收到公眾賬號發(fā)送的消息,因此不需要回復(fù)
        }
        // 掃描帶參數(shù)二維碼
        else if (eventType.equals(WeChatContant.EVENT_TYPE_SCAN)) {
          // TODO 處理掃描帶參數(shù)二維碼事件
        }
        // 上報地理位置
        else if (eventType.equals(WeChatContant.EVENT_TYPE_LOCATION)) {
          // TODO 處理上報地理位置事件
        }
        // 自定義菜單
        else if (eventType.equals(WeChatContant.EVENT_TYPE_CLICK)) {
          // TODO 處理菜單點擊事件
        }
      }
      mes = mes == null ? "不知道你在干嘛" : mes;
      if(respXml == null)
        respXml = WeChatUtil.sendTextMsg(requestMap, mes);
      System.out.println(respXml);
      return respXml;
    } catch (Exception e) {
      e.printStackTrace();
    }
    return "";
    
  }
}

微信工具類(只寫了回復(fù)文本消息,和圖文消息,其他的都是大同小異)

package com.wechat.util;

import java.io.InputStream;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.servlet.http.HttpServletRequest;

import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

import com.wechat.bean.ArticleItem;

/**
 * 請求校驗工具類
 * 
 * @author 32950745
 *
 */
public class WeChatUtil {  

  /**
   * 驗證簽名
   * 
   * @param signature
   * @param timestamp
   * @param nonce
   * @return
   */
  public static boolean checkSignature(String signature, String timestamp, String nonce) {
    String[] arr = new String[] { WeChatContant.TOKEN, timestamp, nonce };
    // 將token、timestamp、nonce三個參數(shù)進(jìn)行字典序排序
    // Arrays.sort(arr);
    sort(arr);
    StringBuilder content = new StringBuilder();
    for (int i = 0; i < arr.length; i++) {
      content.append(arr[i]);
    }
    MessageDigest md = null;
    String tmpStr = null;

    try {
      md = MessageDigest.getInstance("SHA-1");
      // 將三個參數(shù)字符串拼接成一個字符串進(jìn)行sha1加密
      byte[] digest = md.digest(content.toString().getBytes());
      tmpStr = byteToStr(digest);
    } catch (NoSuchAlgorithmException e) {
      e.printStackTrace();
    }

    content = null;
    // 將sha1加密后的字符串可與signature對比,標(biāo)識該請求來源于微信
    return tmpStr != null ? tmpStr.equals(signature.toUpperCase()) : false;
  }

  /**
   * 將字節(jié)數(shù)組轉(zhuǎn)換為十六進(jìn)制字符串
   * 
   * @param byteArray
   * @return
   */
  private static String byteToStr(byte[] byteArray) {
    String strDigest = "";
    for (int i = 0; i < byteArray.length; i++) {
      strDigest += byteToHexStr(byteArray[i]);
    }
    return strDigest;
  }

  /**
   * 將字節(jié)轉(zhuǎn)換為十六進(jìn)制字符串
   * 
   * @param mByte
   * @return
   */
  private static String byteToHexStr(byte mByte) {
    char[] Digit = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' };
    char[] tempArr = new char[2];
    tempArr[0] = Digit[(mByte >>> 4) & 0X0F];
    tempArr[1] = Digit[mByte & 0X0F];

    String s = new String(tempArr);
    return s;
  }

  private static void sort(String a[]) {
    for (int i = 0; i < a.length - 1; i++) {
      for (int j = i + 1; j < a.length; j++) {
        if (a[j].compareTo(a[i]) < 0) {
          String temp = a[i];
          a[i] = a[j];
          a[j] = temp;
        }
      }
    }
  }

  /**
   * 解析微信發(fā)來的請求(xml)
   * 
   * @param request
   * @return
   * @throws Exception
   */
  @SuppressWarnings({ "unchecked"})
  public static Map<String,String> parseXml(HttpServletRequest request) throws Exception {
    // 將解析結(jié)果存儲在HashMap中
    Map<String,String> map = new HashMap<String,String>();

    // 從request中取得輸入流
    InputStream inputStream = request.getInputStream();
    // 讀取輸入流
    SAXReader reader = new SAXReader();
    Document document = reader.read(inputStream);
    // 得到xml根元素
    Element root = document.getRootElement();
    // 得到根元素的所有子節(jié)點
    List<Element> elementList = root.elements();
    // 遍歷所有子節(jié)點
    for (Element e : elementList)
      map.put(e.getName(), e.getText());

    // 釋放資源
    inputStream.close();
    inputStream = null;
    return map;
  }

  public static String mapToXML(Map map) {
    StringBuffer sb = new StringBuffer();
    sb.append("<xml>");
    mapToXML2(map, sb);
    sb.append("</xml>");
    try {
      return sb.toString();
    } catch (Exception e) {
    }
    return null;
  }

  private static void mapToXML2(Map map, StringBuffer sb) {
    Set set = map.keySet();
    for (Iterator it = set.iterator(); it.hasNext();) {
      String key = (String) it.next();
      Object value = map.get(key);
      if (null == value)
        value = "";
      if (value.getClass().getName().equals("java.util.ArrayList")) {
        ArrayList list = (ArrayList) map.get(key);
        sb.append("<" + key + ">");
        for (int i = 0; i < list.size(); i++) {
          HashMap hm = (HashMap) list.get(i);
          mapToXML2(hm, sb);
        }
        sb.append("</" + key + ">");

      } else {
        if (value instanceof HashMap) {
          sb.append("<" + key + ">");
          mapToXML2((HashMap) value, sb);
          sb.append("</" + key + ">");
        } else {
          sb.append("<" + key + "><![CDATA[" + value + "]]></" + key + ">");
        }

      }

    }
  }
  /**
   * 回復(fù)文本消息
   * @param requestMap
   * @param content
   * @return
   */
   public static String sendTextMsg(Map<String,String> requestMap,String content){
    
    Map<String,Object> map=new HashMap<String, Object>();
    map.put("ToUserName", requestMap.get(WeChatContant.FromUserName));
    map.put("FromUserName", requestMap.get(WeChatContant.ToUserName));
    map.put("MsgType", WeChatContant.RESP_MESSAGE_TYPE_TEXT);
    map.put("CreateTime", new Date().getTime());
    map.put("Content", content);
    return mapToXML(map);
  }
   /**
   * 回復(fù)圖文消息
   * @param requestMap
   * @param items
   * @return
   */
  public static String sendArticleMsg(Map<String,String> requestMap,List<ArticleItem> items){
    if(items == null || items.size()<1){
      return "";
    }
    Map<String,Object> map=new HashMap<String, Object>();
    map.put("ToUserName", requestMap.get(WeChatContant.FromUserName));
    map.put("FromUserName", requestMap.get(WeChatContant.ToUserName));
    map.put("MsgType", "news");
    map.put("CreateTime", new Date().getTime());
    List<Map<String,Object>> Articles=new ArrayList<Map<String,Object>>(); 
    for(ArticleItem itembean : items){
      Map<String,Object> item=new HashMap<String, Object>();
      Map<String,Object> itemContent=new HashMap<String, Object>();
      itemContent.put("Title", itembean.getTitle());
      itemContent.put("Description", itembean.getDescription());
      itemContent.put("PicUrl", itembean.getPicUrl());
      itemContent.put("Url", itembean.getUrl());
      item.put("item",itemContent);
      Articles.add(item);
    }
    map.put("Articles", Articles);
    map.put("ArticleCount", Articles.size());
    return mapToXML(map);
  }
  
}

微信常量

public class WeChatContant {
   //APPID
  public static final String appID = "appid";
  //appsecret
  public static final String appsecret = "appsecret";
  // Token
  public static final String TOKEN = "zch";
  public static final String RESP_MESSAGE_TYPE_TEXT = "text";
  public static final Object REQ_MESSAGE_TYPE_TEXT = "text";
  public static final Object REQ_MESSAGE_TYPE_IMAGE = "image";
  public static final Object REQ_MESSAGE_TYPE_VOICE = "voice";
  public static final Object REQ_MESSAGE_TYPE_VIDEO = "video";
  public static final Object REQ_MESSAGE_TYPE_LOCATION = "location";
  public static final Object REQ_MESSAGE_TYPE_LINK = "link";
  public static final Object REQ_MESSAGE_TYPE_EVENT = "event";
  public static final Object EVENT_TYPE_SUBSCRIBE = "SUBSCRIBE";
  public static final Object EVENT_TYPE_UNSUBSCRIBE = "UNSUBSCRIBE";
  public static final Object EVENT_TYPE_SCAN = "SCAN";
  public static final Object EVENT_TYPE_LOCATION = "LOCATION";
  public static final Object EVENT_TYPE_CLICK = "CLICK";
  
  public static final String FromUserName = "FromUserName";
  public static final String ToUserName = "ToUserName";
  public static final String MsgType = "MsgType";
  public static final String Content = "Content";
  public static final String Event = "Event";
  
}

圖文消息實體bean

public class ArticleItem {
  private String Title;
  private String Description;
  private String PicUrl;
  private String Url;
  public String getTitle() {
    return Title;
  }
  public void setTitle(String title) {
    Title = title;
  }
  public String getDescription() {
    return Description;
  }
  public void setDescription(String description) {
    Description = description;
  }
  public String getPicUrl() {
    return PicUrl;
  }
  public void setPicUrl(String picUrl) {
    PicUrl = picUrl;
  }
  public String getUrl() {
    return Url;
  }
  public void setUrl(String url) {
    Url = url;
  }
  
}

好了運行項目,在公眾平臺配置好url,個人訂閱號完成.

關(guān)于“如何使用springboot微信公眾號開發(fā)微信自動回復(fù)”這篇文章就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,使各位可以學(xué)到更多知識,如果覺得文章不錯,請把它分享出去讓更多的人看到。

本文題目:如何使用springboot微信公眾號開發(fā)微信自動回復(fù)
分享URL:http://chinadenli.net/article26/ppsccg.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站改版、做網(wǎng)站、營銷型網(wǎng)站建設(shè)、網(wǎng)站策劃、網(wǎng)站設(shè)計公司、商城網(wǎng)站

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

成都網(wǎng)站建設(shè)公司
日韩在线视频精品中文字幕| 中文人妻精品一区二区三区四区| 国产a天堂一区二区专区| 日韩精品在线观看完整版| 欧美黑人黄色一区二区| 黄片美女在线免费观看| 欧美日韩无卡一区二区| 在线观看国产成人av天堂野外| 少妇丰满a一区二区三区| 91日韩欧美国产视频| 国产综合香蕉五月婷在线| 久久夜色精品国产高清不卡| 欧美精品久久男人的天堂| 国产一级内片内射免费看 | 91日韩欧美在线视频| 国产免费自拍黄片免费看| 亚洲伦片免费偷拍一区| 日韩精品区欧美在线一区| 99久久国产精品成人观看| 欧美一区二区三区喷汁尤物| 欧美一区二区三区五月婷婷 | 久久99一本色道亚洲精品| 日本精品最新字幕视频播放| 男人的天堂的视频东京热| 亚洲中文字幕三区四区| 国产精品午夜一区二区三区| 国产精品日本女优在线观看| 麻豆tv传媒在线观看| 后入美臀少妇一区二区| 日韩成人h视频在线观看| 日本女优一区二区三区免费| 麻豆视传媒短视频免费观看| 99久久国产综合精品二区| 国产精品不卡免费视频| 麻豆国产精品一区二区| 韩国日本欧美国产三级| 青青操日老女人的穴穴| 成人午夜激情免费在线| 福利专区 久久精品午夜| 国产成人在线一区二区三区| 亚洲国产成人爱av在线播放下载|