puppet_win

package module
v0.0.0-...-c3cba88 Latest Latest
Warning

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

Go to latest
Published: Aug 1, 2024 License: Apache-2.0 Imports: 17 Imported by: 0

README

Go Wechaty Puppet Win

Getting started

  • STEP 1: Follow the tutorial to start wechat
  • STEP 2: Modify the WinApiServer, WebsocketHost, and WebsocketPort configurations in the example code
  • STEP 3: go run main.go
package main

import (
	"fmt"
	puppet_win "github.com/dchaofei/puppet-win"
	"github.com/mdp/qrterminal/v3"
	"log"
	"net/url"
	"os"
	"time"

	"github.com/wechaty/go-wechaty/wechaty"
	"github.com/wechaty/go-wechaty/wechaty-puppet/schemas"
	"github.com/wechaty/go-wechaty/wechaty/user"
)

func main() {
	puppetWin, err := puppet_win.NewPuppetWin(puppet_win.Options{
		WinApiServer:  "http://126.xxx.xx.xxx:8888/api/", // windows 机器注入 dll 启动的服务
		WebsocketHost: "127.0.0.1",                       // 本程序会启动 websocket 服务用于接收消息消息回调,确保本地址能被 windows 机器访问
		WebsocketPort: "25465",                           // 本程序会启动 websocket 服务用于接收消息消息回调,确保本端口能被 windows 机器访问
	})
	if err != nil {
		panic(err)
	}

	var bot = wechaty.NewWechaty(wechaty.WithPuppet(puppetWin))

	bot.OnScan(onScan).OnLogin(func(ctx *wechaty.Context, user *user.ContactSelf) {
		fmt.Printf("User %s logined\n", user.Name())
	}).OnMessage(onMessage).OnLogout(func(ctx *wechaty.Context, user *user.ContactSelf, reason string) {
		fmt.Printf("User %s logouted: %s\n", user, reason)
	})

	bot.DaemonStart()
}

func onMessage(ctx *wechaty.Context, message *user.Message) {
	log.Println(message)

	if message.Self() {
		log.Println("Message discarded because its outgoing")
	}

	if message.Age() > 2*60*time.Second {
		log.Println("Message discarded because its TOO OLD(than 2 minutes)")
	}

	if message.Type() != schemas.MessageTypeText || message.Text() != "#ding" {
		log.Println("Message discarded because it does not match 'ding'")
		return
	}

	// 1. reply 'dong'
	_, err := message.Say("dong")
	if err != nil {
		log.Println(err)
		return
	}
	log.Println("REPLY: dong")
}

func onScan(ctx *wechaty.Context, qrCode string, status schemas.ScanStatus, data string) {
	fmt.Printf("onScan: %s\n", status)
	if status == schemas.ScanStatusWaiting || status == schemas.ScanStatusTimeout {
		qrterminal.GenerateHalfBlock(qrCode, qrterminal.L, os.Stdout)

		qrcodeImageUrl := fmt.Sprintf("https://wechaty.js.org/qrcode/%s", url.QueryEscape(qrCode))
		fmt.Printf("onScan: %s - %s\n", status, qrcodeImageUrl)
		return
	}
}

TODO

  • room sync
  • contact sync
  • receive text msg
  • receive and parse app msg
  • send text msg
  • send url msg
  • send app msg
  • Implement all TODO, search TODO implement me in puppet_win.go
  • fix: "emit message message.Ready() err: contact not found"

Reference

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Options

type Options struct {
	WinApiServer  string // winApi 的地址:"http:/127.0.0.1:8888/api"
	RunRemote     bool   // 是否和 winapi 运行在同一台机器
	WebsocketHost string // 主要是用来注册给 winApi 进行消息通知,所以这个地址必须是 winApi 机器能访问通的: "127.0.0.1"
	WebsocketPort string // 本地需要监听websocket端口,会注册给 winApi 进行消息通知: "8888"
}

type PuppetWin

type PuppetWin struct {
	*wechatyPuppet.Puppet
	// contains filtered or unexported fields
}

func NewPuppetWin

func NewPuppetWin(o Options) (*PuppetWin, error)

func (*PuppetWin) ContactAlias

func (p *PuppetWin) ContactAlias(contactID string) (string, error)

func (*PuppetWin) ContactAvatar

func (p *PuppetWin) ContactAvatar(contactID string) (*filebox.FileBox, error)

func (*PuppetWin) ContactList

func (p *PuppetWin) ContactList() ([]string, error)

func (*PuppetWin) ContactQRCode

func (p *PuppetWin) ContactQRCode(contactID string) (string, error)

func (*PuppetWin) ContactRawPayload

func (p *PuppetWin) ContactRawPayload(contactID string) (*schemas.ContactPayload, error)

func (*PuppetWin) ContactSelfQRCode

func (p *PuppetWin) ContactSelfQRCode() (string, error)

func (*PuppetWin) Ding

func (p *PuppetWin) Ding(data string)

func (*PuppetWin) FriendshipAccept

func (p *PuppetWin) FriendshipAccept(friendshipID string) (err error)

func (*PuppetWin) FriendshipAdd

func (p *PuppetWin) FriendshipAdd(contactID, hello string) (err error)

func (*PuppetWin) FriendshipRawPayload

func (p *PuppetWin) FriendshipRawPayload(id string) (*schemas.FriendshipPayload, error)

func (*PuppetWin) FriendshipSearchPhone

func (p *PuppetWin) FriendshipSearchPhone(phone string) (string, error)

func (*PuppetWin) FriendshipSearchWeixin

func (p *PuppetWin) FriendshipSearchWeixin(weixin string) (string, error)

