message

package
v0.0.0-...-0447b3e Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 17, 2016 License: GPL-3.0, Apache-2.0 Imports: 0 Imported by: 0

README

被动接收消息和回复示例

package main

import (
	"fmt"
	"log"
	"net/http"

	"github.com/chanxuehong/wechat/mp"
	"github.com/chanxuehong/wechat/mp/message/request"
	"github.com/chanxuehong/wechat/mp/message/response"
	"github.com/chanxuehong/wechat/util"
)

// 处理普通文本消息, 原样返回
func TextMessageHandler(w http.ResponseWriter, r *mp.Request) {
	textReq := request.GetText(r.MixedMsg)
	textResp := response.NewText(textReq.FromUserName, textReq.ToUserName,
		textReq.CreateTime, textReq.Content)

	if err := mp.WriteAESResponse(w, r, textResp); err != nil {
		log.Println(err)
	}
}

// 上报地理位置事件处理
func LocationEventHandler(w http.ResponseWriter, r *mp.Request) {
	event := request.GetLocationEvent(r.MixedMsg)
	fmt.Println(event) // 处理事件
}

func main() {
	aesKey, err := util.AESKeyDecode("encodedAESKey")
	if err != nil {
		panic(err)
	}

	messageServeMux := mp.NewMessageServeMux()
	messageServeMux.MessageHandleFunc(request.MsgTypeText, TextMessageHandler)
	messageServeMux.EventHandleFunc(request.EventTypeLocation, LocationEventHandler)

	wechatServer := mp.NewDefaultWechatServer("id", "token", "appid", aesKey, messageServeMux)

	wechatServerFrontend := mp.NewWechatServerFrontend(wechatServer, nil)

	http.Handle("/wechat", wechatServerFrontend)
	http.ListenAndServe(":80", nil)
}

Documentation

Overview

消息(接收和发送)接口.

Directories

Path Synopsis
客服主动回复消息.
客服主动回复消息.
群发消息.
群发消息.
mass2all
群发消息给所有用户.
群发消息给所有用户.
mass2group
根据分组进行群发消息.
根据分组进行群发消息.
mass2users
根据OpenID列表群发消息.
根据OpenID列表群发消息.
preview
预览接口.
预览接口.
被动接收基本的消息(事件).
被动接收基本的消息(事件).
被动回复用户消息.
被动回复用户消息.
模板消息接口.
模板消息接口.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL