-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.py
167 lines (117 loc) · 5.32 KB
/
main.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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
import os
import gspread
import requests
from random import shuffle, choice
from dotenv import load_dotenv
load_dotenv()
from fuzzywuzzy import fuzz
import telebot
from telebot.types import InlineQueryResultGif
from telebot.types import InlineKeyboardMarkup
from telebot.types import InlineKeyboardButton
from telebot.types import InputTextMessageContent
from telebot.types import InlineQueryResultArticle
from oauth2client.service_account import ServiceAccountCredentials
FEEDBACK_URL = os.getenv("forms_url")
MIN_MATCH_PERCENT = 50
BASE_URL = "https://cultofthepartyparrot.com/"
GIF = "CgACAgQAAxkBAAM-ZCxVhZuyyp7xCHUXa3yODIiWh2cAAuwCAAKK2QxTy2T3R_ahpVEvBA"
STICKER = 'CAACAgIAAxkBAANAZCxVrOtV9-YNwy1MJbdQC-JIw7AAApsBAAK6wJUFlwkHcwsoZlYvBA'
API_TOKEN = os.getenv("API_KEY")
scope = [
"https://spreadsheets.google.com/feeds",
"https://www.googleapis.com/auth/spreadsheets",
"https://www.googleapis.com/auth/drive.file",
"https://www.googleapis.com/auth/drive",
]
bot = telebot.TeleBot(API_TOKEN, parse_mode="HTML")
creds = ServiceAccountCredentials.from_json_keyfile_name("creds.json", scope)
client = gspread.authorize(creds)
database = client.open("DATABASE").worksheet("PARTY PARROT BOT")
analytics = client.open("ANALYTICS").worksheet("PARTY PARROT BOT")
def add_logs(cell):
analytics.update_acell(cell,
str(int(analytics.acell(cell).value) + 1).replace("'", " "))
def match(query, data):
if fuzz.ratio(query, data) >= MIN_MATCH_PERCENT:
return True
return False
def searchParrot(parrot, limit=10):
response = requests.get(BASE_URL + "parrots.json").json()
results = []
for _ in response:
if match(parrot.lower(), _["name"].lower()):
results.append(_)
shuffle(results)
return results[:limit]
@bot.message_handler(commands=["start"])
def start(message):
add_logs("F3")
col = database.col_values(1)
if str(message.chat.id) not in col:
database.append_row([message.chat.id])
analytics.update_acell('F10', str(len(col) + 1))
bot.send_message(message.chat.id, "<i>🦜 PARTY OR DIE 🦜\n\
\nTag @PartyParrotsBot in any chat (1:1 or group) to start a party and search for the right parrot(s)! 🥳</i>")
bot.send_animation(message.chat.id, GIF)
@bot.message_handler(commands=["help"])
def help(message):
add_logs("F7")
bot.send_message(message.chat.id, '''
Thanks for using the Party Parrots Telegram bot.\n\
\nAfter starting the bot with /start you will be able to tag @PartyParrotsBot in any 1:1 or group chat and search for the right parrot(s).\n\
\nIf you would like to contact me send /contact.\n\
\nFeedback is very appreaciated by filling out a Google form which the bot will send you after sending him /feedback.\n\
\nThe source is available on https://github.com/Crazy-Marvin/PartyParrotsTelegramBot/.\n\nHave fun! 🥳''')
@bot.message_handler(commands=["contact"])
def contact(message):
add_logs("F6")
contact_info = '''
<b>CONTACT :\n\
\n➤ Telegram: https://t.me/Marvin_Marvin\n\
\n➤ E-Mail: [email protected]\n\
\n➤ Issue: https://github.com/Crazy-Marvin/PartyParrotsTelegramBot/issues\n\
\n➤ Source: https://github.com/Crazy-Marvin/PartyParrotsTelegramBot</b>
'''
bot.send_message(message.chat.id, contact_info)
@bot.message_handler(commands=["feedback"])
def feedback(message):
add_logs("F5")
bot.send_message(message.chat.id, FEEDBACK_URL)
bot.send_sticker(message.chat.id, STICKER)
@bot.message_handler(commands=["parrot"])
def parrot(message):
add_logs("F4")
response, results = requests.get(BASE_URL + "parrots.json").json(), []
for _ in response: results.append(_)
randomParrot = choice(results)
gif, hd = randomParrot.get("gif"), randomParrot.get("hd")
name, tip = randomParrot.get("name"), randomParrot.get("tip")
link = BASE_URL + f"parrots/{(hd or gif)}"
if tip: tip = f'<code>{tip}</code>'
bot.send_animation(message.chat.id, link, caption=tip)
@bot.message_handler(commands=["logs"])
def logs(message):
if message.chat.id in [os.getenv("my_id") or os.getenv('analyst_id')]:
bot.send_document(message.chat.id, os.getenv('spreadsheet_url'))
@bot.inline_handler(func=lambda query: True)
def inline_qr(query):
try:
parrots, results = searchParrot(query.query), []
for parrot in parrots:
gif, hd = parrot.get("gif"), parrot.get("hd")
name, tip = parrot.get("name"), parrot.get("tip")
link = BASE_URL + f"parrots/{(hd or gif)}"
if tip: tip = f'<code>{tip}</code>'
results.append(InlineQueryResultGif(
name, link, link, title=name, caption=tip, parse_mode="HTML"))
if results == []:
results.append(InlineQueryResultArticle("parrot", "No parrots found! :(",
InputTextMessageContent("<b>Cult of the Party Parrot! 🦜\nPARTY OR DIE! 😎</b>",
parse_mode="HTML"), reply_markup=InlineKeyboardMarkup().row(
InlineKeyboardButton("Check out more parrots!", url=BASE_URL))))
bot.answer_inline_query(query.id, results, 1,
switch_pm_text="Cult of the Party Parrot! 🦜", switch_pm_parameter="start")
except Exception as e:
print(e)
bot.infinity_polling(skip_pending=True)