如何调用异步函数?

2024-10-03 15:33:56 发布

您现在位置:Python中文网/ 问答频道 /正文

我是新来的inPython,为了学习这种语言,我决定创建电报机器人。如果问题是语法或者我忘记了什么,我在尝试连接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, parse_mode="MarkdownV2", 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()

有人知道如何获取数据吗


Tags: textnameimportidosloggingbotcontext