关于 wechaty 使用一周的感受与教程

2022-04-11 14:32:44 浏览数 (1)

关于 wechaty 使用一周的感受与教程

你被骗了!!只有标题和这一句话使用中文,其他的都是英文,请手动点击翻译

代码语言:javascript复制
Type: Individual user
usage mode: Store messages and manage messages

How to Use

What are you going to do

Make for the company

Make it for yourself

Eazy to use

How to find Problems

  1. Check the issues
  2. Chat with everyone

example:

代码语言:javascript复制
Network error: https://github.com/wechaty/wechaty/issues?q=network error
Rpc error: https://github.com/wechaty/go-wechaty/search?q=err:rpc error: code = Unavailable desc = connection&type=issues

How to find Document

Api : Message, Room, Friendship

Polyglot : Go, Java, Python

Puppet Providers : WeChat, XP, WeChat4U

Puppet Services : PadLocal, WXWork

Specifications : Wechaty, Plugin, Puppet, Service, Token, Gateway

Quick start

@Gateway

代码语言:javascript复制
## windows puppet-xp port 30000

cnpm install -g wechaty@1.11 wechaty-puppet-xp
@wechaty gateway --puppet wechaty-puppet-xp --port 30000 --token insecure_559c97f1-c6a9-39b0-8f98-288b05f315cd --puppet-token f5c70044-42ea-12ca-ae78-b5ece72dde59

## save to bat

@Server

代码语言:javascript复制
// 1. Viper Get config
// 2. viper.Getstring set Arg (wechaty-puppet-xp)
// 3. Third party robot corpus(tuling123)
// 4. WebHook(Dingding,Telegram)

// >>> config.yaml <<<
BOT:
  CHAT: off
Wechaty:
  WECHATY_PUPPET: wechaty-puppet-xp
...

// >>> main.go <<<
package main
import (
	"github.com/spf13/viper"
	"github.com/wechaty/go-wechaty/wechaty"
  // ...
)
func init() {
	log.SetPrefix("[WechatyBot] ") // 设置日志前缀
	log.SetFlags(log.Ltime | log.Lshortfile)

	success := "[33[01;32m✓33[0m] "
	faild := "[33[01;31m✗33[0m] "
	info := "[33[01;33m➜33[0m] "
	rootPath, _ := os.Getwd()

	viper.Set("rootPath", rootPath)
	//...
	if err = viper.ReadInConfig(); err != nil {
		log.Printf("%v读取配置文件失败%v", os.Getenv("faild"), err)
	} else {
		viper.Set("success", success)
		viper.Set("rootPath", rootPath)
		//...
	}
}

func main() {
	Api.DingBot() // Check the dingding configuration
		var bot = wechaty.NewWechaty(wechaty.WithPuppetOption(wp.Option{
		Token:    viper.GetString("Wechaty.WECHATY_PUPPET_SERVICE_TOKEN"),
		Endpoint: viper.GetString("Wechaty.URL"),
	}))
	// 根据观察,这里不要设置 TOKEN 了,全部用Option吧,少年
        // Token 要填写 WECHATY_PUPPET_SERVICE_TOKEN
	bot.OnScan(onScan).
		OnLogin(onLogin).
		OnLogout(onLogout).
		OnError(onError).
		OnMessage(onMessage).
		OnFriendship(onFriendShip)
	bot.DaemonStart()
}

func onMessage(context *wechaty.Context, message *user.Message) {
	if message.Self() {
		log.Printf("%sBot Chat:%s %s n", viper.GetString("info"), message.From().Name(), message.Text())
	} else if message.From().ID() == viper.GetString("AdminID") {
		if message.Text() == "chat.off" {
			setEnv("CHAT", "off")
		} else if message.Text() == "chat.on" {
			setEnv("CHAT", "on")
		} else {
			Admin.Admin(context, message)
		}
	} else if message.Room() != nil {
    	if viper.GetString("CHAT") !="off"{
				// Private.Chat(context, message)
				message.Say(TulingBot(message.Text())) // TulingBot use tuling123 api
        log.Printf("%s%s @我了 ta 说: %s, 回复了: %s", viper.GetString("info"), message.From().Name(), message.Text(), reply)
		}
	} else {
		//	如果不是群消息,那就是联系人消息
		if viper.GetString("CHAT") !="off"{
      if string(message.Text())=="在吗?" {
        message.Say("来了老弟!")
      }
		}
	}
}
...............
Auth WrchatID == XRSec_MSG

Debug

  1. I am using the xp protocol and go language, so I need a gateway as support. It seems that the new version of wechaty will automatically determine whether the IP of the gateway and the server is equal to the IP of the same exit. If they are equal, there is no need to open the port, and no public IP is required
  2. The new version requires TLS encryption, if you need to cancel encryption, please set WECHATY_PUPPET_SERVICE_NO_TLS_INSECURE_SERVER=true parameter @environment

XRSec has the right to modify and interpret this article. If you want to reprint or disseminate this article, you must ensure the integrity of this article, including all contents such as copyright notice. Without the permission of the author, the content of this article shall not be modified or increased or decreased arbitrarily, and it shall not be used for commercial purposes in any way

0 人点赞