|
| 1 | +/** |
| 2 | + * Wechaty - https://github.com/wechaty/wechaty |
| 3 | + * |
| 4 | + * @copyright 2020-now Wechaty |
| 5 | + * |
| 6 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 | + * you may not use this file except in compliance with the License. |
| 8 | + * You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, software |
| 13 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | + * See the License for the specific language governing permissions and |
| 16 | + * limitations under the License. |
| 17 | + * |
| 18 | + */ |
| 19 | +package main |
| 20 | + |
| 21 | +import ( |
| 22 | + "fmt" |
| 23 | + "log" |
| 24 | + "time" |
| 25 | + |
| 26 | + "github.com/wechaty/go-wechaty/wechaty" |
| 27 | + "github.com/wechaty/go-wechaty/wechaty-puppet/schemas" |
| 28 | + "github.com/wechaty/go-wechaty/wechaty/user" |
| 29 | +) |
| 30 | + |
| 31 | +var err error |
| 32 | + |
| 33 | +func main() { |
| 34 | + var bot = wechaty.NewWechaty() |
| 35 | + |
| 36 | + bot.OnScan(func(context *wechaty.Context, qrCode string, status schemas.ScanStatus, data string) { |
| 37 | + fmt.Printf("Scan QR Code to login: %v\nhttps://wechaty.js.org/qrcode/%s\n", status, qrCode) |
| 38 | + }).OnLogin(func(context *wechaty.Context, user *user.ContactSelf) { |
| 39 | + fmt.Printf("User %s logined\n", user.Name()) |
| 40 | + }).OnLogout(func(ctx *wechaty.Context, user *user.ContactSelf, reason string) { |
| 41 | + fmt.Printf("User %s logouted: %s\n", user, reason) |
| 42 | + }) |
| 43 | + |
| 44 | + bot.OnMessage(onMessage) |
| 45 | + // The First Plug-in |
| 46 | + bot.Use(PlugOne()) |
| 47 | + // The Second Plug-in |
| 48 | + bot.Use(PlugTwo("Hello, PlugTwo")) |
| 49 | + |
| 50 | + bot.DaemonStart() |
| 51 | +} |
| 52 | + |
| 53 | +// The first onMessage processing logic |
| 54 | +func onMessage(context *wechaty.Context, message *user.Message) { |
| 55 | + log.Println(message) |
| 56 | + |
| 57 | + if message.Self() { |
| 58 | + log.Println("Message discarded because its outgoing") |
| 59 | + } |
| 60 | + |
| 61 | + if message.Age() > 2*60*time.Second { |
| 62 | + log.Println("Message discarded because its TOO OLD(than 2 minutes)") |
| 63 | + } |
| 64 | + |
| 65 | + if message.Type() != schemas.MessageTypeText || message.Text() != "ding" { |
| 66 | + log.Println("Message discarded because it does not match 'ding'") |
| 67 | + |
| 68 | + // Set needReply Status (`return`) |
| 69 | + context.SetData("needReply", false) |
| 70 | + return |
| 71 | + } |
| 72 | + |
| 73 | + // 1. reply 'dong' |
| 74 | + if _, err = message.Say("dong"); err != nil { |
| 75 | + log.Println(err) |
| 76 | + return |
| 77 | + } |
| 78 | + log.Println("REPLY: dong") |
| 79 | + |
| 80 | + // Set needReply Status |
| 81 | + context.SetData("needReply", true) |
| 82 | + |
| 83 | + // 2. reply image(qrcode image) |
| 84 | + //fileBox, _ := file_box.FromUrl("https://wechaty.github.io/wechaty/images/bot-qr-code.png", "", nil) |
| 85 | + //_, err = message.Say(fileBox) |
| 86 | + //if err != nil { |
| 87 | + // log.Println(err) |
| 88 | + // return |
| 89 | + //} |
| 90 | + //log.Printf("REPLY: %s\n", fileBox) |
| 91 | +} |
| 92 | + |
| 93 | +// PlugOne (The First Plug-in) |
| 94 | +func PlugOne() *wechaty.Plugin { |
| 95 | + newPlug := wechaty.NewPlugin() |
| 96 | + |
| 97 | + // The Second Onmessage Processing Logic |
| 98 | + newPlug.OnMessage(func(context *wechaty.Context, message *user.Message) { |
| 99 | + |
| 100 | + // Get Value From Wechaty Context |
| 101 | + needReply, ok := context.GetData("needReply").(bool) |
| 102 | + |
| 103 | + // Determine If Parsing Error (`!`) |
| 104 | + if !ok { |
| 105 | + log.Println("context GetData needReply Error") |
| 106 | + return |
| 107 | + } |
| 108 | + |
| 109 | + // Determine If You Have Responded (`!`) |
| 110 | + if !needReply { |
| 111 | + return |
| 112 | + } |
| 113 | + |
| 114 | + if _, err = message.Say("PlugOne OnMessage Here!"); err != nil { |
| 115 | + log.Println(err) |
| 116 | + return |
| 117 | + } |
| 118 | + log.Println("PlugOne OnMessage Here!") |
| 119 | + }) |
| 120 | + return newPlug |
| 121 | +} |
| 122 | + |
| 123 | +// PlugTwo (The Second Plug-in) |
| 124 | +func PlugTwo(args string) *wechaty.Plugin { |
| 125 | + newPlug := wechaty.NewPlugin() |
| 126 | + |
| 127 | + // The Third Onmessage Processing Logic |
| 128 | + newPlug.OnMessage(func(context *wechaty.Context, message *user.Message) { |
| 129 | + |
| 130 | + // Get Value From Wechaty Context |
| 131 | + needReply, ok := context.GetData("needReply").(bool) |
| 132 | + |
| 133 | + // Determine If Parsing Error (`!`) |
| 134 | + if !ok { |
| 135 | + log.Println("context GetData needReply Error") |
| 136 | + return |
| 137 | + } |
| 138 | + |
| 139 | + log.Println(args) |
| 140 | + |
| 141 | + // Determine If You Have Responded (`!`) |
| 142 | + if !needReply { |
| 143 | + return |
| 144 | + } |
| 145 | + |
| 146 | + if _, err = message.Say("PlugTwo OnMessage Here!"); err != nil { |
| 147 | + log.Println(err) |
| 148 | + return |
| 149 | + } |
| 150 | + log.Println("PlugTwo OnMessage Here!") |
| 151 | + }) |
| 152 | + return newPlug |
| 153 | +} |
0 commit comments