Image

(마지막 변경: 19.09.2024)

Python의 난수 생성기인 봇의 성능을 테스트하기 위해 사전 구성된 텔레그램을 사용했습니다. 봇 서버. 이 봇은 사용자가 지정한 범위에서 임의의 숫자를 생성할 수 있습니다.

설치를 진행해 보겠습니다:

source my-tel-bot/bin/activate

데모 봇을 기존 로봇으로 교체해 보겠습니다:

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

import telebot
import random

# to create buttons
from telebot import types

bot = telebot.TeleBot('Key received from @BotFather')

# Creating a button after the /start command
@bot.message_handler(commands=['start'])
def welcome(message):
markup = types.ReplyKeyboardMarkup(resize_keyboard=True)
random_sender = types.KeyboardButton("Came a random number")
markup.add(random_sender)
bot.send_message(message.chat.id, 'Number generator started', parse_mode='html',
reply_markup=markup)

# Button click tracking
@bot.message_handler(content_types=['text'])

def first_number_step(message):
if message.text == 'Пришли случайное число':
msg = bot.send_message(message.chat.id, 'Enter start of range')

# jump to function second_number_step
bot.register_next_step_handler(msg, second_number_step)
else:
bot.send_message(message.chat.id, 'There is no such command')

# Getting the first number of a range
def second_number_step(message):
global NUM_first
NUM_first = int(message.text)
msg = bot.send_message(message.chat.id, 'Enter the end of the range')

# jump to function result_number_step
bot.register_next_step_handler(msg, result_number_step)

# Getting the second number of a range
def result_number_step(message):
global NUM_second
NUM_second = int(message.text)

# Function call result(message)
result(message)

# Result output (random)
def result(message):
bot.send_message(message.chat.id, 'Random number: ' + str(random.randint(NUM_first, NUM_second)))

# To keep the bot running all the time
bot.polling(none_stop=True)

완료되었습니다. 스크립트에 키 토큰을 지정하고 서비스를 다시 시작하는 것을 잊지 마세요:

service my-tel-bot restart



No Comments Yet