I want to see some or full example for fastapi method
The examples is too less, it is just an echo example, I want to see the handler to handle async function using fastapi method, I don't want my bot script becoming boilerplate running in 1 file only :D
New error in Heroku environment :^)
Code same as the fastapi example:
@app.post("/callback")
async def handle_callback(request: Request):
signature = request.headers['X-Line-Signature']
# get request body as text
body = await request.body()
body = body.decode()
try:
events = parser.parse(body, signature)
except InvalidSignatureError:
raise HTTPException(status_code=400, detail="Invalid signature")
for e in events:
if not isinstance(e, MessageEvent):
continue
if not isinstance(e.message, TextMessage):
continue
if e.message.text == "/register":
await line_bot_api.reply_message(
e.reply_token,
TextSendMessage(text=e.message.text)
)
return 'OK'
Err:
2022-07-15T16:37:27.617855+00:00 heroku[router]: at=info method=POST path="/callback" host=genshin-oa.herokuapp.com request_id=bdea759f-511f-4761-8363-bfb29692e589 fwd="147.92.150.198" dyno=web.1 connect=0ms service=2ms status=500 bytes=193 protocol=https
2022-07-15T16:37:27.618896+00:00 app[web.1]: 147.92.150.198:0 - "POST /callback HTTP/1.1" 500
2022-07-15T16:37:27.621113+00:00 app[web.1]: [2022-07-15 16:37:27 +0000] [11] [ERROR] Exception in ASGI application
2022-07-15T16:37:27.621114+00:00 app[web.1]: Traceback (most recent call last):
2022-07-15T16:37:27.621115+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.10/site-packages/uvicorn/protocols/http/h11_impl.py", line 403, in run_asgi
2022-07-15T16:37:27.621115+00:00 app[web.1]: result = await app(self.scope, self.receive, self.send)
2022-07-15T16:37:27.621115+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.10/site-packages/uvicorn/middleware/proxy_headers.py", line 78, in __call__
2022-07-15T16:37:27.621115+00:00 app[web.1]: return await self.app(scope, receive, send)
2022-07-15T16:37:27.621116+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.10/site-packages/fastapi/applications.py", line 269, in __call__
2022-07-15T16:37:27.621116+00:00 app[web.1]: await super().__call__(scope, receive, send)
2022-07-15T16:37:27.621116+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.10/site-packages/starlette/applications.py", line 124, in __call__
2022-07-15T16:37:27.621117+00:00 app[web.1]: await self.middleware_stack(scope, receive, send)
2022-07-15T16:37:27.621117+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.10/site-packages/starlette/middleware/errors.py", line 184, in __call__
2022-07-15T16:37:27.621117+00:00 app[web.1]: raise exc
2022-07-15T16:37:27.621118+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.10/site-packages/starlette/middleware/errors.py", line 162, in __call__
2022-07-15T16:37:27.621118+00:00 app[web.1]: await self.app(scope, receive, _send)
2022-07-15T16:37:27.621118+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.10/site-packages/starlette/exceptions.py", line 93, in __call__
2022-07-15T16:37:27.621118+00:00 app[web.1]: raise exc
2022-07-15T16:37:27.621119+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.10/site-packages/starlette/exceptions.py", line 82, in __call__
2022-07-15T16:37:27.621119+00:00 app[web.1]: await self.app(scope, receive, sender)
2022-07-15T16:37:27.621119+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.10/site-packages/fastapi/middleware/asyncexitstack.py", line 21, in __call__
2022-07-15T16:37:27.621120+00:00 app[web.1]: raise e
2022-07-15T16:37:27.621120+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.10/site-packages/fastapi/middleware/asyncexitstack.py", line 18, in __call__
2022-07-15T16:37:27.621120+00:00 app[web.1]: await self.app(scope, receive, send)
2022-07-15T16:37:27.621120+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.10/site-packages/starlette/routing.py", line 670, in __call__
2022-07-15T16:37:27.621121+00:00 app[web.1]: await route.handle(scope, receive, send)
2022-07-15T16:37:27.621121+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.10/site-packages/starlette/routing.py", line 266, in handle
2022-07-15T16:37:27.621121+00:00 app[web.1]: await self.app(scope, receive, send)
2022-07-15T16:37:27.621122+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.10/site-packages/starlette/routing.py", line 65, in app
2022-07-15T16:37:27.621123+00:00 app[web.1]: response = await func(request)
2022-07-15T16:37:27.621123+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.10/site-packages/fastapi/routing.py", line 227, in app
2022-07-15T16:37:27.621123+00:00 app[web.1]: raw_response = await run_endpoint_function(
2022-07-15T16:37:27.621123+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.10/site-packages/fastapi/routing.py", line 160, in run_endpoint_function
2022-07-15T16:37:27.621123+00:00 app[web.1]: return await dependant.call(**values)
2022-07-15T16:37:27.621124+00:00 app[web.1]: File "/app/run.py", line 58, in handle_callback
2022-07-15T16:37:27.621124+00:00 app[web.1]: await line_bot_api.reply_message(
2022-07-15T16:37:27.621124+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.10/site-packages/linebot/async_api.py", line 144, in reply_message
2022-07-15T16:37:27.621124+00:00 app[web.1]: await self._post(
2022-07-15T16:37:27.621125+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.10/site-packages/linebot/async_api.py", line 1930, in _post
2022-07-15T16:37:27.621125+00:00 app[web.1]: response = await self.async_http_client.post(
2022-07-15T16:37:27.621125+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.10/site-packages/linebot/aiohttp_async_http_client.py", line 72, in post
2022-07-15T16:37:27.621125+00:00 app[web.1]: response = await self.session.post(url, headers=headers, data=data, timeout=timeout)
2022-07-15T16:37:27.621125+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.10/site-packages/aiohttp/client.py", line 466, in _request
2022-07-15T16:37:27.621126+00:00 app[web.1]: with timer:
2022-07-15T16:37:27.621126+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.10/site-packages/aiohttp/helpers.py", line 701, in __enter__
2022-07-15T16:37:27.621126+00:00 app[web.1]: raise RuntimeError(
2022-07-15T16:37:27.621126+00:00 app[web.1]: RuntimeError: Timeout context manager should be used inside a task
Please make a documentation clear and complete...
@rushkii You can refer to my sample here. :P
@router.post("/line")
async def callback(request: Request, x_line_signature: str = Header(None)):
body = await request.body()
try:
handler.handle(body.decode("utf-8"), x_line_signature)
except InvalidSignatureError:
raise HTTPException(status_code=400, detail="chatbot handle body error.")
return 'OK'
@handler.add(MessageEvent, message=TextMessage)
def message_text(event):
print("!!!!!!!!!!!!!!!!!!!!!!")
print(event)
print("!!!!!!!!!!!!!!!!!!!!!!")
line_bot_api.reply_message(
event.reply_token,
TextSendMessage(text=event.message.text)
)
Hi @louis70109, but what if I want to call an async function in the message_textfucntion? Because with your provided solution, message_text is a synchronized function and I can't use await or asyncio.run() to run my async function and get back the result.