如何调用异步函数?

问题描述

我是 Python 新手,为了了解这种语言,我决定创建 Telegram BOT。我在尝试与 supabase、idk 连接时遇到问题,如果问题是语法或者我忘记了什么。

我需要使用带有此代码RPC(存储过程)获取一个随机文档,但我收到一个错误

/Users/alvarogoederivera/web/python_bot/lib/python3.8/site-packages/telegram/ext/dispatcher.py:555: RuntimeWarning: coroutine 'random_quote' was never awaited
  handler.handle_update(update,self,check,context)
RuntimeWarning: Enable tracemalloc to get the object allocation traceback
import logging
import asyncio
import os
import telegram
from telegram.ext import Updater,CommandHandler,MessageHandler,Filters
from supabase_py import create_client,Client


from dotenv import load_dotenv
load_dotenv()

url: str = os.getenv("SUPABASE_URL")
key: str = os.getenv("SUPABASE_KEY")
token: str = os.getenv("TELEGRAM_TOKEN")

supabase: Client = create_client(url,key)

#Conf Logging
logging.basicConfig(
  level=logging.INFO,format='%(asctime)s - %(name)s - %(levelname)s - %(message)s'
)

logger = logging.getLogger()

def start(update):
  logger.info(f"El usuario {update.effective_user['username']},ha iniciado")
  name = update.effective_user['first_name']
  update.message.reply_text(f"Hola {name} yo soy tu bot")

async def random_quote(update,context):
  quote = await supabase.rpc('get_random_quote',{})
  print(quote)
  # user_id = update.effective_user['id']
  # logger.info(f"el usuario {user_id} ha solicitado una frase")
  # context.bot.sendMessage(chat_id = user_id,parse_mode="MarkdownV2",text=f"_Frase_ *x*")

def echo(update,context):
  user_id = update.effective_user['id']
  logger.info(f"el usuario {user_id} ha enviado un mensaje")
  text = update.message.text
  context.bot.sendMessage(chat_id = user_id,text=f"_escribiste_ *{text}*")

if __name__ == "__main__":
  bot = telegram.Bot(token = token)

updater = Updater(bot.token)

dp = updater.dispatcher

loop = asyncio.get_event_loop()
loop.run_until_complete(random_quote({},{}))

dp.add_handler(CommandHandler("start",start))
dp.add_handler(CommandHandler("random",random_quote))
dp.add_handler(MessageHandler(Filters.text,echo))

updater.start_polling()

print("BOT LOAD")

updater.idle()

有人知道如何获取数据吗?

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)