littlecodersh / ItChat

A complete and graceful API for Wechat. 微信个人号接口、微信机器人及命令行微信,三十行即可自定义个人号机器人。
http://itchat.readthedocs.io
MIT License
25.52k stars 5.63k forks source link

可能不是issue #786

Open rilyuuj opened 5 years ago

rilyuuj commented 5 years ago

您好,使用您的itchat正在学习python,程序非常棒,调用很多,需要一个个去尝试,按照文档注册了一个真的isAt的tuling机器人反馈,但是经常会收到UnboundLocalError的报错,不是很明白是哪里有问题导致,忘能指导一下,谢谢

以下是demo.py

@itchat.msg_register
def tuling_reply(msg):
    defaultReply = 'I received: ' + msg['Text']
    reply = get_response(msg['Text'])
    return reply or defaultReply

@itchat.msg_register(TEXT, isGroupChat=True)
def groupchat_reply(msg):
    if msg['isAt']:
        defaultReply = 'I received: ' + msg['Text']
        reply = get_response(msg['Text'])
    return reply or defaultReply

以下是错误提示的标准输出

Traceback (most recent call last):
  File "/usr/lib/python2.7/site-packages/itchat/components/register.py", line 66, in configured_reply
    r = replyFn(msg)
  File "demo.py", line 36, in groupchat_reply
    return reply or defaultReply
UnboundLocalError: local variable 'reply' referenced before assignment
hufuyu commented 5 years ago

把最后一行return 前加一个tab和reply= 行对齐

weathon commented 5 years ago

@rilyuuj