I have an inline bot that gets a string from the user(inline mode) and sends back some data. This is my code:
# -*- 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)
In above code when i send to bot a word (like پروانه
) I get this error:
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:
when i change the line bot.answerInlineQuery(query_id, articles, cache_time=5)
to bot.answerInlineQuery(query_id, articles[:-4], cache_time=5)
this problem does not appear and the bot sends back data. when i use bot.answerInlineQuery(query_id, articles[:-3], cache_time=5)
I get the error again. and when i use bot.answerInlineQuery(query_id, articles[6], cache_time=5)
(means exactly the new item of the articles
) the exception does not raises. means probably this newly added item does not have any problem. where is wrong? is there any timeout? or is there any limit on the total articles
object? all message_text
in the items of articles
array are less than 4096 characters. I think this a urllib3
limit because when I change my code and try to send articles
with 1000 items with only one character as text, I get this error again.