littlecodersh / itchatmp

A complete and graceful API for wechat mp. 完备优雅的微信公众号接口,原生支持同步、协程使用。

Home Page:http://itchatmp.readthedocs.io

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

无法发送文字回复?

huming2207 opened this issue · comments

如题,刚试了一下您的示例代码,确认APPID等无误后,在return前加了一行“print(msg[Content])”,然后我试图给该公众号发消息,服务端就报异常,内容如下

比如我下方输入“Ymml”,可见第三行服务端确实已经收到了数据。但是无法实现自动回复。

root@vultr:~/WeChatAtis# python3 wechat_bot.py
itchatmp started! press Ctrl+C to exit.
Ymml
Uncaught exception POST /?signature=xyz&timestamp=1492143218&nonce=153459053&openid=xyzxBFrI&encrypt_type=aes&msg_signature=12xyzc6c95f0a3da479dd (103.7.30.69)
HTTPServerRequest(protocol='http', host='45.63.xyz.xyz', method='POST', uri='/?signature=xyz&timestamp=1492143218&nonce=153459053&openid=xyzxBFrI&encrypt_type=aes&msg_signature=12xyzc6c95f0a3da479dd', version='HTTP/1.1', remote_ip='103.7.30.69', headers={'Accept': '*/*', 'Content-Type': 'text/xml', 'User-Agent': 'Mozilla/4.0', 'Content-Length': '767', 'Pragma': 'no-cache', 'Host': '45.63.xyz.xyz'})
Traceback (most recent call last):
  File "/usr/local/lib/python3.4/dist-packages/itchatmp/views/msgconstruct.py", line 89, in _render
    return template.format(**replyDict)
KeyError: 'Content'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/usr/local/lib/python3.4/dist-packages/tornado/web.py", line 1469, in _execute
    result = yield result
  File "/usr/local/lib/python3.4/dist-packages/tornado/gen.py", line 1015, in run
    value = future.result()
  File "/usr/local/lib/python3.4/dist-packages/tornado/concurrent.py", line 237, in result
    raise_exc_info(self._exc_info)
  File "<string>", line 3, in raise_exc_info
  File "/usr/local/lib/python3.4/dist-packages/tornado/gen.py", line 1021, in run
    yielded = self.gen.throw(*exc_info)
  File "/usr/local/lib/python3.4/dist-packages/itchatmp/components/register.py", line 231, in post
    r, rawReply = yield threadPool.submit(post_fn, self)
  File "/usr/local/lib/python3.4/dist-packages/tornado/gen.py", line 1015, in run
    value = future.result()
  File "/usr/lib/python3.4/concurrent/futures/_base.py", line 395, in result
    return self.__get_result()
  File "/usr/lib/python3.4/concurrent/futures/_base.py", line 354, in __get_result
    raise self._exception
  File "/usr/lib/python3.4/concurrent/futures/thread.py", line 54, in run
    result = self.fn(*self.args, **self.kwargs)
  File "/usr/local/lib/python3.4/dist-packages/itchatmp/components/register.py", line 61, in sync_post_fn
    return verify_reply(core, tns, reply, msgDict, isActualEncrypt)
  File "/usr/local/lib/python3.4/dist-packages/itchatmp/components/register.py", line 174, in verify_reply
    return construct_msg(reply), reply
  File "/usr/local/lib/python3.4/dist-packages/itchatmp/views/msgconstruct.py", line 117, in construct_msg
    return _render(get_template(replyDict['MsgType']), replyDict)
  File "/usr/local/lib/python3.4/dist-packages/itchatmp/views/msgconstruct.py", line 91, in _render
    logger.debug('Missing message element "%s"' % e.message)
AttributeError: 'KeyError' object has no attribute 'message'
500 POST /?signature=xyz&timestamp=1492143218&nonce=153459053&openid=xyzxBFrI&encrypt_type=aes&msg_signature=12xyzc6c95f0a3da479dd (103.7.30.69) 21.84ms
Exception in callback functools.partial(<function wrap.<locals>.null_wrapper at 0x7fdcf0c61598>)
Traceback (most recent call last):
  File "/usr/local/lib/python3.4/dist-packages/tornado/ioloop.py", line 604, in _run_callback
    ret = callback()
  File "/usr/local/lib/python3.4/dist-packages/tornado/stack_context.py", line 275, in null_wrapper
    return fn(*args, **kwargs)
  File "/usr/local/lib/python3.4/dist-packages/itchatmp/components/register.py", line 227, in time_out_callback
    self.finish()
  File "/usr/local/lib/python3.4/dist-packages/tornado/web.py", line 923, in finish
    raise RuntimeError("finish() called twice")
RuntimeError: finish() called twice

@littlecodersh 实测发图片也不行,异常信息如下:

