@pacoyang/wechaty-puppet-wechat 0.28.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (167) hide show
  1. package/.editorconfig +14 -0
  2. package/.eslintrc.js +7 -0
  3. package/.github/workflows/npm.yml +91 -0
  4. package/.markdownlintrc +13 -0
  5. package/.travis.yml +62 -0
  6. package/.vscode/settings.json +73 -0
  7. package/CODEOWNERS +9 -0
  8. package/LICENSE +201 -0
  9. package/NOTICE +8 -0
  10. package/README.md +145 -0
  11. package/dist/examples/demo.d.ts +20 -0
  12. package/dist/examples/demo.d.ts.map +1 -0
  13. package/dist/examples/demo.js +113 -0
  14. package/dist/examples/demo.js.map +1 -0
  15. package/dist/scripts/retry-unit-tests.d.ts +10 -0
  16. package/dist/scripts/retry-unit-tests.d.ts.map +1 -0
  17. package/dist/scripts/retry-unit-tests.js +47 -0
  18. package/dist/scripts/retry-unit-tests.js.map +1 -0
  19. package/dist/src/bridge.d.ts +100 -0
  20. package/dist/src/bridge.d.ts.map +1 -0
  21. package/dist/src/bridge.js +927 -0
  22. package/dist/src/bridge.js.map +1 -0
  23. package/dist/src/bridge.spec.d.ts +21 -0
  24. package/dist/src/bridge.spec.d.ts.map +1 -0
  25. package/dist/src/bridge.spec.js +191 -0
  26. package/dist/src/bridge.spec.js.map +1 -0
  27. package/dist/src/config.d.ts +10 -0
  28. package/dist/src/config.d.ts.map +1 -0
  29. package/dist/src/config.js +57 -0
  30. package/dist/src/config.js.map +1 -0
  31. package/dist/src/event.d.ts +23 -0
  32. package/dist/src/event.d.ts.map +1 -0
  33. package/dist/src/event.js +166 -0
  34. package/dist/src/event.js.map +1 -0
  35. package/dist/src/event.spec.d.ts +3 -0
  36. package/dist/src/event.spec.d.ts.map +1 -0
  37. package/dist/src/event.spec.js +41 -0
  38. package/dist/src/event.spec.js.map +1 -0
  39. package/dist/src/firer.d.ts +50 -0
  40. package/dist/src/firer.d.ts.map +1 -0
  41. package/dist/src/firer.js +409 -0
  42. package/dist/src/firer.js.map +1 -0
  43. package/dist/src/firer.spec.d.ts +21 -0
  44. package/dist/src/firer.spec.d.ts.map +1 -0
  45. package/dist/src/firer.spec.js +190 -0
  46. package/dist/src/firer.spec.js.map +1 -0
  47. package/dist/src/mod.d.ts +23 -0
  48. package/dist/src/mod.d.ts.map +1 -0
  49. package/dist/src/mod.js +28 -0
  50. package/dist/src/mod.js.map +1 -0
  51. package/dist/src/puppet-wechat.d.ts +164 -0
  52. package/dist/src/puppet-wechat.d.ts.map +1 -0
  53. package/dist/src/puppet-wechat.js +1287 -0
  54. package/dist/src/puppet-wechat.js.map +1 -0
  55. package/dist/src/puppet-wechat.spec.d.ts +21 -0
  56. package/dist/src/puppet-wechat.spec.d.ts.map +1 -0
  57. package/dist/src/puppet-wechat.spec.js +123 -0
  58. package/dist/src/puppet-wechat.spec.js.map +1 -0
  59. package/dist/src/pure-function-helpers/is-type.d.ts +3 -0
  60. package/dist/src/pure-function-helpers/is-type.d.ts.map +1 -0
  61. package/dist/src/pure-function-helpers/is-type.js +12 -0
  62. package/dist/src/pure-function-helpers/is-type.js.map +1 -0
  63. package/dist/src/pure-function-helpers/message-extname.d.ts +3 -0
  64. package/dist/src/pure-function-helpers/message-extname.d.ts.map +1 -0
  65. package/dist/src/pure-function-helpers/message-extname.js +44 -0
  66. package/dist/src/pure-function-helpers/message-extname.js.map +1 -0
  67. package/dist/src/pure-function-helpers/message-filename.d.ts +3 -0
  68. package/dist/src/pure-function-helpers/message-filename.d.ts.map +1 -0
  69. package/dist/src/pure-function-helpers/message-filename.js +19 -0
  70. package/dist/src/pure-function-helpers/message-filename.js.map +1 -0
  71. package/dist/src/pure-function-helpers/message-raw-payload-parser.d.ts +4 -0
  72. package/dist/src/pure-function-helpers/message-raw-payload-parser.d.ts.map +1 -0
  73. package/dist/src/pure-function-helpers/message-raw-payload-parser.js +67 -0
  74. package/dist/src/pure-function-helpers/message-raw-payload-parser.js.map +1 -0
  75. package/dist/src/pure-function-helpers/mod.d.ts +7 -0
  76. package/dist/src/pure-function-helpers/mod.d.ts.map +1 -0
  77. package/dist/src/pure-function-helpers/mod.js +23 -0
  78. package/dist/src/pure-function-helpers/mod.js.map +1 -0
  79. package/dist/src/pure-function-helpers/normalize-scan-status.d.ts +3 -0
  80. package/dist/src/pure-function-helpers/normalize-scan-status.d.ts.map +1 -0
  81. package/dist/src/pure-function-helpers/normalize-scan-status.js +21 -0
  82. package/dist/src/pure-function-helpers/normalize-scan-status.js.map +1 -0
  83. package/dist/src/pure-function-helpers/normalize-scan-status.spec.d.ts +21 -0
  84. package/dist/src/pure-function-helpers/normalize-scan-status.spec.d.ts.map +1 -0
  85. package/dist/src/pure-function-helpers/normalize-scan-status.spec.js +37 -0
  86. package/dist/src/pure-function-helpers/normalize-scan-status.spec.js.map +1 -0
  87. package/dist/src/pure-function-helpers/web-message-type.d.ts +4 -0
  88. package/dist/src/pure-function-helpers/web-message-type.d.ts.map +1 -0
  89. package/dist/src/pure-function-helpers/web-message-type.js +58 -0
  90. package/dist/src/pure-function-helpers/web-message-type.js.map +1 -0
  91. package/dist/src/pure-function-helpers/xml.d.ts +14 -0
  92. package/dist/src/pure-function-helpers/xml.d.ts.map +1 -0
  93. package/dist/src/pure-function-helpers/xml.js +64 -0
  94. package/dist/src/pure-function-helpers/xml.js.map +1 -0
  95. package/dist/src/version.d.ts +6 -0
  96. package/dist/src/version.d.ts.map +1 -0
  97. package/dist/src/version.js +9 -0
  98. package/dist/src/version.js.map +1 -0
  99. package/dist/src/version.spec.d.ts +3 -0
  100. package/dist/src/version.spec.d.ts.map +1 -0
  101. package/dist/src/version.spec.js +13 -0
  102. package/dist/src/version.spec.js.map +1 -0
  103. package/dist/src/web-schemas.d.ts +249 -0
  104. package/dist/src/web-schemas.d.ts.map +1 -0
  105. package/dist/src/web-schemas.js +118 -0
  106. package/dist/src/web-schemas.js.map +1 -0
  107. package/dist/src/wechaty-bro.js +868 -0
  108. package/dist/tests/puppeteer-contact.spec.d.ts +21 -0
  109. package/dist/tests/puppeteer-contact.spec.d.ts.map +1 -0
  110. package/dist/tests/puppeteer-contact.spec.js +67 -0
  111. package/dist/tests/puppeteer-contact.spec.js.map +1 -0
  112. package/dist/tests/puppeteer-friendship.spec.d.ts +21 -0
  113. package/dist/tests/puppeteer-friendship.spec.d.ts.map +1 -0
  114. package/dist/tests/puppeteer-friendship.spec.js +115 -0
  115. package/dist/tests/puppeteer-friendship.spec.js.map +1 -0
  116. package/dist/tests/puppeteer-message.spec.d.ts +21 -0
  117. package/dist/tests/puppeteer-message.spec.d.ts.map +1 -0
  118. package/dist/tests/puppeteer-message.spec.js +184 -0
  119. package/dist/tests/puppeteer-message.spec.js.map +1 -0
  120. package/dist/tests/puppeteer-room.spec.d.ts +21 -0
  121. package/dist/tests/puppeteer-room.spec.d.ts.map +1 -0
  122. package/dist/tests/puppeteer-room.spec.js +129 -0
  123. package/dist/tests/puppeteer-room.spec.js.map +1 -0
  124. package/dist/tests/puppeteer.spec.d.ts +21 -0
  125. package/dist/tests/puppeteer.spec.d.ts.map +1 -0
  126. package/dist/tests/puppeteer.spec.js +213 -0
  127. package/dist/tests/puppeteer.spec.js.map +1 -0
  128. package/docs/images/puppeteer-logo.png +0 -0
  129. package/docs/images/wechaty-puppet-wechat.png +0 -0
  130. package/examples/demo.ts +130 -0
  131. package/package.json +94 -0
  132. package/scripts/generate-version.sh +18 -0
  133. package/scripts/npm-pack-testing.sh +49 -0
  134. package/scripts/package-publish-config-tag.sh +13 -0
  135. package/scripts/retry-unit-tests.ts +57 -0
  136. package/src/bridge.spec.ts +224 -0
  137. package/src/bridge.ts +1122 -0
  138. package/src/config.ts +71 -0
  139. package/src/event.spec.ts +41 -0
  140. package/src/event.ts +247 -0
  141. package/src/firer.spec.ts +208 -0
  142. package/src/firer.ts +530 -0
  143. package/src/mod.ts +29 -0
  144. package/src/puppet-wechat.spec.ts +149 -0
  145. package/src/puppet-wechat.ts +1659 -0
  146. package/src/pure-function-helpers/is-type.ts +7 -0
  147. package/src/pure-function-helpers/message-extname.ts +57 -0
  148. package/src/pure-function-helpers/message-filename.ts +25 -0
  149. package/src/pure-function-helpers/message-raw-payload-parser.ts +85 -0
  150. package/src/pure-function-helpers/mod.ts +6 -0
  151. package/src/pure-function-helpers/normalize-scan-status.spec.ts +44 -0
  152. package/src/pure-function-helpers/normalize-scan-status.ts +23 -0
  153. package/src/pure-function-helpers/web-message-type.ts +71 -0
  154. package/src/pure-function-helpers/xml.ts +79 -0
  155. package/src/typings.d.ts +19 -0
  156. package/src/version.spec.ts +10 -0
  157. package/src/version.ts +6 -0
  158. package/src/web-schemas.ts +298 -0
  159. package/src/wechaty-bro.js +868 -0
  160. package/tests/fixtures/inject-file.js +7 -0
  161. package/tests/fixtures/smoke-testing.ts +42 -0
  162. package/tests/puppeteer-contact.spec.ts +78 -0
  163. package/tests/puppeteer-friendship.spec.ts +157 -0
  164. package/tests/puppeteer-message.spec.ts +249 -0
  165. package/tests/puppeteer-room.spec.ts +166 -0
  166. package/tests/puppeteer.spec.ts +262 -0
  167. package/tsconfig.json +26 -0
