-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbot.py
88 lines (62 loc) · 2.04 KB
/
bot.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
import telebot
import re
from collections import defaultdict
from supabase import create_client
import sys
from dotenv import load_dotenv
import os
from scrapper import processList, generate_file_td
load_dotenv()
API_TOKEN = os.getenv("TELEGRAM_TOKEN")
bot = telebot.TeleBot(API_TOKEN)
url_s = os.getenv("URL_SUPA")
key_s = os.getenv("KEY_SUPA")
try:
supabase = create_client(url_s, key_s)
except Exception:
print("Erro ao conectar ao banco de dados")
sys.exit(1)
print("Iniciando bot")
# TODO: Adicionar coisas de tratamento de erro
def addLink(user, link):
supabase.table("links").insert({"url": link, "user_id": user}).execute()
def getLinks(user):
r = supabase.table("links").select("*").eq("user_id", user).execute()
# exclui
# supabase.table("links").delete().eq("user_id", user).execute()
return r.data
@bot.message_handler(commands=["ola", "start"])
def send_welcome(message):
bot.reply_to(
message,
"Mande links que serão guardados e use o comanod /links ou /l para retornar todos eles(serão apagados depois)",
)
@bot.message_handler(commands=["links", "l"])
def a(m):
r = getLinks(m.chat.id)
urls = [i["url"] for i in r]
if len(urls) == 0:
bot.send_message(m.chat.id, "Sem links guardados")
return
else:
bot.send_message(m.chat.id, "Links selecionados:")
for i in urls:
bot.send_message(m.chat.id, i)
temp_file = generate_file_td() + "_" + str(m.chat.id) + ".md"
print(f"Arquivo {temp_file} criado")
processList(urls, temp_file)
try:
with open(temp_file, "r") as f:
bot.send_document(m.chat.id, f)
os.remove(temp_file)
except Exception:
print(f"Erro ao remover {temp_file}")
link_r = r"https?:\/\/[^\s/$.?#].[^\s]*"
@bot.message_handler(func=lambda m: True)
def link_add(m):
if re.search(link_r, m.text):
addLink(m.chat.id, m.text)
bot.reply_to(m, "Link adicionado")
else:
bot.reply_to(m, "Não foi reconhecido como um link")
bot.infinity_polling()