root@vultr:~/WeChatAtis# python3 wechat_bot.py
itchatmp started! press Ctrl+C to exit.
Hhhhh
Access token updated
Uncaught exception POST /?signature=4beed04xyzxyzxyzcd8f5e&timestamp=1492212054&nonce=1433764523&openid=oLRnxyzxyzxyzxdvasoB0yhxBFrI&encrypt_type=aes&msg_signature=6e21032d6a80bd5760073a1fbc284a632be8eabd (103.7.30.69)
HTTPServerRequest(protocol='http', host='45.63.xyz.xyz', method='POST', uri='/?signature=4beed04xyzxyzxyzcd8f5e&timestamp=1492212054&nonce=1433764523&openid=oLRnxyzxyzxyzxdvasoB0yhxBFrI&encrypt_type=aes&msg_signature=6e21032d6a80bd5760073a1fbc284a632be8eabd', version='HTTP/1.1', remote_ip='103.7.30.69', headers={'Pragma': 'no-cache', 'Host': '45.63.xyz.xyz', 'User-Agent': 'Mozilla/4.0', 'Accept': '*/*', 'Content-Length': '768', 'Content-Type': 'text/xml'})
Traceback (most recent call last):
  File "/usr/local/lib/python3.4/dist-packages/itchatmp/views/msgconstruct.py", line 89, in _render
    return template.format(**replyDict)
KeyError: 'Content'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/usr/local/lib/python3.4/dist-packages/tornado/web.py", line 1469, in _execute
    result = yield result
  File "/usr/local/lib/python3.4/dist-packages/tornado/gen.py", line 1015, in run
    value = future.result()
  File "/usr/local/lib/python3.4/dist-packages/tornado/concurrent.py", line 237, in result
    raise_exc_info(self._exc_info)
  File "<string>", line 3, in raise_exc_info
  File "/usr/local/lib/python3.4/dist-packages/tornado/gen.py", line 1021, in run
    yielded = self.gen.throw(*exc_info)
  File "/usr/local/lib/python3.4/dist-packages/itchatmp/components/register.py", line 231, in post
    r, rawReply = yield threadPool.submit(post_fn, self)
  File "/usr/local/lib/python3.4/dist-packages/tornado/gen.py", line 1015, in run
    value = future.result()
  File "/usr/lib/python3.4/concurrent/futures/_base.py", line 395, in result
    return self.__get_result()
  File "/usr/lib/python3.4/concurrent/futures/_base.py", line 354, in __get_result
    raise self._exception
  File "/usr/lib/python3.4/concurrent/futures/thread.py", line 54, in run
    result = self.fn(*self.args, **self.kwargs)
  File "/usr/local/lib/python3.4/dist-packages/itchatmp/components/register.py", line 61, in sync_post_fn
    return verify_reply(core, tns, reply, msgDict, isActualEncrypt)
  File "/usr/local/lib/python3.4/dist-packages/itchatmp/components/register.py", line 172, in verify_reply
    [core.config, reply])), reply
  File "/usr/local/lib/python3.4/dist-packages/itchatmp/views/crypto.py", line 65, in encrypt_msg
    text = construct_msg(replyDict).encode('utf8')
  File "/usr/local/lib/python3.4/dist-packages/itchatmp/views/msgconstruct.py", line 117, in construct_msg
    return _render(get_template(replyDict['MsgType']), replyDict)
  File "/usr/local/lib/python3.4/dist-packages/itchatmp/views/msgconstruct.py", line 91, in _render
    logger.debug('Missing message element "%s"' % e.message)
AttributeError: 'KeyError' object has no attribute 'message'

代码如下:

import itchatmp
import wechat_bot_account
import naips_bot, naips_account

itchatmp.update_config(itchatmp.WechatConfig(
    token = wechat_bot_account.wechat_token,
    appId = wechat_bot_account.wechat_mp_appid,
    appSecret = wechat_bot_account.wechat_mp_appkey,
    encryptMode = itchatmp.content.SAFE,
    encodingAesKey = wechat_bot_account.wechat_mp_aeskey))


@itchatmp.msg_register(itchatmp.content.TEXT)
def text_reply(msg):
    msg_str = msg['Content']
    print(msg_str)
    itchatmp.send('@img@test.jpg', msg['FromUserName'])

我也遇到一样的问题

msg['Content']也找不到元素,妈的

@huming2207 @saonian @tetsaicn 更新到0.0.17试试

@littlecodersh 实测能用了!谢谢!

见图:

wechatimg7

项目见:https://github.com/huming2207/WeChatAtis

这是个用Beautifulsoup抓澳洲民航部门气象信息然后用微信公号查询的bot,刚随便弄了个demo试试能不能用,所以代码很乱很丑,过会儿改掉。