@@ -0,0 +1,868 @@
1
+ /**
2
+ * Wechaty - https://github.com/chatie/wechaty
3
+ *
4
+ * @copyright 2016-2018 Huan LI <zixia@zixia.net>
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
+
20
+ /**
21
+ * Wechaty - Wechat for Bot, and human who talk to bot.
22
+ *
23
+ * PuppetWeChat: WechatyBro
24
+ *
25
+ * Inject this js code to browser,
26
+ * in order to interactive with wechat web program.
27
+ *
28
+ * ATTENTION:
29
+ *
30
+ * JavaScript in this file will be ran inside:
31
+ *
32
+ * BROWSER
33
+ *
34
+ * instead of
35
+ *
36
+ * NODE.js
37
+ *
38
+ * read more about this in puppet-web-bridge.ts
39
+ */
40
+
41
+ /* eslint no-undef: off */
42
+ /* eslint sort-keys: off */
43
+
44
+ (function () {
45
+ function init () {
46
+ if (!angularIsReady()) {
47
+ retObj.code = 503 // 503 SERVICE UNAVAILABLE https://httpstatuses.com/503
48
+ retObj.message = 'init() without a ready angular env'
49
+ return retObj
50
+ }
51
+
52
+ if (WechatyBro.vars.initState === true) {
53
+ log('WechatyBro.init() called twice: already inited')
54
+ retObj.code = 304 // 304 Not Modified https://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.3.5
55
+ retObj.message = 'init() already inited before. returned with do nothing'
56
+ return retObj
57
+ }
58
+
59
+ if (MMCgiLogined()) {
60
+ login('page refresh')
61
+ }
62
+
63
+ glueToAngular()
64
+ hookEvents()
65
+ hookRecalledMsgProcess()
66
+
67
+ log('init() scanCode: ' + WechatyBro.vars.scanCode)
68
+ setTimeout(() => checkScan(), 1000)
69
+
70
+ heartBeat(true)
71
+
72
+ log('inited!. ;-D')
73
+ WechatyBro.vars.initState = true
74
+
75
+ retObj.code = 200
76
+ retObj.message = 'WechatyBro Init Succ'
77
+ return retObj
78
+ }
79
+
80
+ function log (text) {
81
+ WechatyBro.emit('log', text)
82
+ }
83
+
84
+ /**
85
+ *
86
+ * Functions that Glued with AngularJS
87
+ *
88
+ */
89
+ function MMCgiLogined () {
90
+ return !!(window.MMCgi && window.MMCgi.isLogin)
91
+ }
92
+
93
+ function angularIsReady () {
94
+ // don't log inside, because we has not yet init clog here.
95
+ return !!(
96
+ (typeof angular) !== 'undefined'
97
+ && angular.element
98
+ && angular.element('body')
99
+ && angular.element(document).injector()
100
+ )
101
+ }
102
+
103
+ function heartBeat (firstTime) {
104
+ const TIMEOUT = 15000 // 15s
105
+ if (firstTime && WechatyBro.vars.heartBeatTimmer) {
106
+ log('heartBeat timer exist when 1st time is true? return for do nothing')
107
+ return
108
+ }
109
+ WechatyBro.emit('heartbeat', 'heartbeat@browser')
110
+ WechatyBro.vars.heartBeatTimmer = setTimeout(heartBeat, TIMEOUT)
111
+ return TIMEOUT
112
+ }
113
+
114
+ function glueToAngular () {
115
+ const injector = angular.element(document).injector()
116
+ if (!injector) {
117
+ throw new Error('glueToAngular cant get injector(right now)')
118
+ }
119
+ const accountFactory = injector.get('accountFactory')
120
+ // var appFactory = injector.get('appFactory')
121
+ const chatroomFactory = injector.get('chatroomFactory')
122
+ const chatFactory = injector.get('chatFactory')
123
+ const contactFactory = injector.get('contactFactory')
124
+ const confFactory = injector.get('confFactory')
125
+ const emojiFactory = injector.get('emojiFactory')
126
+ const loginFactory = injector.get('loginFactory')
127
+ const utilFactory = injector.get('utilFactory')
128
+
129
+ const http = injector.get('$http')
130
+ const state = injector.get('$state')
131
+ const mmHttp = injector.get('mmHttp')
132
+
133
+ const appScope = angular.element('[ng-controller="appController"]').scope()
134
+ const rootScope = injector.get('$rootScope')
135
+ const loginScope = angular.element('[ng-controller="loginController"]').scope()
136
+
137
+ /*
138
+ // method 1
139
+ appFactory.syncOrig = appFactory.sync
140
+ appFactory.syncCheckOrig = appFactory.syncCheck
141
+ appFactory.sync = function() { WechatyBro.log('appFactory.sync() !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!'); return appFactory.syncOrig(arguments) }
142
+ appFactory.syncCheck = function() { WechatyBro.log('appFactory.syncCheck() !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!'); return appFactory.syncCheckOrig(arguments) }
143
+
144
+ // method 2
145
+ $.ajaxOrig = $.ajax
146
+ $.ajax = function() { WechatyBro.log('$.ajax() !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!'); return $.ajaxOrig(arguments) }
147
+
148
+ $.ajax({
149
+ url: "https://wx.qq.com/zh_CN/htmledition/v2/images/webwxgeticon.jpg"
150
+ , type: "GET"
151
+ }).done(function (response) {
152
+ alert("success");
153
+ })
154
+
155
+ // method 3 - mmHttp
156
+ mmHttp.getOrig = mmHttp.get
157
+ mmHttp.get = function() { WechatyBro.log('mmHttp.get() !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!'); return mmHttp.getOrig(arguments) }
158
+ */
159
+
160
+ /**
161
+ * generate $scope with a contoller (as it is not assigned in html staticly)
162
+ * https://github.com/angular/angular.js/blob/a4e60cb6970d8b6fa9e0af4b9f881ee3ba7fdc99/test/ng/controllerSpec.js#L24
163
+ */
164
+ const contentChatScope = rootScope.$new()
165
+ injector.get('$controller')('contentChatController', { $scope: contentChatScope })
166
+
167
+ // get all we need from wx in browser(angularjs)
168
+ WechatyBro.glue = {
169
+ http,
170
+ injector,
171
+ mmHttp,
172
+ state,
173
+
174
+ accountFactory,
175
+ chatFactory,
176
+ chatroomFactory,
177
+ confFactory,
178
+ contactFactory,
179
+ emojiFactory,
180
+ loginFactory,
181
+ utilFactory,
182
+
183
+ rootScope,
184
+ appScope,
185
+ loginScope,
186
+
187
+ contentChatScope,
188
+ }
189
+
190
+ return true
191
+ }
192
+
193
+ function checkScan () {
194
+ // log('checkScan()')
195
+ if (loginState()) {
196
+ log('checkScan() - already login, no more check, and return(only)') // but I will emit a login event')
197
+ // login('checkScan found already login')
198
+ return
199
+ }
200
+
201
+ const loginScope = WechatyBro.glue.loginScope
202
+ if (!loginScope) {
203
+ log('checkScan() - loginScope disappeared, TODO: find out the reason why this happen')
204
+ // login('loginScope disappeared')
205
+ // return
206
+ return setTimeout(checkScan, 1000)
207
+ }
208
+
209
+ // loginScope.code:
210
+ // 0: 显示二维码
211
+ // 408: 未确认(显示二维码后30秒触发)
212
+ // 201: 扫描,未确认
213
+ // 200: 登录成功
214
+ const code = +loginScope.code
215
+ const url = loginScope.qrcodeUrl
216
+ log('checkScan() code:' + code + ' url:' + url + ' scanCode:' + WechatyBro.vars.scanCode)
217
+
218
+ if (url && code !== WechatyBro.vars.scanCode) {
219
+ log('checkScan() - code change detected: from '
220
+ + WechatyBro.vars.scanCode
221
+ + ' to '
222
+ + code
223
+ )
224
+ WechatyBro.emit('scan', {
225
+ code : code,
226
+ url : url,
227
+ })
228
+ WechatyBro.vars.scanCode = code
229
+ }
230
+
231
+ if (code !== 200) {
232
+ return setTimeout(checkScan, 1000)
233
+ }
234
+
235
+ WechatyBro.vars.scanCode = null
236
+ loginScope.code = null
237
+
238
+ // wait a while because the account maybe blocked by tencent,
239
+ // then there will be a dialog should appear
240
+ setTimeout(() => login('scan code 200'), 1000)
241
+ }
242
+
243
+ function loginState (state) {
244
+ if (typeof state === 'undefined') {
245
+ return !!WechatyBro.vars.loginState
246
+ }
247
+ WechatyBro.vars.loginState = state
248
+ }
249
+
250
+ function login (data) {
251
+ log('login(' + data + ')')
252
+ loginState(true)
253
+ WechatyBro.emit('login', data)
254
+ }
255
+
256
+ function logout (data) {
257
+ log('logout(' + data + ')')
258
+
259
+ loginState(false)
260
+
261
+ // WechatyBro.emit('logout', data)
262
+ if (WechatyBro.glue.loginFactory) {
263
+ WechatyBro.glue.loginFactory.loginout(0)
264
+ } else {
265
+ log('logout() WechatyBro.glue.loginFactory NOT found')
266
+ }
267
+
268
+ setTimeout(() => checkScan(), 1000)
269
+ }
270
+
271
+ function ding (data) {
272
+ log('recv ding')
273
+ return data || 'dong'
274
+ }
275
+
276
+ function hookEvents () {
277
+ const rootScope = WechatyBro.glue.rootScope
278
+ const appScope = WechatyBro.glue.appScope
279
+ if (!rootScope || !appScope) {
280
+ log('hookEvents() no rootScope')
281
+ return false
282
+ }
283
+ rootScope.$on('message:add:success', function (event, data) {
284
+ // if (!loginState()) { // in case of we missed the pageInit event
285
+ // login('by event[message:add:success]')
286
+ // }
287
+ WechatyBro.emit('message', data)
288
+ })
289
+ // rootScope.$on('root:pageInit:success', function (event, data) {
290
+ // login('by event[root:pageInit:success]')
291
+ // })
292
+ // newLoginPage seems not stand for a user login action
293
+ // appScope.$on("newLoginPage", function(event, data) {
294
+ // login('by event[newLoginPage]')
295
+ // })
296
+ window.addEventListener('unload', function (e) {
297
+ // XXX only 1 event can be emitted here???
298
+ WechatyBro.emit('unload', String(e))
299
+ log('event unload')
300
+ })
301
+ return true
302
+ }
303
+
304
+ function hookRecalledMsgProcess () {
305
+ const chatFactory = WechatyBro.glue.chatFactory
306
+ const utilFactory = WechatyBro.glue.utilFactory
307
+ const confFactory = WechatyBro.glue.confFactory
308
+
309
+ // hook chatFactory._recalledMsgProcess, resolve emit RECALLED type msg
310
+ const oldRecalledMsgProcess = chatFactory._recalledMsgProcess
311
+ chatFactory._recalledMsgProcess = function (msg) {
312
+ try {
313
+ oldRecalledMsgProcess.call(chatFactory, msg)
314
+ } catch (e) {
315
+ log('call oldRecalledMsgProcess failed:' + e.message)
316
+ }
317
+ const m = Object.assign({}, msg)
318
+ let content = utilFactory.htmlDecode(m.MMActualContent)
319
+ content = utilFactory.encodeEmoji(content)
320
+ // remove <br/> here
321
+ content = content.replace(/<br.*?\/>/g, '')
322
+ const revokemsg = utilFactory.xml2json(content).revokemsg
323
+ if (revokemsg.msgid) {
324
+ // add recalled message
325
+ m.MsgType = confFactory.MSGTYPE_RECALLED
326
+ m.MMActualContent = revokemsg.msgid
327
+ chatFactory.addChatMessage(m)
328
+ }
329
+ }
330
+ }
331
+
332
+ /**
333
+ *
334
+ * Help Functions which Proxy to WXAPP AngularJS Scope & Factory
335
+ * getMsgImg(message.MsgId,'slave')
336
+ * getMsgImg(message.MsgId,'big',message)
337
+ */
338
+ function getMsgImg (id, type, message) {
339
+ const contentChatScope = WechatyBro.glue.contentChatScope
340
+ if (!contentChatScope) {
341
+ throw new Error('getMsgImg() contentChatScope not found')
342
+ }
343
+ const path = contentChatScope.getMsgImg(id, type, message)
344
+ return window.location.origin + path
345
+ // https://wx.qq.com/?&lang=en_US/cgi-bin/mmwebwx-bin/webwxgetmsgimg?&MsgID=4520385745174034093&skey=%40crypt_f9cec94b_a3aa5c868466d81bc518293eb292926e
346
+ // https://wx.qq.com/cgi-bin/mmwebwx-bin/webwxgetmsgimg?&MsgID=8454987316459381112&skey=%40crypt_f9cec94b_bd210b2224f217afeab8d462af70cf53
347
+ }
348
+
349
+ function getMsgEmoticon (id) {
350
+ const chatFactory = WechatyBro.glue.chatFactory
351
+
352
+ const message = chatFactory.getMsg(id)
353
+ return message.MMPreviewSrc || getMsgImg(message.MsgId, 'big', message) || message.MMThumbSrc
354
+ }
355
+
356
+ function getMsgVideo (id) {
357
+ const contentChatScope = WechatyBro.glue.contentChatScope
358
+ if (!contentChatScope) {
359
+ throw new Error('getMsgVideo() contentChatScope not found')
360
+ }
361
+ const path = contentChatScope.getMsgVideo(id)
362
+ return window.location.origin + path
363
+ }
364
+
365
+ /**
366
+ * from playVoice()
367
+ */
368
+ function getMsgVoice (id) {
369
+ const confFactory = WechatyBro.glue.confFactory
370
+ const accountFactory = WechatyBro.glue.accountFactory
371
+
372
+ const path = confFactory.API_webwxgetvoice + '?msgid=' + id + '&skey=' + accountFactory.getSkey()
373
+ return window.location.origin + path
374
+ }
375
+
376
+ function getMsgPublicLinkImg (id) {
377
+ const path = '/cgi-bin/mmwebwx-bin/webwxgetpubliclinkimg?url=xxx&msgid=' + id + '&pictype=location'
378
+ return window.location.origin + path
379
+ }
380
+
381
+ function getBaseRequest () {
382
+ const accountFactory = WechatyBro.glue.accountFactory
383
+ const BaseRequest = accountFactory.getBaseRequest()
384
+
385
+ return JSON.stringify(BaseRequest)
386
+ }
387
+
388
+ function getPassticket () {
389
+ const accountFactory = WechatyBro.glue.accountFactory
390
+ return accountFactory.getPassticket()
391
+ }
392
+
393
+ function getCheckUploadUrl () {
394
+ const confFactory = WechatyBro.glue.confFactory
395
+ return confFactory.API_checkupload
396
+ }
397
+
398
+ function getUploadMediaUrl () {
399
+ const confFactory = WechatyBro.glue.confFactory
400
+ return confFactory.API_webwxuploadmedia
401
+ }
402
+
403
+ function sendMedia (data) {
404
+ const chatFactory = WechatyBro.glue.chatFactory
405
+ const confFactory = WechatyBro.glue.confFactory
406
+
407
+ if (!chatFactory || !confFactory) {
408
+ log('sendMedia() chatFactory or confFactory not exist.')
409
+ return false
410
+ }
411
+
412
+ try {
413
+ const d = {
414
+ ToUserName: data.ToUserName,
415
+ MediaId: data.MediaId,
416
+ MsgType: data.MsgType,
417
+ FileName: data.FileName,
418
+ FileSize: data.FileSize,
419
+ MMFileExt: data.MMFileExt,
420
+ }
421
+
422
+ if (data.Signature) {
423
+ d.Signature = data.Signature
424
+ }
425
+
426
+ const m = chatFactory.createMessage(d)
427
+
428
+ m.MMFileStatus = confFactory.MM_SEND_FILE_STATUS_SUCCESS
429
+ m.MMStatus = confFactory.MSG_SEND_STATUS_SUCC
430
+ m.sendByLocal = false
431
+
432
+ chatFactory.appendMessage(m)
433
+ chatFactory.sendMessage(m)
434
+ } catch (e) {
435
+ log('sendMedia() exception: ' + e.message)
436
+ return false
437
+ }
438
+ return true
439
+ }
440
+
441
+ function forward (baseData, patchData) {
442
+ const chatFactory = WechatyBro.glue.chatFactory
443
+ const confFactory = WechatyBro.glue.confFactory
444
+
445
+ if (!chatFactory || !confFactory) {
446
+ log('forward() chatFactory or confFactory not exist.')
447
+ return false
448
+ }
449
+
450
+ try {
451
+ let m = chatFactory.createMessage(baseData)
452
+
453
+ // Need to override the parametes after called createMessage()
454
+ m = Object.assign(m, patchData)
455
+
456
+ chatFactory.appendMessage(m)
457
+ chatFactory.sendMessage(m)
458
+ } catch (e) {
459
+ log('forward() exception: ' + e.message)
460
+ return false
461
+ }
462
+ return true
463
+ }
464
+
465
+ function send (ToUserName, Content) {
466
+ const chatFactory = WechatyBro.glue.chatFactory
467
+ const confFactory = WechatyBro.glue.confFactory
468
+
469
+ if (!chatFactory || !confFactory) {
470
+ log('send() chatFactory or confFactory not exist.')
471
+ return false
472
+ }
473
+ try {
474
+ const m = chatFactory.createMessage({
475
+ ToUserName: ToUserName,
476
+ Content: Content,
477
+ MsgType: confFactory.MSGTYPE_TEXT,
478
+ })
479
+ chatFactory.appendMessage(m)
480
+ chatFactory.sendMessage(m)
481
+ } catch (e) {
482
+ log('send() exception: ' + e.message)
483
+ return false
484
+ }
485
+ return true
486
+ }
487
+
488
+ function getMessage (id) {
489
+ const chatFactory = WechatyBro.glue.chatFactory
490
+ if (!chatFactory) {
491
+ log('chatFactory not inited')
492
+ return null
493
+ }
494
+ const msg = chatFactory.getMsg(id)
495
+
496
+ if (!msg) {
497
+ return null
498
+ }
499
+
500
+ const msgWithoutFunction = {}
501
+ Object.keys(msg).forEach(function (k) {
502
+ if (typeof msg[k] !== 'function') {
503
+ msgWithoutFunction[k] = msg[k]
504
+ }
505
+ })
506
+ return msgWithoutFunction
507
+ }
508
+
509
+ function getContact (id) {
510
+ const contactFactory = WechatyBro.glue.contactFactory
511
+ if (!contactFactory) {
512
+ log('contactFactory not inited')
513
+ return null
514
+ }
515
+ let contact = contactFactory.getContact(id)
516
+ let contactWithoutFunction = {}
517
+
518
+ if (contact) {
519
+ if (contact.isContact) {
520
+ // extend rawObj to identify `stranger`
521
+ contact.stranger = !(contact.isContact())
522
+ }
523
+
524
+ Object.keys(contact).forEach(function (k) {
525
+ if (typeof contact[k] !== 'function') {
526
+ contactWithoutFunction[k] = contact[k]
527
+ }
528
+ })
529
+ } else {
530
+ /**
531
+ * when `id` does not exist in _contact Array, maybe it is belongs to a stranger in a room.
532
+ * try to find in room's member list for this `id`, and return the contact info, if any.
533
+ */
534
+ contact = Object.keys(_contacts)
535
+ .filter(id => id.match(/^@@/)) // only search in room
536
+ .map(id => _contacts[id]) // map to room array
537
+ .filter(r => r.MemberList.length) // get rid of room without member list
538
+ .filter(r => r.MemberList
539
+ .filter(m => m.UserName === id)
540
+ .length
541
+ )
542
+ .map(c => c.MemberList
543
+ .filter(m => m.UserName === id)[0]
544
+ )[0]
545
+
546
+ if (contact) {
547
+ contact.stranger = true
548
+
549
+ Object.keys(contact).forEach(k => {
550
+ if (typeof contact[k] !== 'function') {
551
+ contactWithoutFunction[k] = contact[k]
552
+ }
553
+ })
554
+ } else {
555
+ // Not found contact id
556
+ contactWithoutFunction = null
557
+ }
558
+ }
559
+
560
+ return contactWithoutFunction
561
+ }
562
+
563
+ function getUserName () {
564
+ if (!WechatyBro.loginState()) {
565
+ return null
566
+ }
567
+ const accountFactory = WechatyBro.glue.accountFactory
568
+ return accountFactory
569
+ ? accountFactory.getUserName()
570
+ : null
571
+ }
572
+
573
+ function contactList () {
574
+ const contactFactory = WechatyBro.glue.contactFactory
575
+
576
+ return new Promise(resolve => retryFind(0, resolve))
577
+
578
+ // return
579
+
580
+ // retry 3 times, sleep 300ms between each time
581
+ function retryFind (attempt, done) {
582
+ attempt = attempt || 0
583
+
584
+ const contactIdList = contactFactory
585
+ .getAllFriendContact()
586
+ .map(c => c.UserName)
587
+
588
+ if (contactIdList && contactIdList.length) {
589
+ done(contactIdList)
590
+ } else if (attempt > 3) {
591
+ done([])
592
+ } else {
593
+ attempt++
594
+ setTimeout(() => retryFind(attempt, done), 1000)
595
+ }
596
+ }
597
+ }
598
+
599
+ function contactRemark (UserName, remark) {
600
+ if (remark === null || remark === undefined) {
601
+ remark = ''
602
+ }
603
+
604
+ const contact = _contacts[UserName]
605
+ if (!contact) {
606
+ throw new Error('contactRemark() can not found UserName ' + UserName)
607
+ }
608
+
609
+ const accountFactory = WechatyBro.glue.accountFactory
610
+ const confFactory = WechatyBro.glue.confFactory
611
+ const emojiFactory = WechatyBro.glue.emojiFactory
612
+ const mmHttp = WechatyBro.glue.mmHttp
613
+
614
+ return new Promise(resolve => {
615
+ mmHttp({
616
+ method: 'POST',
617
+ url: confFactory.API_webwxoplog,
618
+ data: angular.extend({
619
+ UserName: UserName,
620
+ CmdId: confFactory.oplogCmdId.MODREMARKNAME,
621
+ RemarkName: emojiFactory.formatHTMLToSend(remark),
622
+ }, accountFactory.getBaseRequest()),
623
+ MMRetry: {
624
+ count : 3,
625
+ timeout : 1e4,
626
+ serial : !0,
627
+ },
628
+ })
629
+ .success(() => {
630
+ contact.RemarkName = remark
631
+ return resolve(true)
632
+ })
633
+ .error(() => {
634
+ return resolve(false) // TODO: use reject???
635
+ })
636
+ })
637
+ }
638
+
639
+ // function roomFind(filterFunction) {
640
+ function roomList () {
641
+ const contactFactory = WechatyBro.glue.contactFactory
642
+
643
+ // var match
644
+ // if (!filterFunction) {
645
+ // match = () => true
646
+ // } else {
647
+ // match = eval(filterFunction)
648
+ // }
649
+ // log(match.toString())
650
+ return contactFactory.getAllChatroomContact()
651
+ // .filter(r => match(r.NickName))
652
+ .map(r => r.UserName)
653
+ }
654
+
655
+ function roomDelMember (ChatRoomName, UserName) {
656
+ const chatroomFactory = WechatyBro.glue.chatroomFactory
657
+ return chatroomFactory.delMember(ChatRoomName, UserName)
658
+ }
659
+
660
+ function roomAddMember (ChatRoomName, UserName) {
661
+ const chatroomFactory = WechatyBro.glue.chatroomFactory
662
+ // log(ChatRoomName)
663
+ // log(UserName)
664
+
665
+ return new Promise(resolve => {
666
+ // There's no return value of addMember :(
667
+ // https://github.com/wechaty/webwx-app-tracker/blob/f22cb043ff4201ee841990dbeb59e22643092f92/formatted/webwxApp.js#L2404-L2413
668
+ const timer = setTimeout(() => {
669
+ log('roomAddMember() timeout')
670
+ // TODO change to reject here. (BREAKING CHANGES)
671
+ return resolve(0)
672
+ }, 10 * 1000)
673
+
674
+ chatroomFactory.addMember(ChatRoomName, UserName, function (/* result */) {
675
+ clearTimeout(timer)
676
+ return resolve(1)
677
+ })
678
+ })
679
+ }
680
+
681
+ function roomModTopic (ChatRoomName, topic) {
682
+ const chatroomFactory = WechatyBro.glue.chatroomFactory
683
+ return chatroomFactory.modTopic(ChatRoomName, topic)
684
+ }
685
+
686
+ function roomCreate (UserNameList/* , topic */) {
687
+ const UserNameListArg = UserNameList.map(function (n) { return { UserName: n } })
688
+
689
+ const chatroomFactory = WechatyBro.glue.chatroomFactory
690
+ const state = WechatyBro.glue.state
691
+
692
+ return new Promise(resolve => {
693
+ chatroomFactory.create(UserNameListArg)
694
+ .then(function (r) {
695
+ // eslint-disable-next-line
696
+ if (r.BaseResponse && 0 == r.BaseResponse.Ret || -2013 == r.BaseResponse.Ret) {
697
+ state.go('chat', { userName: r.ChatRoomName }) // BE CAREFUL: key name is userName, not UserName! 20161001
698
+ // if (topic) {
699
+ // setTimeout(_ => roomModTopic(r.ChatRoomName, topic), 3000)
700
+ // }
701
+ if (!r.ChatRoomName) {
702
+ throw new Error('chatroomFactory.create() got empty r.ChatRoomName')
703
+ }
704
+ resolve(r.ChatRoomName)
705
+ } else {
706
+ throw new Error('chatroomFactory.create() error with Ret: '
707
+ + r && r.BaseResponse.Ret
708
+ + 'with ErrMsg: '
709
+ + r && r.BaseResponse.ErrMsg
710
+ )
711
+ }
712
+ })
713
+ .catch(function (e) {
714
+ // TODO change to reject (BREAKIKNG CHANGES)
715
+ resolve(
716
+ JSON.parse(
717
+ JSON.stringify(
718
+ e
719
+ , Object.getOwnPropertyNames(e)
720
+ )
721
+ )
722
+ )
723
+ })
724
+ })
725
+ }
726
+
727
+ function verifyUserRequest (UserName, VerifyContent) {
728
+ VerifyContent = VerifyContent || ''
729
+
730
+ const contactFactory = WechatyBro.glue.contactFactory
731
+ const confFactory = WechatyBro.glue.confFactory
732
+
733
+ const Ticket = '' // what's this?
734
+
735
+ return new Promise(resolve => {
736
+ contactFactory.verifyUser({
737
+ Opcode: confFactory.VERIFYUSER_OPCODE_SENDREQUEST,
738
+ Scene: confFactory.ADDSCENE_PF_WEB,
739
+ UserName,
740
+ Ticket,
741
+ VerifyContent,
742
+ })
743
+ .then(() => { // succ
744
+ // alert('ok')
745
+ // log('friendAdd(' + UserName + ', ' + VerifyContent + ') done')
746
+ return resolve(true)
747
+ })
748
+ .catch((err) => { // fail
749
+ // alert('not ok')
750
+ log('friendAdd(' + UserName + ', ' + VerifyContent + ') fail: ' + err)
751
+ resolve(false)
752
+ })
753
+ })
754
+ }
755
+
756
+ function verifyUserOk (UserName, Ticket) {
757
+ const contactFactory = WechatyBro.glue.contactFactory
758
+ const confFactory = WechatyBro.glue.confFactory
759
+
760
+ return new Promise(resolve => {
761
+ contactFactory.verifyUser({
762
+ UserName: UserName,
763
+ Opcode: confFactory.VERIFYUSER_OPCODE_VERIFYOK,
764
+ Scene: confFactory.ADDSCENE_PF_WEB,
765
+ Ticket: Ticket,
766
+ }).then(() => { // succ
767
+ // alert('ok')
768
+ log('friendVerify(' + UserName + ', ' + Ticket + ') done')
769
+ return resolve(true)
770
+ }).catch(err => { // fail
771
+ // alert('err')
772
+ log('friendVerify(' + UserName + ', ' + Ticket + ') fail' + err)
773
+ return resolve(false)
774
+ })
775
+ })
776
+ }
777
+
778
+ // ///////////////////////////////////////////////////////////////////////////
779
+ // ///////////////////////////////////////////////////////////////////////////
780
+ // ///////////////////////////////////////////////////////////////////////////
781
+
782
+ /*
783
+ * WechatyBro injectio must return this object.
784
+ * PuppetWeChatBridge need this to decide if injection is successful.
785
+ */
786
+ const retObj = {
787
+ code: 200, // 2XX ok, 4XX/5XX error. HTTP like
788
+ message: 'any message',
789
+ }
790
+
791
+ if (typeof this.WechatyBro !== 'undefined') {
792
+ retObj.code = 201
793
+ retObj.message = 'WechatyBro already injected?'
794
+ return retObj
795
+ }
796
+
797
+ const WechatyBro = {
798
+ glue: {
799
+ // will be initialized by glueToAngular() function
800
+ },
801
+
802
+ // glue funcs
803
+ // , getLoginStatusCode: function() { return WechatyBro.glue.loginScope.code }
804
+ // , getLoginQrImgUrl: function() { return WechatyBro.glue.loginScope.qrcodeUrl }
805
+ angularIsReady,
806
+
807
+ // variable
808
+ vars: {
809
+ loginState : false,
810
+ initState : false,
811
+
812
+ scanCode : null,
813
+ heartBeatTimmer : null,
814
+ },
815
+
816
+ // funcs
817
+ ding, // simple return 'dong'
818
+ emit: window.wechatyPuppetBridgeEmit, // send event to PuppetWeChatBridge in Node.js
819
+ init, // initialize WechatyBro @ Browser
820
+ log, // log to Node.js
821
+ logout, // logout current logined user
822
+ send, // send message to wechat user
823
+
824
+ getContact,
825
+ getMessage,
826
+ getUserName,
827
+ getMsgImg,
828
+ getMsgEmoticon,
829
+ getMsgVideo,
830
+ getMsgVoice,
831
+ getMsgPublicLinkImg,
832
+ getBaseRequest,
833
+ getPassticket,
834
+ getUploadMediaUrl,
835
+ sendMedia,
836
+ forward,
837
+ getCheckUploadUrl,
838
+
839
+ // for Wechaty Contact Class
840
+ contactList,
841
+ contactRemark,
842
+
843
+ // for Wechaty Room Class
844
+ roomCreate,
845
+ roomAddMember,
846
+ roomList,
847
+ roomDelMember,
848
+ roomModTopic,
849
+
850
+ // for Friend Request
851
+ verifyUserRequest,
852
+ verifyUserOk,
853
+
854
+ // test purpose
855
+ isLogin: () => {
856
+ log('isLogin() DEPRECATED. use loginState() instead')
857
+ return loginState()
858
+ },
859
+ loginState,
860
+ }
861
+
862
+ this.WechatyBro = WechatyBro
863
+
864
+ retObj.code = 200
865
+ retObj.message = 'WechatyBro Inject Done'
866
+
867
+ return retObj
868
+ }.apply(window))