generated from putuwaw/flask-app-vercel
-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathapp.py
132 lines (109 loc) · 4.11 KB
/
app.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
import telebot
import os
import time
import requests
import random
from flask import Flask
from modules import modules
from handlers.routes import configure_routes
from dotenv import load_dotenv
load_dotenv()
TOKEN = os.getenv('TOKEN')
URL = os.getenv('URL')
OWNER = os.getenv('OWNER')
SHORTEN_URL_ENDPOINT = os.getenv('SHORTEN_URL_ENDPOINT')
bot = telebot.TeleBot(token=TOKEN, threaded=False)
app = Flask(__name__)
configure_routes(app, bot)
@bot.message_handler(commands=['start'])
def command_start(message):
cid = message.chat.id
bot.send_message(
cid, "Welcome to putuwaw_bot!\nType /help to find all commands.")
@bot.message_handler(commands=['help'])
def command_help(message):
cid = message.chat.id
help_text = "The following commands are available: \n"
for key in modules.COMMANDS:
help_text += '/' + key + ': '
help_text += modules.COMMANDS[key] + '\n'
bot.send_message(cid, help_text)
@bot.message_handler(commands=['ping', 'p'])
def command_ping(message):
if message.chat.id != int(OWNER):
bot.reply_to(message, "Sorry you are not allowed to use this command!")
else:
start_time = time.time()
ping = modules.get_running_time(start_time)
bot.reply_to(message, "PONG! Running time: {:.3f} s.".format(ping))
@bot.message_handler(commands=['caps'])
def command_caps(message):
string = str(message.text)
caps = string.replace('/caps ', '')
bot.reply_to(message, caps.upper())
@bot.message_handler(commands=['ask'])
def command_ask(message):
if message.chat.id != int(OWNER):
bot.reply_to(message, "Sorry you are not allowed to use this command!")
else:
string = str(message.text)
question = string.replace('/ask ', '')
bot.reply_to(message, modules.get_answer(question))
@bot.message_handler(commands=['short'])
def command_team(message):
string = str(message.text).replace('/short ', '').split()
url = ""
custom = ""
if len(string) < 1:
bot.reply_to(message, "Too few arguments!")
elif len(string) > 2:
bot.reply_to(message, "Too many arguments!")
else:
url = string[0]
if len(string) == 2:
custom = string[1]
if not modules.is_valid_url(url):
bot.reply_to(message, "URL is not valid!")
elif not modules.is_valid_custom(custom) and custom != '':
bot.reply_to(message, "Custom URL is not valid!")
else:
r = requests.post(SHORTEN_URL_ENDPOINT, json={
'url': url,
'custom': custom
})
response = r.json()
result = response['status'] + "\n" + response['message']
bot.reply_to(message, result)
@bot.message_handler(commands=['rand'])
def command_random(message):
string = str(message.text)
question = string.replace('/rand ', '').split()
idx = random.randint(0, len(question) - 1)
bot.reply_to(message, question[idx])
@bot.message_handler(commands=['team'])
def command_team(message):
string = str(message.text).replace('/team ', '').split()
if string[0].isnumeric():
n_team = int(string[0])
result = modules.get_random_team(n_team, string[1:])
bot.reply_to(message, result)
else:
bot.reply_to(message, "First argument must be a number!")
@bot.message_handler(commands=['stats'])
def command_stats(message):
if message.chat.id != int(OWNER):
bot.reply_to(message, "Sorry you are not allowed to use this command!")
else:
string = str(message.text).replace('/stats ', '').split()
if len(string) < 2:
bot.reply_to(message, "Too few arguments!")
elif len(string) > 2:
bot.reply_to(message, "Too many arguments!")
else:
response = modules.get_github_stats(string[0], string[1])
bot.reply_to(message, response)
@bot.message_handler(func=lambda message: modules.is_command(message.text))
def command_unknown(message):
command = str(message.text).split()[0]
bot.reply_to(
message, "Sorry, {} command not found!\nPlease use /help to find all commands.".format(command))