前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >关于 wechaty 使用一周的感受与教程

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

作者头像
XRSec
发布2022-04-11 14:32:44
1.8K0
发布2022-04-11 14:32:44
举报
文章被收录于专栏:XRSec.BlogXRSec.Blog

关于 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%3Arpc+error%3A+code+%3D+Unavailable+desc+%3D+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 := "[\033[01;32m✓\033[0m] "
	faild := "[\033[01;31m✗\033[0m] "
	info := "[\033[01;33m➜\033[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

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2022-04-05,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 关于 wechaty 使用一周的感受与教程
    • How to Use
      • What are you going to do
    • How to find Problems
      • How to find Document
        • Quick start
          • Debug
          相关产品与服务
          SSL 证书
          腾讯云 SSL 证书(SSL Certificates)为您提供 SSL 证书的申请、管理、部署等服务,为您提供一站式 HTTPS 解决方案。
          领券
          问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档