func (*PuppetWin) Logout

func (p *PuppetWin) Logout() error

func (*PuppetWin) MessageContact

func (p *PuppetWin) MessageContact(messageID string) (string, error)

func (*PuppetWin) MessageFile

func (p *PuppetWin) MessageFile(id string) (*filebox.FileBox, error)

func (*PuppetWin) MessageForward

func (p *PuppetWin) MessageForward(conversationID string, messageID string) (string, error)

MessageForward ...

func (*PuppetWin) MessageImage

func (p *PuppetWin) MessageImage(messageID string, imageType schemas.ImageType) (*filebox.FileBox, error)

func (*PuppetWin) MessageLocation

func (p *PuppetWin) MessageLocation(messageID string) (*schemas.LocationPayload, error)

func (*PuppetWin) MessageRawMiniProgramPayload

func (p *PuppetWin) MessageRawMiniProgramPayload(messageID string) (*schemas.MiniProgramPayload, error)

func (*PuppetWin) MessageRawPayload

func (p *PuppetWin) MessageRawPayload(id string) (*schemas.MessagePayload, error)

func (*PuppetWin) MessageRecall

func (p *PuppetWin) MessageRecall(messageID string) (bool, error)

func (*PuppetWin) MessageSendContact

func (p *PuppetWin) MessageSendContact(conversationID string, contactID string) (string, error)

func (*PuppetWin) MessageSendFile

func (p *PuppetWin) MessageSendFile(conversationID string, fileBox *filebox.FileBox) (string, error)

func (*PuppetWin) MessageSendLocation

func (p *PuppetWin) MessageSendLocation(conversationID string, payload *schemas.LocationPayload) (string, error)

func (*PuppetWin) MessageSendMiniProgram

func (p *PuppetWin) MessageSendMiniProgram(conversationID string, miniProgramPayload *schemas.MiniProgramPayload) (string, error)

func (*PuppetWin) MessageSendText

func (p *PuppetWin) MessageSendText(conversationID string, text string, mentionIDList ...string) (string, error)

func (*PuppetWin) MessageSendURL

func (p *PuppetWin) MessageSendURL(conversationID string, urlLinkPayload *schemas.UrlLinkPayload) (string, error)

func (*PuppetWin) MessageURL

func (p *PuppetWin) MessageURL(messageID string) (*schemas.UrlLinkPayload, error)

func (*PuppetWin) RoomAdd

func (p *PuppetWin) RoomAdd(roomID, contactID string) error

func (*PuppetWin) RoomAnnounce

func (p *PuppetWin) RoomAnnounce(roomID string) (string, error)

func (*PuppetWin) RoomAvatar

func (p *PuppetWin) RoomAvatar(roomID string) (*filebox.FileBox, error)

func (*PuppetWin) RoomCreate

func (p *PuppetWin) RoomCreate(contactIDList []string, topic string) (string, error)

func (*PuppetWin) RoomDel

func (p *PuppetWin) RoomDel(roomID, contactID string) error

func (*PuppetWin) RoomInvitationAccept

func (p *PuppetWin) RoomInvitationAccept(roomInvitationID string) error

func (*PuppetWin) RoomInvitationRawPayload

func (p *PuppetWin) RoomInvitationRawPayload(id string) (*schemas.RoomInvitationPayload, error)

func (*PuppetWin) RoomList

func (p *PuppetWin) RoomList() ([]string, error)

func (*PuppetWin) RoomMemberList

func (p *PuppetWin) RoomMemberList(roomID string) ([]string, error)

func (*PuppetWin) RoomMemberRawPayload

func (p *PuppetWin) RoomMemberRawPayload(roomID string, contactID string) (*schemas.RoomMemberPayload, error)

func (*PuppetWin) RoomQRCode

func (p *PuppetWin) RoomQRCode(roomID string) (string, error)

func (*PuppetWin) RoomQuit

func (p *PuppetWin) RoomQuit(roomID string) error

func (*PuppetWin) RoomRawPayload

func (p *PuppetWin) RoomRawPayload(id string) (*schemas.RoomPayload, error)

func (*PuppetWin) RoomTopic

func (p *PuppetWin) RoomTopic(roomID string) (string, error)

func (*PuppetWin) SetContactAlias

func (p *PuppetWin) SetContactAlias(contactID string, alias string) error

func (*PuppetWin) SetContactAvatar

func (p *PuppetWin) SetContactAvatar(contactID string, fileBox *filebox.FileBox) error

func (*PuppetWin) SetContactSelfName

func (p *PuppetWin) SetContactSelfName(name string) error

func (*PuppetWin) SetContactSelfSignature

func (p *PuppetWin) SetContactSelfSignature(signature string) error

func (*PuppetWin) SetRoomAnnounce

func (p *PuppetWin) SetRoomAnnounce(roomID, text string) error

func (*PuppetWin) SetRoomTopic

func (p *PuppetWin) SetRoomTopic(roomID string, topic string) error

func (*PuppetWin) Start

func (p *PuppetWin) Start() (err error)

func (*PuppetWin) Stop

func (p *PuppetWin) Stop()

func (*PuppetWin) TagContactAdd

func (p *PuppetWin) TagContactAdd(id, contactID string) (err error)

func (*PuppetWin) TagContactDelete

func (p *PuppetWin) TagContactDelete(id string) (err error)

func (*PuppetWin) TagContactList

func (p *PuppetWin) TagContactList(contactID string) ([]string, error)

func (*PuppetWin) TagContactRemove

func (p *PuppetWin) TagContactRemove(id, contactID string) (err error)

Directories

Path Synopsis
msg

Jump to

Keyboard shortcuts

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