Giter Site home page Giter Site logo

Comments (10)

songtianyi avatar songtianyi commented on July 22, 2024
  1. 找到UserName
friend := session.Cm.GetContactByPYQuanPin("SONGTIANYI")
  1. 发送消息
session.SendText("hello", session.Bot.UserName, friend.UserName)

插件里的代码并不是说只有在收消息的时候才能用到,和msg无关的代码都可以拿来在其他地方用

from wechat-go.

yusank avatar yusank commented on July 22, 2024

嗯嗯,知道了,但是登录的时候 session.LoginAndServe() 函数内部的 server() 会一直等待消息的传进来,也就是说,我在 main 函数里调用session.LoginAndServe()之后就没办法往后执行

type WeChat struct {
	session 	*wxweb.Session
}

func (wx *WeChat)HandleDebug() {
	var err error

	wx.session, err = wxweb.CreateSession(nil, nil, wxweb.TERMINAL_MODE)
	if err != nil {
		logs.Error(err)
		return
	}



	monitor.Register(wx.session)

	wx.session.HandlerRegister.EnableByName("monitor")

	if err := wx.session.LoginAndServe(false); err != nil {
		logs.Error("session exit, %s", err)
	}
	wx.session.SendText("hello", wx.session.Bot.UserName, "filehelper")
}

在这儿,SendText() 函数就一直没执行,

希望作者能解惑,O(∩_∩)O谢谢

from wechat-go.

songtianyi avatar songtianyi commented on July 22, 2024

@yusank
把LoginAndServe用go包起来就行了, 但是有一个问题,在这里你是不知道是否已经登录成功。需要做些改进,写好了告诉你。

from wechat-go.

songtianyi avatar songtianyi commented on July 22, 2024

@yusank

type WeChat struct {
	session 	*wxweb.Session
}

func (wx *WeChat)HandleDebug() {
	var err error

	wx.session, err = wxweb.CreateSession(nil, nil, wxweb.TERMINAL_MODE)
	if err != nil {
		logs.Error(err)
		return
	}



	monitor.Register(wx.session)

	wx.session.HandlerRegister.EnableByName("monitor")

	errChan := make(chan error)
	go func(errChan chan error) {
       	if err := wx.session.LoginAndServe(false); err != nil {
			logs.Error("session exit, %s", err)
			errChan <- err
		}
	}(errChan)
	tick := time.Tick(2 * time.Second)
	for {
		select {
			case err := <-errChan:
				panic(err)
			case <-tick:
				// check login status
				if wx.session.Cookies != nil && wx.session.Cm != nil {
					break
				}
		}

	}
	// now it is ok to send message
	wx.session.SendText("hello", wx.session.Bot.UserName, "filehelper")
	<- errChan
}

没有测过,你看懂意思就行

from wechat-go.

songtianyi avatar songtianyi commented on July 22, 2024

@yusank
这个方式并不优雅,后续我会加个watch channel,目前可以这么做。

from wechat-go.

yusank avatar yusank commented on July 22, 2024

@songtianyi 嗯,谢谢!看懂了,麻烦你了.
我想到另一种方法就是通过插件的思路,用户给发发特定消息(类似秘钥吧),然后我就记住他,然后就给他发我要发的消息,也是用到了 channel
wechat-monitor

from wechat-go.

songtianyi avatar songtianyi commented on July 22, 2024

@yusank
这样方式需要微信端来触发,那这样就和插件模式无异了。
无状态推送是可以做的,我的代码对这方面支持的不好,后边会改进,case暂时开放,改进好了再关闭。

from wechat-go.

yusank avatar yusank commented on July 22, 2024

@songtianyi
确实是,需要触发一次微信端。不过目前我的需求应该能满足,毕竟只要不登出的话,可以随时给特定的用户发消息。但是做到无状态发送就最好了。
已经关注你的项目了,坐等更新~😊

from wechat-go.

miniframework avatar miniframework commented on July 22, 2024

@yusank @songtianyi 什么叫无状态发送,不需要登录么?

from wechat-go.

yusank avatar yusank commented on July 22, 2024

@yusank @songtianyi 什么叫无状态发送,不需要登录么?

这里的无状态是说无需目标用户触发,做到随时给任何好友发消息,目前的逻辑是需要用户触发从而得到用户的动态id,才能进行发消息

from wechat-go.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.