电报机器人不断回复而不是执行以下代码(使用webhook)

时间:2016-06-24 13:47:57

标签: http google-app-engine google-cloud-datastore app-engine-ndb webhooks

我正在尝试为GAE上的多人游戏托管我的电报机器人,这里使用了网络钩子。这就是我的数据库部分:

        if text.startswith('/'):
        if text == '/start':
            reply('Bot enabled')
            setEnabled(chat_id, True)
        elif text == '/stop':
            reply('Bot disabled')
            setEnabled(chat_id, False)
        elif text == '/newgame':
            if chat_type == 'group':
                existing_game = Game.query (Game.chat_id == chat_id).get()
                if existing_game:
                    reply ("game is alr intitiated liao")
                else:
                    ##create a new game here 
                    #still stuck here 
                    ##========================##
                    #reply("keep replying this line")
                    ##========================##
                    new_game = Game (
                        chat_id = chat_id,
                        id = chat_id
                        )    
                    curr_game_key = new_game.put()

                    new_player = Player (
                        parent = curr_game_key,
                        user_id = fr_user_id,
                        id = fr_user_id)
                    new_player.put()
                    reply("waiting for more friends to join")
            else:
                reply('game must be conducted within a group chat! jio more friends!')
        else:
            reply('What command?')
    else:
        if getEnabled(chat_id):
            reply('I got your message! (but I do not know how to answer)')
        else:
            logging.info('not enabled for chat_id {}'.format(chat_id))

Web钩子处理程序下的代码部分:

#reply("keep replying this line")

问题在于,当我发送' / newgame'在群聊中,没有任何东西发回给我。如果我取消注释以下行,我的机器人不断发送给我"继续回复此行"喜欢疯狂。:

def reply(msg=None, img=None):
        if msg:
            resp = urllib2.urlopen(BASE_URL + 'sendMessage', urllib.urlencode({
                'chat_id': str(chat_id),
                'text': msg.encode('utf-8'),
                'disable_web_page_preview': 'true',
                'reply_to_message_id': str(message_id),
            })).read()
        elif img:
            resp = multipart.post_multipart(BASE_URL + 'sendPhoto', [
                ('chat_id', str(chat_id)),
                ('reply_to_message_id', str(message_id)),
            ], [
                ('photo', 'image.jpg', img),
            ])
        else:
            logging.error('no msg or img specified')
            resp = None

        logging.info('send response:')
        logging.info(resp)

回复功能:

Internal Server Error
The server has either erred or is incapable of performing the requested operation.

Traceback (most recent call last):
  File "/base/data/home/runtimes/python27/python27_lib/versions/third_party/webapp2-2.5.2/webapp2.py", line 1535, in __call__
    rv = self.handle_exception(request, response, e)
  File "/base/data/home/runtimes/python27/python27_lib/versions/third_party/webapp2-2.5.2/webapp2.py", line 1529, in __call__
    rv = self.router.dispatch(request, response)
  File "/base/data/home/runtimes/python27/python27_lib/versions/third_party/webapp2-2.5.2/webapp2.py", line 1278, in default_dispatcher
    return route.handler_adapter(request, response)
  File "/base/data/home/runtimes/python27/python27_lib/versions/third_party/webapp2-2.5.2/webapp2.py", line 1102, in __call__
    return handler.dispatch()
  File "/base/data/home/runtimes/python27/python27_lib/versions/third_party/webapp2-2.5.2/webapp2.py", line 572, in dispatch
    return self.handle_exception(e, self.app.debug)
  File "/base/data/home/runtimes/python27/python27_lib/versions/third_party/webapp2-2.5.2/webapp2.py", line 570, in dispatch
    return method(*args, **kwargs)
  File "/base/data/home/apps/s~orbitaltest2/1.393734187774164753/main.py", line 66, in get
    self.response.write(json.dumps(json.load(urllib2.urlopen(BASE_URL + 'getUpdates'))))
  File "/base/data/home/runtimes/python27/python27_dist/lib/python2.7/urllib2.py", line 127, in urlopen
    return _opener.open(url, data, timeout)
  File "/base/data/home/runtimes/python27/python27_dist/lib/python2.7/urllib2.py", line 410, in open
    response = meth(req, response)
  File "/base/data/home/runtimes/python27/python27_dist/lib/python2.7/urllib2.py", line 523, in http_response
    'http', request, response, code, msg, hdrs)
  File "/base/data/home/runtimes/python27/python27_dist/lib/python2.7/urllib2.py", line 448, in error
    return self._call_chain(*args)
  File "/base/data/home/runtimes/python27/python27_dist/lib/python2.7/urllib2.py", line 382, in _call_chain
    result = func(*args)
  File "/base/data/home/runtimes/python27/python27_dist/lib/python2.7/urllib2.py", line 531, in http_error_default
    raise HTTPError(req.get_full_url(), code, msg, hdrs, fp)
HTTPError: HTTP Error 409: Conflict

错误:

class GetUpdatesHandler(webapp2.RequestHandler):
    def get(self):
        urlfetch.set_default_fetch_deadline(60)
        self.response.write(json.dumps(json.load(urllib2.urlopen(BASE_URL + 'getUpdates'))))

第66行所属的处理程序的代码:

BitmapSource bmp = BitmapSource.Create(imageStrideInPixels, height, 96, 96, PixelFormats.Bgra32, null, imageData, imageStrideInPixels * pixelWidth);  

完全是新手,任何建议都表示赞赏!

1 个答案:

答案 0 :(得分:0)

您应该检查一下状态代码&您的webhook返回了什么内容。

您有两种选择如何回复使用:

  1. 致电Telegram API
  2. 返回JSON作为对webhook调用的响应
  3. 由于您没有提供reply("keep replying this line")的源代码,因此很难说出究竟是什么问题。

    无论如何,您的webhook应该返回HTTP状态代码200.如果它没有Telegram将其视为内部错误并且正在尝试向您重新发送消息。这就是为什么你会反复拨打电话而且它会像疯了一样回复#34;

    最有可能的是,呼叫try/except成功,但出现了错误,电报错误回复。

    添加A1块并记录例外。

    检查您的日志&如果需要,请添加其他日志例如,我记录了来自webhook的HTTP响应内容。它会有所帮助。