node-webot / co-wechat-api Goto Github PK
View Code? Open in Web Editor NEWWechat API. Support Async Functions
License: Other
Wechat API. Support Async Functions
License: Other
上传视频素材时会报错:
{"data":"socket hang upPOST https://api.weixin.qq.com/cgi-bin/material/add_material?access_token=16_ronA4R5gRJYkl-OYm3GQEvf1Hv4DXB4ud04EhM7aUu8IWMJOL4IRW78Va7uy_fy8_-odPboLBbQJ-RjH-E5HsAtU_sQiLBVpFpgdyAy1EuUwDKfqQLWxmoKE3_n1qcOwevZPXiAJAKAI&type=video failed."}
将源码中的 stream
改为 data
。
co-wechat-api/lib/api_material.js
Line 84 in 4e13d13
request方法中没有返回res数据,这个很尴尬,在下载素材的时候,需要拿到返回的headers中的数据(里面包含了图片的名称和类型数据)
const user = [
'o3gdewT5m6HzRGZrGr726Y3M20AE',
'o3gdewZPmhIQlCnatI3LI9D55-cc',
'o3gdewYlR32p_zrHH76O7DsJOn7I',
'o3gdewUVPwuExcS9XAjQkOY-8LPU'
]
const userList = await api.batchGetUser(user);
console.log(userList);
// got as below
(node:6042) UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 1): TypeError: api.batchGetUser is not a function
(node:6042) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
使用npm i co-wechat-api时,默认导入的是2.7.2版本
现在最新的版本已经是^3了
最大的差异就在于2版本的是使用的generator,3版本的使用的是async await。
导致在koa中引用的时候一直返回空值。查看源码才发现问题
就算npm默认使用2版本号,希望能至少在文档里提一下注意事项。
另外,谢谢,封装的代码很好用
返回结果:
{"errcode":0,"errmsg":"ok","msgid":271781022721654784}
由于数字太大了,JSON.parse 之后精度就丢失了,变成了 271781022721654800,目前request接口没提供预处理结果的方法,不好自己修正。
请问有公众号发红包的接口么?公众号支付那个接口是不是?
co-wechat-api/lib/api_template.js
Line 107 in 4e13d13
这个请求地址 https://api.weixin.qq.com/cgi-bin/message/wxopen/template/send?access_token=ACCESS_TOKEN
的参数 与微信文档中的参数不一致。
经测试,无法推送模板消息。
参数 | 是否必填 | 说明 |
---|---|---|
touser | 是 | 接收者openid |
template_id | 是 | 模板ID |
url | 否 | 模板跳转链接 |
miniprogram | 否 | 跳小程序所需数据,不需跳小程序可不用传该数据 |
appid | 是 | 所需跳转到的小程序appid(该小程序appid必须与发模板消息的公众号是绑定关联关系) |
pagepath | 是 | 所需跳转到小程序的具体页面路径,支持带参数,(示例index?foo=bar) |
data | 是 | 模板数据 |
注:url和miniprogram都是非必填字段,若都不传则模板无跳转;若都传,会优先跳转至小程序。开发者可根据实际需要选择其中一种跳转方式即可。当用户的微信客户端版本不支持跳小程序时,将会跳转至url。
现在的接口不能实现小程序的配置,并且topColor这个参数也去掉了。
希望可以添加index.d.ts
描述文件支持Typescript的开发。
测试代码:
var filePath = path.resolve('./public/upload');
var filename = path.join(filePath, '1.jpeg');
var type = 'image';
var result = yield api.uploadMaterial(filename, type);
this.body = result;
已创建好api对象,执行时,返回:
Error: socket hang up
at createHangUpError (_http_client.js:203:15)
at TLSSocket.socketOnEnd (_http_client.js:288:23)
at emitNone (events.js:72:20)
at TLSSocket.emit (events.js:166:7)
at endReadableNT (_stream_readable.js:905:12)
at doNTCallback2 (node.js:441:9)
at process._tickCallback (node.js:355:17)
测试环境:
MACOS:10.11.4
node:v4.2.2
换为express+wechat-api则无问题。
as the title said...
when I used the api to get the qrcode, I get stuck at the codes below.
api_common.js
var res = yield httpx.request(url, options);
console.log(res);//couldn't get the res utill time was out
but when I changed the codes, It worked fine..
var res = yield httpx.request("www.baidu.com", options);
console.log(res);//got it!
then I used the "koa-request" instead of "httpx", both of the above worked fine!
So I was wondering if I should count on the version of node?
API: batchGetUsers
环境: node v8.1.3
问题描述:
同样的openid 在使 co + generator 的时候 返回结果正常
修改为 anync + await 之后 返回结果始终为一个 空对象
表示重构失败,完全不知所以然,忘解答
{ WeChatAPIError: JSON.parse error. buffer is {"subscribe":1,"openid":"ogW8rt6i8BmyHaXvleP5SBULysSk","nickname":"Íõ¾ü","sex":1,"language":"zh_CN","city":"y?v\","province":"l?S","country":"","headimgurl":"http://wx.qlogo.cn/mmopen/Q3auHgzwzM4nXzLJEGv1SUt5ibMeibUjmaK45y06UOtaKDc2NgjvjjnsiccgwMPyKOwvstIKA85bM7zziac5m9zmfw/0","subscribe_time":1504246660,"unionid":"oKfK5s5qiqgWMHaeJ6f3sy5TYvx8","remark":"","groupid":0,"tagid_list":[]}
现在很多这种错误,我看了下wechat-api的issues说是解决了,co的这个能解决一下吗?
错误日志如下:
WeChatAPIError: invalid credential, access_token is invalid or not latest hint: [j0563vr23]
at API.request (/xxxxx/node_modules/co-wechat-api/lib/api_common.js:122:17)
at next (native)
at exports.getTicket (/xxxxx/node_modules/co-wechat-api/lib/api_js.js:74:26)
at next (native)
at exports.ensureTicket (/xxxxx/node_modules/co-wechat-api/lib/api_js.js:160:26)
at next (native)
at exports.getJsConfig (/xxxxx/node_modules/co-wechat-api/lib/api_js.js:179:28)
at next (native)
at onFulfilled (/xxxxx/node_modules/co/index.js:65:19)
at process._tickDomainCallback (node.js:409:9)
['image', 'voice', 'video', 'thumb'].forEach(function (type) {
var method = 'upload' + type[0].toUpperCase() + type.substring(1);
exports[method] = function* (filepath) {
return yield this.uploadMedia(filepath, type);
};
});
这里定义了一个exports.uploadImage
被下面这个方法覆盖了
exports.uploadImage = function* (filepath) {
var token = yield this.ensureAccessToken();
var stat = yield fs.stat(filepath);
var form = formstream();
form.file('media', filepath, path.basename(filepath), stat.size);
var url = this.prefix + 'media/uploadimg?access_token=' + token.accessToken;
var opts = {
method: 'POST',
timeout: 60000, // 60秒超时
headers: form.headers(),
data: form
};
opts.headers.Accept = 'application/json';
return yield this.request(url, opts);
};
return this.getAccessToken();
需改为
return await this.getAccessToken();
koa@1 实在是一种误导, 许多项目跟风基于 co 构建.
现在 koa@2 全面切换到 Promise 和 async function 了.
与其把所有方法都写成 generator, 不如全部返回 Promise 适用性更广.
比如用 bluebird 把 wechat-api 包装一下.
缺少用户管理-黑名单管理API
https://github.com/node-webot/co-wechat-api/blob/master/lib/api_js.js#L149
少了yield*,从cache获取ticket一直都是undefined,所以每次都会直接刷微信接口
返回错误异常Unexpected token � in JSON at position 0,其他接口没有这个问题,就这个接口存在异常
nodejs 8.0.0环境下调用getTicket接口回调返回的是空,拿不到从微信获取的数据
/root/aasd/node_modules/co-wechat-api/lib/api_common.js:59
this.getToken = getToken || async function () {
^^^^^^^^
SyntaxError: Unexpected token function
at Object.exports.runInThisContext (vm.js:76:16)
at Module._compile (module.js:542:28)
at Object.Module._extensions..js (module.js:579:10)
at Module.load (module.js:487:32)
at tryModuleLoad (module.js:446:12)
at Function.Module._load (module.js:438:3)
at Module.require (module.js:497:17)
at require (internal/module.js:20:19)
at Object. (/root/51work/node_modules/co-wechat-api/index.js:3:13)
at Module._compile (module.js:570:32)
at Object.Module._extensions..js (module.js:579:10)
at Module.load (module.js:487:32)
at tryModuleLoad (module.js:446:12)
at Function.Module._load (module.js:438:3)
at Module.require (module.js:497:17)
at require (internal/module.js:20:19)
/root/51work/node_modules/co-wechat-api/lib/api_common.js:59
当微信官方文档已更新,但本库未来得及更新,而又想用新的微信 api 时,可调用 patch 方法来扩展新功能。https://github.com/node-webot/wechat-api#patch%E6%89%A9%E5%B1%95
v3.3.0 版本在8.3.0上
希望 能够随手加上
at line 202:
var token = await this.getToken()
=> var token = await this.getToken(this.appid)
这样在一个系统维护多个 api 时,可以知道是哪个 api 的缓存被调用了,否则这个函数的实现必须针对每个 api 都用闭包来包装。
为什么不提供支付相关的接口呢?
调用 uploadMaterial(filepath, 'TYPE) 时,无反应,即无成功result,也无errmsg,程序也无报错。
调用 uploadMedia(filename, TYPE)时, 返回buffer,如下,即非腾讯返回示例里的四种类型。
mediaresult: {"type":"Buffer","data":[123,34,118,111,105,99,101,95,99,111,117,11
0,116,34,58,49,44,34,118,105,100,101,111,95,99,111,117,110,116,34,58,48,44,34,10
5,109,97,103,101,95,99,111,117,110,116,34,58,51,44,34,110,101,119,115,95,99,111,
117,110,116,34,58,50,125]}
WeChatAPIError: article size out of limit hint: [SYxBsA07873949]
co-wechat-api 版本: 2.4.2
httpx 版本: 2.0.0
我使用uploadMedia
时一直都是提示"missing media data hint",看了下 co-wechat-api 的代码,似乎所有的 POST API 都可能有问题?
先看 lib/api_media.js
https://github.com/node-webot/co-wechat-api/blob/master/lib/api_media.js#L32
它给 this.request 传的 opts 中 http method 是用 type
来指定的。
在 api_common.js 中,它原样传给 httpx。
再看 httpx 的 index.js:
https://github.com/JacksonTian/httpx/blob/master/index.js#L29
它接受的是method
属性名,而不是type
。
这个是 bug ?
Thanks
如题。。。
按照官方的api,及co-wechat-api不是也是应该返回json吗? 然后我用JSON.parse来解析 得到的结果是 {"errcode":41005,"errmsg":"media data missing"}
exports.getPageStatistics = async function (options) {
const { accessToken } = await this.ensureAccessToken();
var url = 'https://api.weixin.qq.com/shakearound/statistics/page?access_token=' + accessToken;
return this.request(url, postJSON(options));
};
exports.listBeaconGroup = async function (options) {
const { accessToken } = await this.ensureAccessToken();
var url = 'https://api.weixin.qq.com/shakearound/device/group/getlist?access_token=' + accessToken;
return this.request(url, postJSON(options));
};
exports.queryGroupBeacons = async function (options) {
const { accessToken } = await this.ensureAccessToken();
var url = 'https://api.weixin.qq.com/shakearound/device/group/getdetail?access_token=' + accessToken;
return this.request(url, postJSON(options));
};
exports.addBeaconGroup = async function (options) {
const { accessToken } = await this.ensureAccessToken();
var url = 'https://api.weixin.qq.com/shakearound/device/group/add?access_token=' + accessToken;
return this.request(url, postJSON(options));
};
exports.updateBeaconGroup = async function (options) {
const { accessToken } = await this.ensureAccessToken();
var url = 'https://api.weixin.qq.com/shakearound/device/group/update?access_token=' + accessToken;
return this.request(url, postJSON(options));
};
exports.deleteBeaconGroup = async function (options) {
const { accessToken } = await this.ensureAccessToken();
var url = 'https://api.weixin.qq.com/shakearound/device/group/delete?access_token=' + accessToken;
return this.request(url, postJSON(options));
};
exports.addGroupBeacons = async function (options) {
const { accessToken } = await this.ensureAccessToken();
var url = 'https://api.weixin.qq.com/shakearound/device/group/adddevice?access_token=' + accessToken;
return this.request(url, postJSON(options));
};
exports.deleteGroupBeacons = async function (options) {
const { accessToken } = await this.ensureAccessToken();
var url = 'https://api.weixin.qq.com/shakearound/device/group/deletedevice?access_token=' + accessToken;
return this.request(url, postJSON(options));
};
看了sendTemplate 的api,没有 form_id 的参数呢。不需要么?微信接口里面是必须的参数。
请问下接口中没有authorize吗
大佬们,什么时候提上日程!
sendTemplate 接口调用的 request方法里面抛出了异常,但是在sendTemplate接口中并没有捕获异常,导致外部调用时不能捕获该异常。
请问,您的设计就是不让外部捕获这个异常吗?或者关于捕获这个异常您有什么好办法呢
目前这个库使用generator函数加上convert确实也挺不错。
感觉使用async/await可能更好看点,不知道有往这方面写的打算吗?
如题
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.