├── .gitignore ├── LICENSE ├── README.md ├── _webhookExample └── main.py ├── assisant.go ├── bridge ├── arg │ └── arg.go ├── bridge.go ├── proxy.go ├── result │ └── result.go └── uuid.go ├── gurad.go ├── main.go ├── service ├── restfull.go └── uuid.go ├── tuling.go ├── uuidprocessor └── processor.go └── xiaoice.go /.gitignore: -------------------------------------------------------------------------------- 1 | # Compiled Object files, Static and Dynamic libs (Shared Objects) 2 | *.o 3 | *.a 4 | *.so 5 | 6 | # Folders 7 | _obj 8 | _test 9 | 10 | # Architecture specific extensions/prefixes 11 | *.[568vq] 12 | [568vq].out 13 | 14 | *.cgo1.go 15 | *.cgo2.c 16 | _cgo_defun.c 17 | _cgo_gotypes.go 18 | _cgo_export.* 19 | 20 | _testmain.go 21 | 22 | *.test 23 | *.prof 24 | conf.yaml 25 | ggbot-mac 26 | _examples/icey 27 | sendmsg 28 | .idea 29 | contact 30 | ggbot 31 | qrcode.png 32 | connector 33 | release 34 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | http://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, 10 | and distribution as defined by Sections 1 through 9 of this document. 11 | 12 | "Licensor" shall mean the copyright owner or entity authorized by 13 | the copyright owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all 16 | other entities that control, are controlled by, or are under common 17 | control with that entity. For the purposes of this definition, 18 | "control" means (i) the power, direct or indirect, to cause the 19 | direction or management of such entity, whether by contract or 20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 21 | outstanding shares, or (iii) beneficial ownership of such entity. 22 | 23 | "You" (or "Your") shall mean an individual or Legal Entity 24 | exercising permissions granted by this License. 25 | 26 | "Source" form shall mean the preferred form for making modifications, 27 | including but not limited to software source code, documentation 28 | source, and configuration files. 29 | 30 | "Object" form shall mean any form resulting from mechanical 31 | transformation or translation of a Source form, including but 32 | not limited to compiled object code, generated documentation, 33 | and conversions to other media types. 34 | 35 | "Work" shall mean the work of authorship, whether in Source or 36 | Object form, made available under the License, as indicated by a 37 | copyright notice that is included in or attached to the work 38 | (an example is provided in the Appendix below). 39 | 40 | "Derivative Works" shall mean any work, whether in Source or Object 41 | form, that is based on (or derived from) the Work and for which the 42 | editorial revisions, annotations, elaborations, or other modifications 43 | represent, as a whole, an original work of authorship. For the purposes 44 | of this License, Derivative Works shall not include works that remain 45 | separable from, or merely link (or bind by name) to the interfaces of, 46 | the Work and Derivative Works thereof. 47 | 48 | "Contribution" shall mean any work of authorship, including 49 | the original version of the Work and any modifications or additions 50 | to that Work or Derivative Works thereof, that is intentionally 51 | submitted to Licensor for inclusion in the Work by the copyright owner 52 | or by an individual or Legal Entity authorized to submit on behalf of 53 | the copyright owner. For the purposes of this definition, "submitted" 54 | means any form of electronic, verbal, or written communication sent 55 | to the Licensor or its representatives, including but not limited to 56 | communication on electronic mailing lists, source code control systems, 57 | and issue tracking systems that are managed by, or on behalf of, the 58 | Licensor for the purpose of discussing and improving the Work, but 59 | excluding communication that is conspicuously marked or otherwise 60 | designated in writing by the copyright owner as "Not a Contribution." 61 | 62 | "Contributor" shall mean Licensor and any individual or Legal Entity 63 | on behalf of whom a Contribution has been received by Licensor and 64 | subsequently incorporated within the Work. 65 | 66 | 2. Grant of Copyright License. Subject to the terms and conditions of 67 | this License, each Contributor hereby grants to You a perpetual, 68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 69 | copyright license to reproduce, prepare Derivative Works of, 70 | publicly display, publicly perform, sublicense, and distribute the 71 | Work and such Derivative Works in Source or Object form. 72 | 73 | 3. Grant of Patent License. Subject to the terms and conditions of 74 | this License, each Contributor hereby grants to You a perpetual, 75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 76 | (except as stated in this section) patent license to make, have made, 77 | use, offer to sell, sell, import, and otherwise transfer the Work, 78 | where such license applies only to those patent claims licensable 79 | by such Contributor that are necessarily infringed by their 80 | Contribution(s) alone or by combination of their Contribution(s) 81 | with the Work to which such Contribution(s) was submitted. If You 82 | institute patent litigation against any entity (including a 83 | cross-claim or counterclaim in a lawsuit) alleging that the Work 84 | or a Contribution incorporated within the Work constitutes direct 85 | or contributory patent infringement, then any patent licenses 86 | granted to You under this License for that Work shall terminate 87 | as of the date such litigation is filed. 88 | 89 | 4. Redistribution. You may reproduce and distribute copies of the 90 | Work or Derivative Works thereof in any medium, with or without 91 | modifications, and in Source or Object form, provided that You 92 | meet the following conditions: 93 | 94 | (a) You must give any other recipients of the Work or 95 | Derivative Works a copy of this License; and 96 | 97 | (b) You must cause any modified files to carry prominent notices 98 | stating that You changed the files; and 99 | 100 | (c) You must retain, in the Source form of any Derivative Works 101 | that You distribute, all copyright, patent, trademark, and 102 | attribution notices from the Source form of the Work, 103 | excluding those notices that do not pertain to any part of 104 | the Derivative Works; and 105 | 106 | (d) If the Work includes a "NOTICE" text file as part of its 107 | distribution, then any Derivative Works that You distribute must 108 | include a readable copy of the attribution notices contained 109 | within such NOTICE file, excluding those notices that do not 110 | pertain to any part of the Derivative Works, in at least one 111 | of the following places: within a NOTICE text file distributed 112 | as part of the Derivative Works; within the Source form or 113 | documentation, if provided along with the Derivative Works; or, 114 | within a display generated by the Derivative Works, if and 115 | wherever such third-party notices normally appear. The contents 116 | of the NOTICE file are for informational purposes only and 117 | do not modify the License. You may add Your own attribution 118 | notices within Derivative Works that You distribute, alongside 119 | or as an addendum to the NOTICE text from the Work, provided 120 | that such additional attribution notices cannot be construed 121 | as modifying the License. 122 | 123 | You may add Your own copyright statement to Your modifications and 124 | may provide additional or different license terms and conditions 125 | for use, reproduction, or distribution of Your modifications, or 126 | for any such Derivative Works as a whole, provided Your use, 127 | reproduction, and distribution of the Work otherwise complies with 128 | the conditions stated in this License. 129 | 130 | 5. Submission of Contributions. Unless You explicitly state otherwise, 131 | any Contribution intentionally submitted for inclusion in the Work 132 | by You to the Licensor shall be under the terms and conditions of 133 | this License, without any additional terms or conditions. 134 | Notwithstanding the above, nothing herein shall supersede or modify 135 | the terms of any separate license agreement you may have executed 136 | with Licensor regarding such Contributions. 137 | 138 | 6. Trademarks. This License does not grant permission to use the trade 139 | names, trademarks, service marks, or product names of the Licensor, 140 | except as required for reasonable and customary use in describing the 141 | origin of the Work and reproducing the content of the NOTICE file. 142 | 143 | 7. Disclaimer of Warranty. Unless required by applicable law or 144 | agreed to in writing, Licensor provides the Work (and each 145 | Contributor provides its Contributions) on an "AS IS" BASIS, 146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 147 | implied, including, without limitation, any warranties or conditions 148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 149 | PARTICULAR PURPOSE. You are solely responsible for determining the 150 | appropriateness of using or redistributing the Work and assume any 151 | risks associated with Your exercise of permissions under this License. 152 | 153 | 8. Limitation of Liability. In no event and under no legal theory, 154 | whether in tort (including negligence), contract, or otherwise, 155 | unless required by applicable law (such as deliberate and grossly 156 | negligent acts) or agreed to in writing, shall any Contributor be 157 | liable to You for damages, including any direct, indirect, special, 158 | incidental, or consequential damages of any character arising as a 159 | result of this License or out of the use or inability to use the 160 | Work (including but not limited to damages for loss of goodwill, 161 | work stoppage, computer failure or malfunction, or any and all 162 | other commercial damages or losses), even if such Contributor 163 | has been advised of the possibility of such damages. 164 | 165 | 9. Accepting Warranty or Additional Liability. While redistributing 166 | the Work or Derivative Works thereof, You may choose to offer, 167 | and charge a fee for, acceptance of support, warranty, indemnity, 168 | or other liability obligations and/or rights consistent with this 169 | License. However, in accepting such obligations, You may act only 170 | on Your own behalf and on Your sole responsibility, not on behalf 171 | of any other Contributor, and only if You agree to indemnify, 172 | defend, and hold each Contributor harmless for any liability 173 | incurred by, or claims asserted against, such Contributor by reason 174 | of your accepting any such warranty or additional liability. 175 | 176 | END OF TERMS AND CONDITIONS 177 | 178 | APPENDIX: How to apply the Apache License to your work. 179 | 180 | To apply the Apache License to your work, attach the following 181 | boilerplate notice, with the fields enclosed by brackets "{}" 182 | replaced with your own identifying information. (Don't include 183 | the brackets!) The text should be enclosed in the appropriate 184 | comment syntax for the file format. We also recommend that a 185 | file or class name and description of purpose be included on the 186 | same "printed page" as the copyright notice for easier 187 | identification within third-party archives. 188 | 189 | Copyright {yyyy} {name of copyright owner} 190 | 191 | Licensed under the Apache License, Version 2.0 (the "License"); 192 | you may not use this file except in compliance with the License. 193 | You may obtain a copy of the License at 194 | 195 | http://www.apache.org/licenses/LICENSE-2.0 196 | 197 | Unless required by applicable law or agreed to in writing, software 198 | distributed under the License is distributed on an "AS IS" BASIS, 199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 200 | See the License for the specific language governing permissions and 201 | limitations under the License. 202 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # GGBot 2 | 3 | 一个牛逼的微信机器人,非常适合非技术人员自由定制 4 | ### 先看个视频 5 | [GGBot 演示](http://7xnowv.com1.z0.glb.clouddn.com/ggbot-demo.mp4) 6 | 7 | ### 下载地址 8 | 9 | [window 版本](http://oap91rhcb.bkt.clouddn.com/GGBot-windows.exe) 10 | 11 | > mac os x 和 linux 建议自己编译 12 | 13 | ### 自定义 14 | 15 | 直接运行下载到的可执行文件即可,弹出二维码以后用手机扫描二维码即可登陆。 机器人自动加载:`微软小冰` `加群欢迎语` `群签到` `添加好友自动通过` 。更多的功能正在开发中,欢迎在`issue`中提出你想要的功能。 16 | 17 | > 默认会加载`微软小冰`, 所以请大家先用`微信app`关注微软小冰公众号 18 | 19 | ##### 如何使用图灵机器人 20 | 机器人在运行过一次以后会在可执行文件的统一目录生成 `conf.yaml`, 打开这个文件 21 | ``` yaml 22 | showqrcodeonterminal: false #是否在命令行中显示二维码 23 | fuzzydiff: true #联系人对比是否启用模糊匹配 24 | uniquegroupmember: true #是否为群成员生成ggid 25 | features: 26 | assistant: 27 | enable: false 28 | ownerggid: "" 29 | guard: 30 | enable: false 31 | tuling: 32 | enable: false 33 | key: "" 34 | xiaoice: 35 | enable: false 36 | webhookservice: 37 | enable: false 38 | msgwebhook: "" 39 | loginstatewebhook: "" 40 | uuidwebhook: "" 41 | ``` 42 | 修改下面2行 43 | ``` yaml 44 | tuling: 45 | apikey: "" #这里填写你申请到的图灵机器人key 46 | enable: true # 将这里改为true 47 | ``` 48 | 49 | > apikey 可以在图灵机器人的官网免费申请 [点击这里立即申请](http://www.tuling123.com) 50 | 51 | ### 二次开发 52 | 本机器人基于[wechat](https://github.com/KevinGong2013/wechat)开发 53 | ###### 安装`go-lang`开发环境 54 | [传送门](https://www.golang.org) 55 | 56 | ###### 安装`wechat`包 57 | ``` go 58 | go get github.com/KevinGong2013/wechat 59 | ``` 60 | ###### clone 源码 61 | ``` go 62 | git clone https://github.com/KevinGong2013/ggbot.git 63 | ``` 64 | ###### 编译运行 65 | ``` bash 66 | cd ggbot 67 | go build 68 | ./ggbot 69 | ``` 70 | 71 | ### 交流讨论 72 | 73 | 1.github issue (推荐) 74 | 2.qq 群:609776708 75 | 76 | ### 常见问题 77 | 78 | ##### 0x00 79 | Q: windows 系统编译运行,cmd显示不正常该怎么办? 80 | A: [Enable ANSI colors in Windows command prompt](https://web.liferay.com/web/igor.spasic/blog/-/blogs/enable-ansi-colors-in-windows-command-prompt) 81 | 82 | ## License 83 | 84 | The code in this repository is licensed under the Apache License, Version 2.0 (the "License"); 85 | you may not use this file except in compliance with the License. 86 | You may obtain a copy of the License at 87 | 88 | http://www.apache.org/licenses/LICENSE-2.0 89 | 90 | Unless required by applicable law or agreed to in writing, software 91 | distributed under the License is distributed on an "AS IS" BASIS, 92 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 93 | See the License for the specific language governing permissions and 94 | limitations under the License. 95 | 96 | **NOTE**: This software depends on other packages that may be licensed under different open source licenses. 97 | 98 | Copyright 2017 - 2027 Kevin.Gong aoxianglele#icloud.com 99 | -------------------------------------------------------------------------------- /_webhookExample/main.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/python 2 | # encoding=utf-8 3 | 4 | # 先执行 python main.py 然后运行ggbot 会收到login msg contact 的消息推送 5 | from BaseHTTPServer import BaseHTTPRequestHandler,HTTPServer 6 | 7 | class WebhookHandler(BaseHTTPRequestHandler): 8 | def do_POST(self): 9 | 10 | content_length = int(self.headers['Content-Length']) # <--- Gets the size of data 11 | post_data = self.rfile.read(content_length) # <--- Gets the data itself 12 | 13 | print self.path 14 | print post_data 15 | 16 | #Create a web server and define the handler to manage the 17 | #incoming request 18 | server = HTTPServer(('', 3288), WebhookHandler) 19 | 20 | print 'Started httpserver on port 3288' 21 | 22 | #Wait forever for incoming http requests 23 | server.serve_forever() 24 | -------------------------------------------------------------------------------- /assisant.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "bytes" 5 | "encoding/json" 6 | "fmt" 7 | "strings" 8 | "time" 9 | 10 | "github.com/KevinGong2013/wechat" 11 | ) 12 | 13 | type assistant struct { 14 | bot *wechat.WeChat 15 | username string 16 | } 17 | 18 | func newAssistant(bot *wechat.WeChat, username string) *assistant { 19 | return &assistant{bot, username} 20 | } 21 | 22 | func (a *assistant) delMember(groupUserName, memberUserName string) error { 23 | ps := map[string]interface{}{ 24 | `DelMemberList`: memberUserName, 25 | `ChatRoomName`: groupUserName, 26 | `BaseRequest`: a.bot.BaseRequest, 27 | } 28 | data, _ := json.Marshal(ps) 29 | 30 | url := fmt.Sprintf(`%s/webwxupdatechatroom?fun=delmember`, a.bot.BaseURL) 31 | 32 | var resp wechat.Response 33 | 34 | err := a.bot.Execute(url, bytes.NewReader(data), &resp) 35 | 36 | if err != nil { 37 | return err 38 | } 39 | 40 | if resp.IsSuccess() { 41 | return nil 42 | } 43 | 44 | return fmt.Errorf(`delete %s on %s failed`, memberUserName, groupUserName) 45 | } 46 | 47 | func (a *assistant) handle(msg wechat.EventMsgData) { 48 | if msg.IsGroupMsg { 49 | if msg.MsgType == 10000 && strings.Contains(msg.Content, `加入群聊`) { 50 | nn, err := search(msg.Content, `"`, `"通过`) 51 | if err != nil { 52 | logger.Errorf(`send group welcome failed %s`, msg.Content) 53 | } 54 | a.bot.SendTextMsg(`欢迎【`+nn+`】加入群聊`, msg.FromUserName) 55 | } else if strings.Contains(msg.Content, `签到`) { 56 | c := a.bot.ContactByUserName(msg.SenderUserName) 57 | a.bot.SendTextMsg(fmt.Sprintf(`%s 完成了签到`, c.NickName), msg.FromUserName) 58 | } 59 | 60 | // 群主踢人 61 | if msg.SenderUserName == a.username && strings.HasPrefix(msg.Content, `滚蛋`) { 62 | gun := msg.FromUserName 63 | if msg.IsSentByMySelf { 64 | gun = msg.ToUserName 65 | } 66 | nn := strings.Replace(msg.Content, `滚蛋`, ``, 1) 67 | if members, err := a.bot.MembersOfGroup(gun); err == nil { 68 | for _, c := range members { 69 | logger.Debug(c.NickName) 70 | if c.NickName == nn { 71 | a.bot.SendTextMsg(nn+` 送你免费飞机票`, gun) 72 | time.Sleep(3 * time.Second) 73 | err := a.delMember(gun, c.UserName) 74 | if err != nil { 75 | a.bot.SendTextMsg(`暂时不T你把`, gun) 76 | } 77 | } 78 | } 79 | } 80 | } 81 | } 82 | } 83 | 84 | func search(source, prefix, suffix string) (string, error) { 85 | 86 | index := strings.Index(source, prefix) 87 | if index == -1 { 88 | err := fmt.Errorf("can't find [%s] in [%s]", prefix, source) 89 | return ``, err 90 | } 91 | index += len(prefix) 92 | 93 | end := strings.Index(source[index:], suffix) 94 | if end == -1 { 95 | err := fmt.Errorf("can't find [%s] in [%s]", suffix, source) 96 | return ``, err 97 | } 98 | 99 | result := source[index : index+end] 100 | 101 | return result, nil 102 | } 103 | -------------------------------------------------------------------------------- /bridge/arg/arg.go: -------------------------------------------------------------------------------- 1 | package arg 2 | 3 | const ( 4 | // Token ... 5 | Token = 0 6 | 7 | // Login ... 8 | Login = 1 9 | 10 | // RedPacket .. 11 | RedPacket = 2 12 | ) 13 | 14 | //Arg send to client arg struct 15 | type Arg struct { 16 | Seq uint64 17 | CMD int 18 | Value map[string]interface{} 19 | } 20 | 21 | // NewArg ... 22 | func NewArg(cmd int) *Arg { 23 | return &Arg{ 24 | CMD: cmd, 25 | Value: make(map[string]interface{}), 26 | } 27 | } 28 | 29 | // Append .. 30 | func (arg *Arg) Append(k string, v interface{}) *Arg { 31 | arg.Value[k] = v 32 | return arg 33 | } 34 | -------------------------------------------------------------------------------- /bridge/bridge.go: -------------------------------------------------------------------------------- 1 | package bridge 2 | 3 | import ( 4 | "encoding/json" 5 | "fmt" 6 | "net/http" 7 | "strconv" 8 | "sync" 9 | "time" 10 | 11 | "github.com/KevinGong2013/ggbot/bridge/arg" 12 | r "github.com/KevinGong2013/ggbot/bridge/result" 13 | log "github.com/Sirupsen/logrus" 14 | ) 15 | 16 | // Connector is design to connect iOS wechat app 17 | type Connector interface { 18 | RefreshToken(token string) 19 | Send(a *arg.Arg) error 20 | } 21 | 22 | var logger = log.WithFields(log.Fields{ 23 | "module": "bridge", 24 | }) 25 | 26 | // Wrapper ... 27 | type Wrapper struct { 28 | connector Connector 29 | 30 | mutex sync.Mutex 31 | seq uint64 32 | pending map[uint64]chan *r.Result 33 | } 34 | 35 | // NewWrapper ... 36 | func NewWrapper(c Connector) *Wrapper { 37 | 38 | w := &Wrapper{ 39 | connector: c, 40 | pending: make(map[uint64]chan *r.Result), 41 | } 42 | 43 | // 44 | http.HandleFunc("/bridge", w.handle) 45 | 46 | go http.ListenAndServe(`:3280`, nil) 47 | 48 | return w 49 | } 50 | 51 | // Go ... 52 | func (w *Wrapper) Go(a *arg.Arg, done chan *r.Result) { 53 | 54 | w.mutex.Lock() 55 | 56 | seq := w.seq 57 | w.seq++ 58 | w.pending[seq] = done 59 | 60 | a.Seq = seq // 很重要 61 | 62 | w.mutex.Unlock() 63 | 64 | err := w.connector.Send(a) 65 | 66 | if err != nil { 67 | done <- r.NewResultWithError(err.Error()) 68 | w.mutex.Lock() 69 | delete(w.pending, seq) 70 | w.mutex.Unlock() 71 | } 72 | } 73 | 74 | // Call .. 75 | func (w *Wrapper) Call(a *arg.Arg) *r.Result { 76 | done := make(chan *r.Result, 1) 77 | w.Go(a, done) 78 | 79 | timer := time.NewTimer(time.Minute * 1).C 80 | 81 | select { 82 | case r := <-done: 83 | if r.IsFailure() { 84 | logger.Errorf(`send %v to wechat app failed error: %v`, a, r.Err) 85 | } else { 86 | logger.Infof(`send %v to wechat app successed`, a) 87 | } 88 | return r 89 | case <-timer: 90 | logger.Errorf(`send %v to wechat app time out`, a) 91 | return r.NewResultWithError(fmt.Sprintf(`time out %v`, a)) 92 | } 93 | } 94 | 95 | func (w *Wrapper) handle(writer http.ResponseWriter, req *http.Request) { 96 | defer req.Body.Close() 97 | 98 | logger.Debugf(`---cmd:%v------ did receive wechat app response`, req.Header[`Cmd`]) 99 | 100 | seqs := req.Header[`Seq`] 101 | cmds := req.Header[`Cmd`] 102 | 103 | if len(seqs) == 0 || len(cmds) == 0 { 104 | logger.Errorf(`invalidate req %v`, req) 105 | return 106 | } 107 | 108 | seq, _ := strconv.ParseUint(seqs[0], 10, 32) 109 | cmd, _ := strconv.Atoi(cmds[0]) 110 | 111 | var result map[string]interface{} 112 | 113 | decoder := json.NewDecoder(req.Body) 114 | err := decoder.Decode(&result) 115 | if err != nil { 116 | logger.Errorf(`decode error: %v`, err) 117 | return 118 | } 119 | 120 | logger.Debugf(`---seq:[%v]------ body: %v`, seq, result) 121 | 122 | if seq == 0 && cmd == arg.Token { 123 | t, _ := result[`token`].(string) 124 | w.connector.RefreshToken(t) 125 | } else if seq == 0 && cmd == arg.RedPacket { 126 | status, _ := result[`status`].(string) 127 | if status == `opened` { 128 | logger.Info(`wechat app did open a redpacket`) 129 | } 130 | } else { 131 | w.mutex.Lock() 132 | done := w.pending[seq] 133 | delete(w.pending, seq) 134 | w.mutex.Unlock() 135 | 136 | done <- r.NewResultWithMap(result) 137 | } 138 | } 139 | -------------------------------------------------------------------------------- /bridge/proxy.go: -------------------------------------------------------------------------------- 1 | package bridge 2 | 3 | import ( 4 | "github.com/KevinGong2013/ggbot/bridge/arg" 5 | "github.com/KevinGong2013/ggbot/bridge/result" 6 | "github.com/KevinGong2013/wechat" 7 | ) 8 | 9 | //login client will login this uuid 10 | func (w *Wrapper) login(uuid string) *result.Result { 11 | 12 | a := arg.NewArg(arg.Login).Append(`uuid`, uuid) 13 | 14 | r := w.Call(a) 15 | 16 | return r 17 | } 18 | 19 | // OpenRedPacket designed to notify wechat app open read packet. 20 | func (w *Wrapper) OpenRedPacket() *result.Result { 21 | 22 | a := arg.NewArg(arg.RedPacket) 23 | 24 | r := w.Call(a) 25 | 26 | return r 27 | } 28 | 29 | // SendMessage ... 30 | func (w *Wrapper) SendMessage(msg wechat.Msg) *result.Result { 31 | return result.NewResultWithError(`unimplement`) 32 | } 33 | -------------------------------------------------------------------------------- /bridge/result/result.go: -------------------------------------------------------------------------------- 1 | package result 2 | 3 | import ( 4 | "encoding/json" 5 | "fmt" 6 | ) 7 | 8 | // Result ... 9 | type Result struct { 10 | Value []byte 11 | Err string 12 | } 13 | 14 | // IsSuccess ... 15 | func (result *Result) IsSuccess() bool { 16 | return len(result.Err) == 0 17 | } 18 | 19 | // IsFailure ... 20 | func (result *Result) IsFailure() bool { 21 | return !result.IsSuccess() 22 | } 23 | 24 | // NewResultWithError create return value with error 25 | func NewResultWithError(err string) *Result { 26 | return &Result{nil, err} 27 | } 28 | 29 | // NewResultWithValue create return value with value 30 | func NewResultWithValue(value []byte) *Result { 31 | return &Result{value, ``} 32 | } 33 | 34 | // NewResultWithMap ... 35 | func NewResultWithMap(m map[string]interface{}) *Result { 36 | by, err := json.Marshal(m) 37 | if err != nil { 38 | return NewResultWithError(`marshal m error`) 39 | } 40 | return NewResultWithValue(by) 41 | } 42 | 43 | // String .. 44 | func (result *Result) String() string { 45 | if result.IsSuccess() { 46 | return fmt.Sprintf(`[SUCCESSED] %v`, string(result.Value)) 47 | } 48 | return fmt.Sprintf(`[FAILED] %v`, result.Err) 49 | } 50 | -------------------------------------------------------------------------------- /bridge/uuid.go: -------------------------------------------------------------------------------- 1 | package bridge 2 | 3 | import "fmt" 4 | 5 | // ProcessUUID impolements UUIDProcessor interface 6 | func (w *Wrapper) ProcessUUID(uuid string) error { 7 | 8 | r := w.login(uuid) 9 | if r.IsFailure() { 10 | return fmt.Errorf(`bridge uuid processor errror: %v`, r.Err) 11 | } 12 | 13 | return nil 14 | } 15 | 16 | // UUIDDidConfirm impolements UUIDProcessor interface 17 | func (w *Wrapper) UUIDDidConfirm(err error) { 18 | if err != nil { 19 | logger.Errorf(`uuid has been invalidated`) 20 | } else { 21 | logger.Info(`uuid did confirmed`) 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /gurad.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "bytes" 5 | "encoding/json" 6 | "fmt" 7 | "strconv" 8 | "time" 9 | 10 | "github.com/KevinGong2013/wechat" 11 | ) 12 | 13 | // Guard ... 14 | type guard struct { 15 | bot *wechat.WeChat 16 | } 17 | 18 | func newGuard(bot *wechat.WeChat) *guard { 19 | return &guard{bot} 20 | } 21 | 22 | // AddFriend ... 23 | func (g *guard) addFriend(username, content string) error { 24 | return g.verifyUser(username, content, 2) 25 | } 26 | 27 | // AcceptAddFriend ... 28 | func (g *guard) acceptAddFriend(username, content string) error { 29 | return g.verifyUser(username, content, 3) 30 | } 31 | 32 | func (g *guard) verifyUser(username, content string, status int) error { 33 | 34 | url := fmt.Sprintf(`%s/webwxverifyuser?r=%s&%s`, g.bot.BaseURL, strconv.FormatInt(time.Now().Unix(), 10), g.bot.PassTicketKV()) 35 | 36 | data := map[string]interface{}{ 37 | `BaseRequest`: g.bot.BaseRequest, 38 | `Opcode`: status, 39 | `VerifyUserListSize`: 1, 40 | `VerifyUserList`: map[string]string{ 41 | `Value`: username, 42 | `VerifyUserTicket`: ``, 43 | }, 44 | `VerifyContent`: content, 45 | `SceneListCount`: 1, 46 | `SceneList`: 33, 47 | `skey`: g.bot.BaseRequest.Skey, 48 | } 49 | 50 | bs, _ := json.Marshal(data) 51 | 52 | var resp wechat.Response 53 | 54 | err := g.bot.Execute(url, bytes.NewReader(bs), &resp) 55 | if err != nil { 56 | return err 57 | } 58 | if resp.IsSuccess() { 59 | return nil 60 | } 61 | return resp.Error() 62 | } 63 | 64 | func (g *guard) autoAcceptAddFirendRequest(msg wechat.EventMsgData) { 65 | if msg.MsgType == 37 { 66 | rInfo := msg.OriginalMsg[`RecommendInfo`].(map[string]interface{}) 67 | err := g.addFriend(rInfo[`UserName`].(string), 68 | msg.OriginalMsg[`Ticket`].(string)) 69 | if err != nil { 70 | logger.Error(err) 71 | } 72 | err = g.bot.SendTextMsg(`新添加了一个好友`, `filehelper`) 73 | if err != nil { 74 | logger.Error(err) 75 | } 76 | } 77 | } 78 | -------------------------------------------------------------------------------- /main.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "io/ioutil" 5 | "os" 6 | 7 | "github.com/KevinGong2013/ggbot/service" 8 | "github.com/KevinGong2013/ggbot/uuidprocessor" 9 | "github.com/KevinGong2013/wechat" 10 | "github.com/Sirupsen/logrus" 11 | "gopkg.in/yaml.v2" 12 | ) 13 | 14 | var logger = logrus.WithFields(logrus.Fields{ 15 | "module": "ggbot", 16 | }) 17 | 18 | // Config .. 19 | type Config struct { 20 | ShowQRCodeOnTerminal bool 21 | FuzzyDiff bool 22 | UniqueGroupMember bool 23 | Features struct { 24 | Assistant struct { 25 | Enable bool 26 | OwnerGGID string 27 | } 28 | Guard struct { 29 | Enable bool 30 | } 31 | Tuling struct { 32 | Enable bool 33 | Key string 34 | } 35 | Xiaoice struct { 36 | Enable bool 37 | } 38 | WebhookService struct { 39 | Enable bool 40 | MsgWebhook string 41 | LoginStateWebhook string 42 | UUIDWebhook string 43 | } 44 | } 45 | } 46 | 47 | var config = Config{} 48 | 49 | func main() { 50 | 51 | tf := logrus.TextFormatter{} 52 | tf.FullTimestamp = true 53 | tf.TimestampFormat = `2006-01-02 15:04:05` 54 | logrus.SetFormatter(&tf) 55 | logrus.SetLevel(logrus.DebugLevel) 56 | 57 | path := `conf.yaml` 58 | _, err := os.Stat(path) 59 | if !(err == nil || os.IsExist(err)) { 60 | config.ShowQRCodeOnTerminal = false 61 | config.Features.Tuling.Key = `` 62 | config.Features.Tuling.Enable = false 63 | // config.FuzzyDiff = true 64 | data, _ := yaml.Marshal(config) 65 | createFile(path, data) 66 | } 67 | data, _ := ioutil.ReadFile(path) 68 | err = yaml.Unmarshal(data, &config) 69 | if err != nil { 70 | logger.Error(`配置文件不正确`) 71 | } 72 | 73 | options := wechat.DefaultConfigure() 74 | 75 | // 是否开启联系人模糊匹配 76 | // options.FuzzyDiff = config.FuzzyDiff 77 | options.UniqueGroupMember = config.UniqueGroupMember 78 | 79 | if config.ShowQRCodeOnTerminal { 80 | options.Processor = uuidprocessor.New() 81 | } 82 | 83 | var webhookService *service.Wrapper 84 | if config.Features.WebhookService.Enable { 85 | webhookService = service.NewWrapper(config.Features.WebhookService.UUIDWebhook) 86 | } 87 | 88 | if webhookService != nil && len(config.Features.WebhookService.UUIDWebhook) > 0 { 89 | options.Processor = webhookService 90 | } 91 | 92 | bot, err := wechat.NewBot(options) 93 | if err != nil { 94 | panic(err) 95 | } 96 | 97 | t := newTuling(config.Features.Tuling.Key, bot) 98 | x := newXiaoice(bot) 99 | // a := newAssistant(bot, "username") 100 | g := newGuard(bot) 101 | 102 | bot.Handle(`/msg`, func(evt wechat.Event) { 103 | logger.Debug(`begin handle [/msg]`) 104 | data := evt.Data.(wechat.EventMsgData) 105 | if config.Features.Tuling.Enable { 106 | go t.autoReplay(data) 107 | } 108 | if config.Features.Xiaoice.Enable { 109 | go x.autoReplay(data) 110 | } 111 | if config.Features.Guard.Enable { 112 | go g.autoAcceptAddFirendRequest(data) 113 | } 114 | // if config.Features.Assistant.Enable { 115 | // go a.handle(data) 116 | // } 117 | if webhookService != nil && len(config.Features.WebhookService.MsgWebhook) > 0 { 118 | go webhookService.Forward(config.Features.WebhookService.MsgWebhook, data) 119 | } 120 | }) 121 | 122 | // disable xiaoice 123 | // bot.Handle(`/login`, func(arg2 wechat.Event) { 124 | // isSuccess := arg2.Data.(int) == 1 125 | // if isSuccess && x != nil { 126 | // if cs, err := bot.ContactsByNickName(`小冰`); err == nil { 127 | // for _, c := range cs { 128 | // if c.Type == wechat.Offical { 129 | // x.un = c.UserName // 更新小冰的UserName 130 | // break 131 | // } 132 | // } 133 | // } 134 | // } 135 | // if webhookService != nil && len(config.Features.WebhookService.LoginStateWebhook) > 0 { 136 | // go webhookService.Forward(config.Features.WebhookService.LoginStateWebhook, arg2.Data) 137 | // } 138 | // }) 139 | 140 | bot.Go() 141 | } 142 | 143 | func createFile(name string, data []byte) (err error) { 144 | 145 | defer func() { 146 | if err != nil { 147 | logger.Error(err) 148 | } 149 | }() 150 | 151 | oflag := os.O_CREATE | os.O_WRONLY | os.O_TRUNC 152 | 153 | file, err := os.OpenFile(name, oflag, 0666) 154 | if err != nil { 155 | return 156 | } 157 | defer file.Close() 158 | 159 | _, err = file.Write(data) 160 | 161 | return 162 | } 163 | -------------------------------------------------------------------------------- /service/restfull.go: -------------------------------------------------------------------------------- 1 | package service 2 | 3 | import ( 4 | "bytes" 5 | "encoding/json" 6 | "io/ioutil" 7 | "net/http" 8 | 9 | "github.com/KevinGong2013/wechat" 10 | "github.com/pressly/chi" 11 | "github.com/pressly/chi/middleware" 12 | 13 | log "github.com/Sirupsen/logrus" 14 | ) 15 | 16 | var innerlogger = log.WithFields(log.Fields{ 17 | "module": "service", 18 | }) 19 | 20 | // Wrapper ... 21 | type Wrapper struct { 22 | uuidWebhookURL string 23 | r *chi.Mux 24 | wx *wechat.WeChat 25 | } 26 | 27 | // NewWrapper ... 28 | func NewWrapper(uuidWebhookURL string) *Wrapper { 29 | 30 | r := chi.NewRouter() 31 | // A good base middleware stack 32 | r.Use(middleware.RequestID) 33 | r.Use(middleware.RealIP) 34 | r.Use(middleware.Logger) 35 | r.Use(middleware.Recoverer) 36 | 37 | w := &Wrapper{uuidWebhookURL, r, nil} 38 | w.initService() 39 | 40 | go http.ListenAndServe(`:3280`, r) 41 | 42 | return w 43 | } 44 | 45 | func (w *Wrapper) initService() { 46 | 47 | // w.r.Get(`/contacts/:nickName`, func(writer http.ResponseWriter, req *http.Request) { 48 | // if w.wx == nil { 49 | // http.Error(writer, `wechat did logout`, 500) 50 | // } else { 51 | // nn := chi.URLParam(req, `nickName`) 52 | // contacts, err := w.wx.ContactsByNickName(nn) 53 | // if err != nil { 54 | // http.Error(writer, `not found contact`, 404) 55 | // } else { 56 | // bs, _ := json.Marshal(contacts) 57 | // writer.Write(bs) 58 | // } 59 | // } 60 | // }) 61 | 62 | w.r.Post(`/msg`, func(writer http.ResponseWriter, req *http.Request) { 63 | if w.wx == nil { 64 | http.Error(writer, `wechat did logout`, 500) 65 | } else { 66 | var body map[string]interface{} 67 | defer req.Body.Close() 68 | bs, _ := ioutil.ReadAll(req.Body) 69 | err := json.Unmarshal(bs, &body) 70 | if err != nil { 71 | http.Error(writer, err.Error(), 500) 72 | } else { 73 | content, _ := body[`content`].(string) 74 | to, _ := body[`to`].(string) 75 | w.wx.SendTextMsg(content, to) 76 | } 77 | } 78 | }) 79 | } 80 | 81 | // Forward data to webhook 82 | func (w *Wrapper) Forward(url string, data interface{}) error { 83 | 84 | bs, err := json.Marshal(data) 85 | if err != nil { 86 | return err 87 | } 88 | 89 | _, err = http.Post(url, `application/json`, bytes.NewReader(bs)) 90 | 91 | return err 92 | } 93 | -------------------------------------------------------------------------------- /service/uuid.go: -------------------------------------------------------------------------------- 1 | package service 2 | 3 | // ProcessUUID impolements UUIDProcessor interface 4 | func (w *Wrapper) ProcessUUID(uuid string) error { 5 | return w.Forward(w.uuidWebhookURL, map[string]interface{}{ 6 | `uuid`: uuid, 7 | `didConfirm`: false, 8 | }) 9 | } 10 | 11 | // UUIDDidConfirm impolements UUIDProcessor interface 12 | func (w *Wrapper) UUIDDidConfirm(err error) { 13 | w.Forward(w.uuidWebhookURL, map[string]interface{}{ 14 | `uuid`: ``, 15 | `didConfirm`: true, 16 | }) 17 | } 18 | -------------------------------------------------------------------------------- /tuling.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "encoding/json" 5 | "errors" 6 | "net/http" 7 | "net/url" 8 | 9 | "github.com/KevinGong2013/wechat" 10 | ) 11 | 12 | type tuling struct { 13 | key string 14 | bot *wechat.WeChat 15 | } 16 | 17 | func newTuling(key string, bot *wechat.WeChat) *tuling { 18 | return &tuling{key, bot} 19 | } 20 | 21 | func (t *tuling) autoReplay(data wechat.EventMsgData) { 22 | // if data.IsSendedByMySelf { 23 | // return 24 | // } 25 | // replay, err := t.response(data.Content, data.FromUserName, data.FromGGID) 26 | // if err != nil { 27 | // logger.Error(err) 28 | // t.bot.SendTextMsg(`你接着说 ... `, data.FromUserName) 29 | // } else { 30 | // t.bot.SendTextMsg(replay, data.FromUserName) 31 | // } 32 | } 33 | 34 | func (t *tuling) response(msg, to, userid string) (string, error) { 35 | 36 | values := url.Values{} 37 | 38 | values.Add(`key`, t.key) 39 | values.Add(`info`, msg) 40 | values.Add(`userid`, userid) 41 | 42 | resp, err := http.PostForm(`http://www.tuling123.com/openapi/api`, values) 43 | if err != nil { 44 | return ``, err 45 | } 46 | 47 | reader := resp.Body 48 | defer resp.Body.Close() 49 | 50 | result := make(map[string]interface{}) 51 | 52 | err = json.NewDecoder(reader).Decode(&result) 53 | if err != nil { 54 | return ``, err 55 | } 56 | 57 | code := result[`code`].(float64) 58 | 59 | if code == 100000 { 60 | text := result[`text`].(string) 61 | return text, nil 62 | } else if code == 200000 { 63 | text := result[`text`].(string) 64 | url := result[`url`].(string) 65 | return text + ` 66 | ` + url, nil 67 | } 68 | 69 | logger.Errorf(`info: [%s], userid: [%s]`, msg, userid) 70 | logger.Error(result) 71 | return ``, errors.New(`tuling api unkonw error`) 72 | } 73 | -------------------------------------------------------------------------------- /uuidprocessor/processor.go: -------------------------------------------------------------------------------- 1 | package uuidprocessor 2 | 3 | import ( 4 | "fmt" 5 | 6 | log "github.com/Sirupsen/logrus" 7 | "github.com/skip2/go-qrcode" 8 | ) 9 | 10 | const ( 11 | fg = "\033[48;5;2m \033[0m" 12 | bg = "\033[48;5;7m \033[0m" 13 | ) 14 | 15 | var logger = log.WithFields(log.Fields{ 16 | "module": "uuidProcessor", 17 | }) 18 | 19 | // UUIDProcessor ... 20 | type UUIDProcessor struct{} 21 | 22 | // New a uuid processor 23 | func New() *UUIDProcessor { 24 | return &UUIDProcessor{} 25 | } 26 | 27 | // ProcessUUID implements UUIDProcessor interface 28 | func (up *UUIDProcessor) ProcessUUID(uuid string) error { 29 | 30 | content := fmt.Sprintf(`https://login.weixin.qq.com/l/%s`, uuid) 31 | 32 | code, err := qrcode.New(content, qrcode.Low) 33 | if err != nil { 34 | return err 35 | } 36 | 37 | showQRCode(code) 38 | 39 | return nil 40 | } 41 | 42 | // UUIDDidConfirm implements UUIDProcessor interface 43 | func (up *UUIDProcessor) UUIDDidConfirm(err error) { 44 | if err != nil { 45 | logger.Errorf(`above QRCODE has been invalidated`) 46 | } else { 47 | logger.Info(`uuid did confirmed`) 48 | } 49 | } 50 | 51 | func showQRCode(code *qrcode.QRCode) { 52 | 53 | for ir, row := range code.Bitmap() { 54 | lr := len(row) 55 | if ir == 0 || ir == 1 || ir == 2 || 56 | ir == lr-1 || ir == lr-2 || ir == lr-3 { 57 | continue 58 | } 59 | for ic, col := range row { 60 | lc := len(code.Bitmap()) 61 | if ic == 0 || ic == 1 || ic == 2 || 62 | ic == lc-1 || ic == lc-2 || ic == lc-3 { 63 | continue 64 | } 65 | if col { 66 | fmt.Print(fg) 67 | } else { 68 | fmt.Print(bg) 69 | } 70 | } 71 | fmt.Println() 72 | } 73 | } 74 | -------------------------------------------------------------------------------- /xiaoice.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "sync" 5 | 6 | wx "github.com/KevinGong2013/wechat" 7 | ) 8 | 9 | type xiaoice struct { 10 | sync.Mutex 11 | un string 12 | waitting []string 13 | bot *wx.WeChat 14 | } 15 | 16 | func newXiaoice(wx *wx.WeChat) *xiaoice { 17 | x := &xiaoice{} 18 | x.bot = wx 19 | return x 20 | } 21 | 22 | func (x *xiaoice) autoReplay(msg wx.EventMsgData) { 23 | if msg.IsSentByMySelf { 24 | return 25 | } 26 | if msg.FromUserName == x.un { // 小冰发来的消息 27 | x.Lock() 28 | x.Unlock() 29 | 30 | count := len(x.waitting) 31 | if count == 0 { 32 | logger.Warnf(`msg Form xiaoice %s`, msg.Content) 33 | return 34 | } 35 | to := x.waitting[count-1] 36 | x.waitting = x.waitting[:count-1] 37 | 38 | if msg.IsMediaMsg { 39 | if path, err := x.bot.DownloadMedia(msg.MediaURL, msg.OriginalMsg[`MsgId`].(string)); err == nil { 40 | x.bot.SendFile(path, to) 41 | } 42 | } else { 43 | x.bot.SendTextMsg(msg.Content, to) 44 | } 45 | } else if !msg.IsSentByMySelf { // 转发别人的消息到小冰 46 | var err error 47 | if msg.IsMediaMsg { 48 | if path, e := x.bot.DownloadMedia(msg.MediaURL, msg.OriginalMsg[`MsgId`].(string)); e == nil { 49 | err = x.bot.SendFile(path, x.un) 50 | } else { 51 | err = e 52 | } 53 | } else { 54 | err = x.bot.SendTextMsg(msg.Content, x.un) 55 | } 56 | 57 | if err == nil { 58 | x.Lock() 59 | defer x.Unlock() 60 | x.waitting = append(x.waitting, msg.FromUserName) 61 | } else { 62 | logger.Error(err) 63 | } 64 | } 65 | } 66 | --------------------------------------------------------------------------------