Image

(Ultimo cambio: 04.05.2024)

Para probar el rendimiento del bot, un generador de números aleatorios en Python, usamos servidor de bots de Telegram preconfigurado. Este bot puede generar un número aleatorio a partir de un rango que especifique.

Procedamos con la instalación:

source my-tel-bot/bin/activate

Reemplacemos nuestro bot de demostración con el existente:

/root/my-tel-bot/bot.py

import telebot
import random

# para crear botones
from telebot import types

bot = telebot.TeleBot('Clave recibida de @BotFather')

# Crear un botón después de un comando /start
@bot.message_handler(commands=['start'])
def welcome(message):
markup = types.ReplyKeyboardMarkup(resize_keyboard=True)
random_sender = types.KeyboardButton("Salio un numero al azar")
markup.add(random_sender)
bot.send_message(message.chat.id, 'Generador de números iniciado', parse_mode='html',
reply_markup=markup)

# Seguimiento de clics en botones
@bot.message_handler(content_types=['text'])

def first_number_step(message):
if message.text == 'Salio un numero al azar':
msg = bot.send_message(message.chat.id, 'Ingrese el inicio del rango')

# saltar a la función second_number_step
bot.register_next_step_handler(msg, second_number_step)
else:
bot.send_message(message.chat.id, 'No hay tal comando')

# Obtener el primer número de un rango
def second_number_step(message):
global NUM_first
NUM_first = int(message.text)
msg = bot.send_message(message.chat.id, 'Introduzca el final del rango')

# saltar a la función result_number_step
bot.register_next_step_handler(msg, result_number_step)

# Obtener el segundo número de un rango
def result_number_step(message):
global NUM_second
NUM_second = int(message.text)

# Llamada de función result(message)
result(message)

# Salida de resultados (aleatoria)
def result(message):
bot.send_message(message.chat.id, 'Número aleatorio: ' + str(random.randint(NUM_first, NUM_second)))

# Para mantener el bot funcionando todo el tiempo
bot.polling(none_stop=True)

Listo, no olvide especificar su token de clave en el script y reinicie el servicio:

service my-tel-bot restart



Sin comentarios aún