Sign up for a robot app at Feishu

  • Configure basic information, such as the name and icon, and obtain the appID and AppSecret of an application

  • Start robot function

  • Configure the robot callback URL to obtain the EncryptKey and Token

Note: At this point you need to verify that the URL is valid. See the code section

  • Release application

Install the FastWego/FeISHU development SDK

go get -u github.com/fastwego/feishu

Development robot

configuration

  • Update the flightbook application configuration to.envFile:

AppId=xxx
AppSecret=xxx
VerificationToken=xxx
EncryptKey=xxx

LISTEN=:80
Copy the code
  • Write code:
package main

import (
	"context"
	"encoding/json"
	"fmt"
	"log"
	"net/http"
	"os"
	"os/signal"
	"syscall"
	"time"

	"github.com/fastwego/feishu/apis/message"
	"github.com/fastwego/feishu/types/event_types"

	"github.com/fastwego/feishu"

	"github.com/spf13/viper"

	"github.com/gin-gonic/gin"
)

var App *feishu.App

func init(a) {
	// Load the configuration file
	viper.SetConfigFile(".env")
	_ = viper.ReadInConfig()

	// Create an application instance
	App = feishu.NewApp(feishu.AppConfig{
		AppId:             viper.GetString("AppId"),
		AppSecret:         viper.GetString("AppSecret"),
		VerificationToken: viper.GetString("VerificationToken"),
		EncryptKey:        viper.GetString("EncryptKey"),})}func main(a) {

	router := gin.New()
	router.Use(gin.Logger(), gin.Recovery())

	// Receive the flybook event callback
	router.POST("/api/feishu/ding-dong-bot", DingDongBot)

	svr := &http.Server{
		Addr:    viper.GetString("LISTEN"),
		Handler: router,
	}

	go func(a) {
		err := svr.ListenAndServe()
		iferr ! =nil&& err ! = http.ErrServerClosed { log.Fatalln(err) } }() quit :=make(chan os.Signal)
	signal.Notify(quit, syscall.SIGINT, syscall.SIGTERM)
	<-quit

	timeout := time.Duration(5) * time.Second
	ctx, cancel := context.WithTimeout(context.Background(), timeout)
	defer cancel()

	iferr := svr.Shutdown(ctx); err ! =nil {
		log.Fatalln(err)
	}
}

// The robot responds
func DingDongBot(c *gin.Context) {

	event, err := App.Server.ParseEvent(c.Request)
	iferr ! =nil {
		return
	}

	switch event.(type) {
	case event_types.EventChallenge: / / check the url
		App.Server.Challenge(c.Writer, event.(event_types.EventChallenge))
	case event_types.EventMessageText:
		userMsg := event.(event_types.EventMessageText)
		fmt.Println(userMsg)

		replyTextMsg := struct {
			OpenId  string `json:"open_id"`
			MsgType string `json:"msg_type"`
			Content struct {
				Text string `json:"text"`
			} `json:"content"`
		}{
			OpenId:  userMsg.Event.OpenID,
			MsgType: "text",
			Content: struct {
				Text string `json:"text"`
			}{Text: "dong"},
		}

		data, err := json.Marshal(replyTextMsg)
		iferr ! =nil {
			fmt.Println(err)
			return
		}

		resp, err := message.Send(App, data)
		fmt.Println(string(resp), err)
	}
}

Copy the code

Compile & deploy to server

GOOS=linux go build

chmod +x ./ding-dong-bot && ./ding-dong-bot

Test sending messages

After the application is published, you can see the entry in the workbench

Send a message to the robot, and the robot replies to Dong

conclusion

Congratulations to you! Completed the development of a flying book robot in 5 minutes

Full demo code: github.com/fastwego/fe…