Skip to content

Instantly share code, notes, and snippets.

@imaegoo
Created March 16, 2022 06:30
Show Gist options
  • Save imaegoo/28984b93bb8547895eed49332e4b6a0a to your computer and use it in GitHub Desktop.
Save imaegoo/28984b93bb8547895eed49332e4b6a0a to your computer and use it in GitHub Desktop.
Quick note from wecom to coding
const { getSignature, decrypt } = require('@wecom/crypto')
const atob = require('atob')
const axios = require('axios')
const moment = require('moment')
require('moment-timezone')
const { XMLParser } = require('fast-xml-parser')
const xmlParser = new XMLParser()
const codingToken = ''
const wecomToken = ''
const wecomEncodingAESKey = ''
async function commit(Content) {
const time = moment().tz('Asia/Shanghai').format('YYYY-MM-DD-HH-mm-ss')
const headers = {
Authorization: 'token ' + codingToken
}
let res = await axios({
url: 'https://imaegoo.coding.net/open-api',
method: 'post',
headers,
data: {
Action: 'DescribeGitCommits',
DepotId: 9441094,
PageNumber: 1,
PageSize: 1,
Ref: 'main'
}
})
const LastCommitSha = res.data.Response.Commits[0].Sha
res = await axios({
url: 'https://imaegoo.coding.net/open-api',
method: 'post',
headers,
data: {
Action: 'CreateGitFiles',
DepotId: 9441094,
LastCommitSha,
Message: time,
Ref: 'main',
GitFiles: [
{
Path: `速记/${time}.md`,
Content
}
]
}
})
console.log(res.data)
return res.data
}
exports.main = async (event, context) => {
console.log('event', event)
console.log('context', context)
const { content, queryStringParameters } = event
if (content) {
return await commit(content)
} else if (queryStringParameters) {
const { msg_signature, timestamp, nonce, echostr } = queryStringParameters
if (!msg_signature || !timestamp || !nonce) return {}
const body = echostr || xmlParser.parse(atob(event.body)).xml.Encrypt
const mySignature = getSignature(wecomToken, timestamp, nonce, body)
console.log('mySignature', mySignature)
if (mySignature !== msg_signature) return {}
const myMessage = decrypt(wecomEncodingAESKey, body).message
console.log('myMessage', myMessage)
const content = xmlParser.parse(myMessage).xml.Content
return await commit(content)
} else {
return {}
}
}
{
"dependencies": {
"@wecom/crypto": "latest",
"atob": "latest",
"axios": "latest",
"fast-xml-parser": "latest",
"moment": "latest",
"moment-timezone": "latest"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment