Skip to content

Commit

Permalink
feat: add mp add draft feature
Browse files Browse the repository at this point in the history
  • Loading branch information
Jeff-Tian committed Oct 2, 2023
1 parent 0a3e385 commit 2f0a625
Show file tree
Hide file tree
Showing 4 changed files with 137 additions and 8 deletions.
3 changes: 2 additions & 1 deletion package.json
Expand Up @@ -21,7 +21,7 @@
"studio:windows": "start https://jqp5j170i6.execute-api.us-east-1.amazonaws.com/dev/gatsby/graphql",
"ci-check": "yarn test&&yarn test:e2e&&yarn build",
"schema": "yarn offline",
"mocha": "mocha"
"mocha": "mocha --timeout 10000"
},
"repository": {
"type": "git",
Expand Down Expand Up @@ -65,6 +65,7 @@
"express": "4.18.2",
"express-graphql": "0.12.0",
"extract-files": "^13.0.0",
"form-data": "^4.0.0",
"fs-extra": "^11.1.1",
"gatsby": "5.11.0",
"gatsby-source-filesystem": "^5.11.0",
Expand Down
75 changes: 68 additions & 7 deletions test/mp.test.js
Expand Up @@ -4,22 +4,83 @@ const nock = require('nock')
const mp = require('../wechat/mp')

describe("mp features", () => {
it('gets access token', async () => {
const expectedResult = {
"access_token": "73_I5O2Ts19YxIGqUekrHhKU4MgUgzCZb6a0VzMFF38_S8SDy5-El7bqDrs-27oNJipeAqar0mJ_McSi3efvXJ0Pr8XPSwpQf6bUROy42MbRVaGEx4ENxAWPh4mmUcYZMgAIAHIV",
"expires_in": 7200
}
const mockTokenRes = {
"access_token": "73_I5O2Ts19YxIGqUekrHhKU4MgUgzCZb6a0VzMFF38_S8SDy5-El7bqDrs-27oNJipeAqar0mJ_McSi3efvXJ0Pr8XPSwpQf6bUROy42MbRVaGEx4ENxAWPh4mmUcYZMgAIAHIV",
"expires_in": 7200
}

it('gets access token', async () => {
nock('https://api.weixin.qq.com').post('/cgi-bin/stable_token', {
grant_type: 'client_credential',
appid: '1234',
secret: '5678',
force_refresh: false
}).reply(200, expectedResult)
}).reply(200, mockTokenRes)

const res = await mp.getAccessToken();

assert.ok(res);
assert.deepEqual(res.data, expectedResult);
assert.deepEqual(res.data, mockTokenRes);
})

it('adds an image', async () => {
nock('https://api.weixin.qq.com').post('/cgi-bin/stable_token', {
grant_type: 'client_credential',
appid: '1234',
secret: '5678',
force_refresh: false
}).reply(200, mockTokenRes)

nock('https://api.weixin.qq.com')
.filteringRequestBody(/.*/, '*')
.post(`/cgi-bin/media/upload?access_token=${mockTokenRes.access_token}&type=image`, '*').reply(200, {
"created_at": 1696231450,
"item": [],
"media_id": "OTC24v0nm33p4vMTERlCO-SXXxAD5Loj15meGTZJmLqpoMJhAXPJ8ebFxGoueWmW",
"type": "image"
})

const res = await mp.addTempImage();
assert.ok(res);
assert.deepEqual(res.data, {
"created_at": 1696231450,
"item": [],
"media_id": "OTC24v0nm33p4vMTERlCO-SXXxAD5Loj15meGTZJmLqpoMJhAXPJ8ebFxGoueWmW",
"type": "image"
});
})

it('adds a draft', async () => {
nock('https://api.weixin.qq.com').post('/cgi-bin/stable_token', {
grant_type: 'client_credential',
appid: '1234',
secret: '5678',
force_refresh: false
}).reply(200, mockTokenRes)

nock('https://api.weixin.qq.com')
.filteringRequestBody(/.*/, '*')
.post(`/cgi-bin/material/add_material?access_token=${mockTokenRes.access_token}&type=image`, '*')
.reply(200, {})

nock('https://api.weixin.qq.com')
.filteringRequestBody(/.*/, '*')
.post(`/cgi-bin/draft/add?access_token=${mockTokenRes.access_token}`)
.reply(200, {
"item": [],
"media_id": "H3tPf5wuG9Gu8tD8v7bpjWB8seLdmWdiF_LpGF0-wQzCU8XBbfiwOYgRxd8qzGqu"
})

const res = await mp.addDraft({
title: 'title',
content: 'content',
need_open_comment: 1
});

assert.ok(res);
assert.deepEqual(res.data, {
"item": [],
"media_id": "H3tPf5wuG9Gu8tD8v7bpjWB8seLdmWdiF_LpGF0-wQzCU8XBbfiwOYgRxd8qzGqu"
});
})
})
67 changes: 67 additions & 0 deletions wechat/mp.js
@@ -1,4 +1,7 @@
const axios = require('axios')
const fs = require('fs')
const path = require('path')
const FormData = require('form-data')

module.exports.getAccessToken = async () => {
return axios.post('https://api.weixin.qq.com/cgi-bin/stable_token', {
Expand All @@ -8,3 +11,67 @@ module.exports.getAccessToken = async () => {
force_refresh: false
})
}

module.exports.addTempImage = async (access_token) => {
if (!access_token) {
const token = await module.exports.getAccessToken();
access_token = token.data.access_token;
}

const formData = new FormData();
formData.append('media', fs.createReadStream(path.join(__dirname, './test.png')));

console.log('uploading with token ', access_token);

return axios.post(
`https://api.weixin.qq.com/cgi-bin/media/upload?access_token=${access_token}&type=image`,
formData,
{
headers: {
...formData.getHeaders()
}
}
)
}

module.exports.addPersistedImage = async (access_token) => {
if (!access_token) {
const token = await module.exports.getAccessToken();
access_token = token.data.access_token;
}

const formData = new FormData();
formData.append('media', fs.createReadStream(path.join(__dirname, './test.png')));

console.log('uploading persisted image with token ', access_token);

return axios.post(
`https://api.weixin.qq.com/cgi-bin/material/add_material?access_token=${access_token}&type=image`,
formData,
{
headers: {
...formData.getHeaders()
}
}
)
}

module.exports.addDraft = async ({ title, content, content_source_url }) => {
const token = await module.exports.getAccessToken();

const mediaRes = await module.exports.addPersistedImage(token.data.access_token);

console.log('mediaRes= ', mediaRes.data.media_id);

return axios.post(`https://api.weixin.qq.com/cgi-bin/draft/add?access_token=${token.data.access_token}`, {
articles: [
{
title,
content,
content_source_url,
need_open_comment: 1,
thumb_media_id: mediaRes.data.media_id
}
]
});
}
Binary file added wechat/test.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 2f0a625

Please sign in to comment.