我有一个内联机器人,它从用户那里获取一个字符串(内联模式)并发回一些数据。 这是我的代码:
# -*- 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)
在上面的代码中,当我向bot发送一个单词(如پروانه
)时,我收到此错误:
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个只有一个字符作为文本的项目时,我再次收到此错误。