A wechat group is a public place to gather like-minded friends, and the wechat group of quantitative trading circles is no exception. Recently, many users who have been quantified by inventors always ask how to push messages to the wechat group at will. For example, their strategies send transaction signals, but they do not want to automate transactions. They hope to send the signals provided by robots to the wechat group as a reference.
For this reason, I looked at the wechat interface and found that the public number is needed, which is not particularly convenient. So I decided to take a different tack and tried the following solution. Due to the author’s limited level, only the most basic function of this demand can be completed, you see only for reference.
Implementation scheme
The decision to write in Golang requires the use of a Golang library github.com/go-vgo/robotgo, which basically implements the requirements of the solution.
First we run a service program on our computer, the code is as follows:
package main
import (
"fmt"
"time"
"github.com/go-vgo/robotgo"
"net/http"
"io/ioutil"
)
func postMsg (msg string) {
fmt.Println("Start the mission!)
// process ids
processIds := "WeChat"
fpid, err3 := robotgo.FindIds(processIds)
robotgo.ActivePID(fpid[0])
time.Sleep(time.Millisecond * 2000)
if err3 == nil {
fmt.Println(fmt.Sprintf("find %s", processIds), "ids:", fpid)
/* Use the image recognition mode, ArrPicFileName := []string{"pic1.png", "pic2.png", "pic3.png"} for _, arrPicFileName := []string{"pic1.png", "pic2.png", "pic3.png"} for _, name := range arrPicFileName { picPath := fmt.Sprintf("/xxx/xxx/Desktop/xxx/%s", name) fmt.Println("picPath:", fmt.Sprintf("/xxx/xxx/Desktop/xxx/%s", name)) fx, fy := robotgo.FindPic(picPath) fmt.Println("move to :", fx+10, fy+10) robotgo.MoveMouseSmooth(fx+10, fy+10) time.Sleep(time.Millisecond * 2000) robotgo.MouseClick("left", false) robotgo.TypeStr(msg) time.Sleep(time.Second) robotgo.KeyTap("enter") time.Sleep(time.Second) } */
// * fixed the region coordinates, the upper right of the screen is 0,0
arrArea := []map[string]int{
map[string]int{
"x" : 190."y" : 200,},map[string]int{
"x" : 190."y" : 200+70,},map[string]int{
"x" : 190."y" : 200+70+70,}}for _, area := range arrArea {
robotgo.MoveMouseSmooth(area["x"], area["y"])
time.Sleep(time.Millisecond * 2000)
robotgo.MouseClick("left".false)
robotgo.TypeStr(msg)
time.Sleep(time.Second)
robotgo.KeyTap("enter")
time.Sleep(time.Second)
}
/ / * /
}
fmt.Println("Mission accomplished!")}func Handle (w http.ResponseWriter, r *http.Request) {
b, err := ioutil.ReadAll(r.Body)
iferr ! =nil {
panic(err)
}
fmt.Println("req body:".string(b))
postMsg(string(b))
w.Write([]byte("finished!"))}func main (a) {
fmt.Println("Listen" http://127.0.0.1:9090)
http.HandleFunc("/data", Handle)
http.ListenAndServe("127.0.0.1:9090".nil)}Copy the code
The function of this service program is to wait for the request, and after receiving the request, call the postMsg function to perform a series of simulated mouse movement click and input operations, so as to open the wechat software window, click the pre-defined area, input the information in the request and send it to the wechat group.
Wechat window click region confirm, tested two schemes. First, save the name and picture of wechat group, load the picture comparison screen when triggering postMsg, find the coordinates and click. This method recognition rate is not very high, sometimes can not identify (may be I too vegetables, useless to THE T_T, there are big god please advise). Therefore, the second scheme, which is more reliable, is to fix the click area and plan a set of coordinates of the click area, that is: the coordinates recorded by the arrArea variable in the above code, you ask me where the coordinate value comes from? Answer: look at the screen capture pixel coordinates, measured out of T_T.
Inventors test strategies on quantitative trading platforms:
function main() {
var msg = {
"type" : "msg"."robotId" : _G(),
"msg" : "hello fmz!"
}
var n = 0
while(true) {
if(n == 20) {
var ret = HttpQuery("http://127.0.0.1:9090/data".JSON.stringify(msg))
Log("Exit")
break
}
n++
LogStatus(_D(), "n:", n)
Sleep(1000)}}Copy the code
The strategy simulation signal trading (n = = 20, assuming that the trigger trading signals, think can trade), to the local service, address: http://127.0.0.1:9090/data send request.
test
The inventor quantified the bots running on the trading platform (the custodians also run locally) :
Local service running:
WeChat window
The pushed message is:
{"type":"msg","robotId":130350,"msg":"hello fmz!" }Copy the code
This plan only serves as a primer, welcome to discuss if there is a better plan.