Spaces:
Sleeping
Sleeping
Commit
·
2647e4c
1
Parent(s):
64a1361
Update app.py
Browse files
app.py
CHANGED
@@ -1,76 +1,59 @@
|
|
1 |
import os
|
2 |
-
|
3 |
-
|
4 |
-
from telegram.ext import Updater, CommandHandler, MessageHandler, Filters, CallbackContext
|
5 |
|
6 |
import requests
|
7 |
|
8 |
-
# Configurar o token do bot
|
9 |
BOT_TOKEN = os.environ.get('BOT_TOKEN')
|
10 |
|
11 |
-
|
12 |
-
logging.basicConfig(format='%(asctime)s - %(name)s - %(levelname)s - %(message)s', level=logging.INFO)
|
13 |
-
logger = logging.getLogger(__name__)
|
14 |
|
15 |
-
# Função para lidar com o comando /start
|
16 |
-
def start(update: Update, context: CallbackContext) -> None:
|
17 |
-
update.message.reply_text("Posso gerar números aleatórios verdadeiros")
|
18 |
|
19 |
-
|
20 |
-
def
|
21 |
-
|
22 |
-
|
|
|
23 |
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
update.message.reply_text("Por favor, escolha o comprimento da matriz. O valor deve variar de 1 a 1024.")
|
28 |
|
29 |
-
|
30 |
-
def text_handler(update: Update, context: CallbackContext) -> None:
|
31 |
-
user_data = context.user_data
|
32 |
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
|
37 |
-
|
38 |
-
|
39 |
-
return
|
40 |
|
41 |
-
|
42 |
-
update.message.reply_text("Desculpe, não entendi. Você pode usar o comando /aleatorio para começar.")
|
43 |
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
array_length = update.message.text
|
48 |
|
|
|
49 |
api_url = f"https://qrng.anu.edu.au/API/jsonI.php?length={array_length}&type={data_type}"
|
50 |
|
|
|
51 |
try:
|
52 |
response = requests.get(api_url)
|
53 |
-
response.raise_for_status()
|
54 |
|
|
|
55 |
result = response.json()
|
|
|
|
|
56 |
random_data = result.get('data', [])
|
57 |
|
58 |
-
|
|
|
59 |
|
60 |
except requests.exceptions.RequestException as e:
|
61 |
-
|
62 |
-
|
63 |
-
# Configurar o Updater e adicionar manipuladores
|
64 |
-
updater = Updater(BOT_TOKEN)
|
65 |
-
dispatcher = updater.dispatcher
|
66 |
-
|
67 |
-
dispatcher.add_handler(CommandHandler("start", start))
|
68 |
-
dispatcher.add_handler(CommandHandler("aleatorio", aleatorio))
|
69 |
-
dispatcher.add_handler(MessageHandler(Filters.text & ~Filters.command, text_handler))
|
70 |
-
dispatcher.add_handler(MessageHandler(Filters.text, process_array_length_choice))
|
71 |
|
72 |
-
# Iniciar o polling
|
73 |
-
updater.start_polling()
|
74 |
|
75 |
-
|
76 |
-
updater.idle()
|
|
|
1 |
import os
|
2 |
+
|
3 |
+
import telebot
|
|
|
4 |
|
5 |
import requests
|
6 |
|
|
|
7 |
BOT_TOKEN = os.environ.get('BOT_TOKEN')
|
8 |
|
9 |
+
bot = telebot.TeleBot(BOT_TOKEN)
|
|
|
|
|
10 |
|
|
|
|
|
|
|
11 |
|
12 |
+
@bot.message_handler(commands=['start', 'hello'])
|
13 |
+
def send_welcome(message):
|
14 |
+
bot.reply_to(message, "Posso gerar números aleatórios verdadeiros")
|
15 |
+
|
16 |
+
@bot.message_handler(commands=['aleatorio'])
|
17 |
|
18 |
+
def sign_handler(message):
|
19 |
+
text = "Por favor escolha um tipo de dado:\nuint8 (Inteiros 0–255), uint16 (Inteiros 0–65535)"
|
20 |
+
bot.reply_to(message, text)
|
|
|
21 |
|
22 |
+
bot.register_next_step_handler(message, process_data_type_choice)
|
|
|
|
|
23 |
|
24 |
+
def process_data_type_choice(message):
|
25 |
+
|
26 |
+
data_type = message.text
|
27 |
|
28 |
+
|
29 |
+
bot.reply_to(message, "Por favor, escolha o comprimento da matriz. O valor deve variar de 1 a 1024.")
|
|
|
30 |
|
31 |
+
bot.register_next_step_handler(message, lambda msg: process_array_length_choice(msg, data_type))
|
|
|
32 |
|
33 |
+
def process_array_length_choice(message, data_type):
|
34 |
+
|
35 |
+
array_length = message.text
|
|
|
36 |
|
37 |
+
|
38 |
api_url = f"https://qrng.anu.edu.au/API/jsonI.php?length={array_length}&type={data_type}"
|
39 |
|
40 |
+
|
41 |
try:
|
42 |
response = requests.get(api_url)
|
43 |
+
response.raise_for_status()
|
44 |
|
45 |
+
|
46 |
result = response.json()
|
47 |
+
|
48 |
+
|
49 |
random_data = result.get('data', [])
|
50 |
|
51 |
+
|
52 |
+
bot.reply_to(message, f"Números aleatórios verdadeiros: {random_data}")
|
53 |
|
54 |
except requests.exceptions.RequestException as e:
|
55 |
+
|
56 |
+
bot.reply_to(message, f"Aguarde 1 min depois da ultima solicitação")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
57 |
|
|
|
|
|
58 |
|
59 |
+
bot.infinity_polling()
|
|