我有一个内联机器人,它从用户那里获取一个字符串(内联模式)并发回一些数据。这是我的代码:
# -*- coding: utf-8 -*-
import sys
import time
import telepot
from telepot.loop import MessageLoop
from telepot.namedtuple import *
from pprint import pprint
from elasticsearch import Elasticsearch
from mongoengine import connect
from model import *
from setting import *
bot = telepot.Bot(TOKEN)
es = Elasticsearch()
connect('poem')
content_type, chat_type, chat_id = None, None, None
def handle(msg):
global content_type, chat_type, chat_id
content_type, chat_type, chat_id = telepot.glance(msg)
pprint(msg)
def on_inline_query(msg):
query_id, from_id, query_string = telepot.glance(msg, flavor='inline_query')
print ('Inline Query:', msg)
response = es.search(
index="poem",
body={
"query": {
"match": {"text": query_string},
}
}
)
articles = []
for index, hit in enumerate(response['hits']['hits']):
poem = GanjoorPoemModel.objects(id=hit['_source']['poem_id']).first()
header = u"%s\n%s" % (hit['_source']['poet'], hit['_source']['book'])
if len(poem.sub_book):
for sub in poem.sub_book:
header += u"\n%s" % sub
header += u"\n----\n"
link = poem.link
text = header + poem.text
if len(text) > 4096:
temp = poem.text[:(4096-len(header)-len(link)-10)] + "\n" + link
text = header + temp
print "A", str(poem.link)
# text = text.encode('utf-8', 'ignore').decode('utf-8')
iqra = InlineQueryResultArticle(
id=str(index) + "_" + hit['_source']['poem_id'],
title=u"%s-%s" %(hit['_source']['poet'], hit['_source']['book']),
input_message_content=InputTextMessageContent(
message_text=text
),
description=hit['_source']['text'],
thumb_url='https://appreview.ir/wp-content/uploads/com.example.mohammad.books_.png'
)
articles.append(iqra)
bot.answerInlineQuery(query_id, articles, cache_time=5)
def on_chosen_inline_result(msg):
result_id, from_id, query_string = telepot.glance(msg, flavor='chosen_inline_result')
print ('Chosen Inline Result:', result_id, from_id, query_string)
MessageLoop(bot, {'inline_query': on_inline_query,
'chosen_inline_result': on_chosen_inline_result}).run_as_thread()
print ('Listening ...')
# Keep the program running.
while 1:
time.sleep(10)
在上面的代码中,当我向机器人发送一个单词(例如پروانه
)时,我收到了这个错误:
Traceback (most recent call last):
File "/usr/local/lib/python2.7/dist-packages/telepot/loop.py", line 37, in run_forever
self._handle(msg)
File "/usr/local/lib/python2.7/dist-packages/telepot/helper.py", line 1031, in route
return fn(msg, *args, **kwargs)
File "bot.py", line 63, in on_inline_query
bot.answerInlineQuery(query_id, articles, cache_time=5)
File "/usr/local/lib/python2.7/dist-packages/telepot/__init__.py", line 868, in answerInlineQuery
return self._api_request('answerInlineQuery', _rectify(p))
File "/usr/local/lib/python2.7/dist-packages/telepot/__init__.py", line 435, in _api_request
return api.request((self._token, method, params, files), **kwargs)
File "/usr/local/lib/python2.7/dist-packages/telepot/api.py", line 137, in request
return _parse(r)
File "/usr/local/lib/python2.7/dist-packages/telepot/api.py", line 116, in _parse
raise exception.BadHTTPResponse(response.status, text, response)
BadHTTPResponse: Status 413 - First 500 characters are shown below:
当我将行更改bot.answerInlineQuery(query_id, articles, cache_time=5)
为此bot.answerInlineQuery(query_id, articles[:-4], cache_time=5)
问题时,不会出现此问题,并且机器人会发回数据。当我使用时,bot.answerInlineQuery(query_id, articles[:-3], cache_time=5)
我再次收到错误。并且当我使用bot.answerInlineQuery(query_id, articles[6], cache_time=5)
(确切地说是 的新项目articles
)时,不会引发异常。意味着这个新添加的项目可能没有任何问题。哪里错了?有没有超时?还是对总articles
对象有任何限制?数组中的所有message_text
项articles
都小于 4096 个字符。我认为这是一个urllib3
限制,因为当我更改代码并尝试发送articles
1000 个只有一个字符作为文本的项目时,我再次收到此错误。