0

我是 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, 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()

有谁知道如何获取数据?

4

1 回答 1

0

我最近发现了如何从 supabase 获取数据,这样做你必须尝试这样的事情:

data = supabase.table('users').select('id').eq('id', un).execute()

或者

data = supabase.table('users').select('stuff').execute()

本质上这是说我希望它选择名为 users 的表,然后我想选择所有等于名为 un 的对象的 id

然后,一旦您想查看该数据,就可以使用以下内容:

data.get('data')

这将返回与 un 值匹配的所有内容:

[{'id': 'NotNazuh'}]
于 2021-12-13T01:17:11.227 回答