Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Using wechaty to start a wechatOA account #1016

Closed
lijiarui opened this issue Dec 17, 2017 · 5 comments
Closed

Using wechaty to start a wechatOA account #1016

lijiarui opened this issue Dec 17, 2017 · 5 comments

Comments

@lijiarui
Copy link
Member

lijiarui commented Dec 17, 2017

Wechaty act as a chatbot connector to connect wechat, so I suggest to add an ability to connect wechat OA account

Reference: Wechat Module is a middleware and SDK of Wechat Official Account Admin Platform.

I suggest starting wechatOA account as follows

Wechaty.instance({
  type: {
    'official':
      {
        appID: '',
        appSecret: '',
        appToken: '',
        encodingAESKey: 'encodinAESKey',
        checkSignature: true  // option
      }
    }
}) // Singleton
.on('message',  message => console.log(`Message: ${message}`))
.init()

The following is using wechat module to receive and send text, audio and image message, see my test-repo

const express = require('express')
const wechat = require('wechat')
const app = express()
const config = {
	token: 'wechat', 
	appid: 'wx4369a7d8a2331169', 
	encodingAESKey: 'H1RdC0azpPwWEohbocVF9MJAkqXIOnDgG0RGD02seAU', 
	checkSignature: true // Option
}

app.use(express.query())
app.use('/', wechat(config, function (req, res, next) {
	console.log(req.weixin)

	if (req.weixin.Content === 'text') {
		res.reply({
			content: '你好,Hello World!',
			type: 'text'
		})
	}

	if (req.weixin.Content === 'voice') {
		res.reply({
			content: { mediaId: '9seTBfhw1KmTiaORRo4dPXwULfy4DQnyjcnPPJjjT9gwoqU2WXRTEqfwxV2pRQbZ' },
			type: 'voice'
		})
	}

	if (req.weixin.Content === 'image') {
		res.reply({
			content: { mediaId: '73YlTk1qniHG_EzB4PzxKG8_bYbJy_AW01gr0R8yR-aat7N_BcaFY9Ei_GaXyZl7' },
			type: 'image'
		})
	}
}))

const port = 80
app.listen(port)
console.log(`Server listening at http://127.0.0.1:${port}`)

Log as follows

Text

{
    "ToUserName": "gh_87e03c422b73", 
    "FromUserName": "o04vx0YXJGTnPdiXLSu1xZW2Ajuo", 
    "CreateTime": "1513529032", 
    "MsgType": "text", 
    "Content": "你好", 
    "MsgId": "6500557694439254915"
}

Audio

{
    "ToUserName": "gh_87e03c422b73", 
    "FromUserName": "o04vx0YXJGTnPdiXLSu1xZW2Ajuo", 
    "CreateTime": "1513529079", 
    "MsgType": "voice", 
    "MediaId": "9seTBfhw1KmTiaORRo4dPXwULfy4DQnyjcnPPJjjT9gwoqU2WXRTEqfwxV2pRQbZ", 
    "Format": "amr", 
    "MsgId": "6500557895850000384", 
    "Recognition": "你好哈喽!"
}

Image

{
    "ToUserName": "gh_87e03c422b73", 
    "FromUserName": "o04vx0YXJGTnPdiXLSu1xZW2Ajuo", 
    "CreateTime": "1513529103", 
    "MsgType": "image", 
    "PicUrl": "http://mmbiz.qpic.cn/mmbiz_jpg/4uygLEdrlQoH20XAjxMptmZHawb1wspbyE4K07H4zRzvpcib72GYcfdTGxzlpKNbkoyBtltAyJPSoSFNaAOJ6ZQ/0", 
    "MsgId": "6500557999381932938", 
    "MediaId": "73YlTk1qniHG_EzB4PzxKG8_bYbJy_AW01gr0R8yR-aat7N_BcaFY9Ei_GaXyZl7"
}

Result
image

@lijiarui
Copy link
Member Author

lijiarui commented Dec 17, 2017

About How to download the attachment, as doc shows, there are two funtions

http请求方式: GET,https调用
https://api.weixin.qq.com/cgi-bin/media/get?access\_token=ACCESS\_TOKEN&media\_id=MEDIA\_ID
请求示例(示例为通过curl命令获取多媒体文件)
curl -I -G "https://api.weixin.qq.com/cgi-bin/media/get?access\_token=ACCESS\_TOKEN&media\_id=MEDIA\_ID"

Return Image data

200	OK
Cache-Control: no-cache, must-revalidate
Connection: close
Date: Sun, 17 Dec 2017 17:23:01 GMT
Content-Type: image/jpeg
Content-Length: 91293
Content-disposition: attachment; filename="73YlTk1qniHG_EzB4PzxKG8_bYbJy_AW01gr0R8yR-aat7N_BcaFY9Ei_GaXyZl7.jpg"

Example:

http://file.api.weixin.qq.com/cgi-bin/media/get?access_token=4_DlaLwqEXEFS51kAOSeiQcO-RanXVAYxaJ8l-RNV-tixju1TXkjfA5lvTmXTjMfnWjJWoaC7RyNwriH8GxlN2Wk28KwigRc0AZMKenr6XqDitSgyrILCvAtPHSsuNUsA2cWKGNvQf1oVzje5CQEDhAHALZY&media_id=73YlTk1qniHG_EzB4PzxKG8_bYbJy_AW01gr0R8yR-aat7N_BcaFY9Ei_GaXyZl7

@huan
Copy link
Member

huan commented Dec 18, 2017

Thank you for the proposal.

To integrate the Official Account of Wechat to Wechaty will enable our developers to deal with all kinds of wechat message inside Wechaty.

There still have some design patterns need to be decided, like:

  1. Should OA be a new Puppet?
  2. Should Wechaty start two Puppets(Personal & Official) at the same time, or we should start two Wechaty instances?
  3. etc.

@lijiarui
Copy link
Member Author

lijiarui commented Dec 18, 2017

I have no idea about whether it should be a new puppet, but maybe it should start two puppets at the same time if wechaty want to act as a connector to deal with all message logic.

Then, maybe it should look like this:

const  officalAccount = { 
  type: {
    official:
      {
        appID: '',
        appSecret: '',
        appToken: '',
        encodingAESKey: 'encodinAESKey',
        checkSignature: true 
      }
    }
}
const personalAccount = {
  type: 'personal'
}

Wechaty.instance([officalAccount, personalAccount]) 
.on('message',  message => console.log(`Message: ${message}`))
.init()

@dcsan
Copy link

dcsan commented Dec 18, 2017

It would be interesting if wechaty could be used for OAs too.
but it should probably have some higher level features for managing conversations with async features. I think MS bot framework has a few different formats for dialogs
https://docs.botframework.com/en-us/node/builder/chat-reference/classes/_botbuilder_d_.waterfalldialog.html
also botkit
https://github.com/howdyai/botkit/blob/master/docs/readme.md#start-a-conversation

botkit also provides middleware to normalize messages from multiple chat platforms, this maybe a good reference if we are trying to handle multiple inputs with one core code base:
https://github.com/howdyai/botkit/blob/master/docs/readme-pipeline.md

@huan
Copy link
Member

huan commented Aug 15, 2020

Implemented via wechaty/puppet-official-account#4

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants