| абоба777 |
28.11.2023 20:52 |
Приветствую! сегодня я поделюсь кодом с разработки простого бота на тему:Камень, Ножницы, Бумага
Данный код вы можете сами изменить, что-то добавить что то убрать - на ваш выбор!
Библиотека: python-telegram-bot (как установить можете найти в инете)
Бот сделан строго для Телеграма!
Код ниже:
Не забудь заменить `YOUR_TELEGRAM_BOT_TOKEN` на токен своего бота, который можно получить у @BotFather в Telegram.
Код чпек:
Код:
python
from
telegram
.
ext
import
Updater
,
CommandHandler
,
MessageHandler
,
Filters
import
random
def
start
(
update
,
context
)
:
context
.
bot
.
send_message
(
chat_id
=
update
.
effective_chat
.
id
,
text
=
"Привет! Давай сыграем в Камень, ножницы, бумага. Введи /play чтобы начать игру."
)
def
play
(
update
,
context
)
:
choices
=
[
"камень"
,
"ножницы"
,
"бумага"
]
user_choice
=
context
.
args
[
0
]
.
lower
(
)
bot_choice
=
random
.
choice
(
choices
)
if
user_choice
not
in
choices
:
context
.
bot
.
send_message
(
chat_id
=
update
.
effective_chat
.
id
,
text
=
"Неверный выбор. Попробуй еще раз."
)
return
if
user_choice
==
bot_choice
:
result
=
"Ничья!"
elif
(
user_choice
==
"камень"
and
bot_choice
==
"ножницы"
)
or
(
user_choice
==
"ножницы"
and
bot_choice
==
"бумага"
)
or
(
user_choice
==
"бумага"
and
bot_choice
==
"камень"
)
:
result
=
"Ты победил!"
else
:
result
=
"Ты проиграл!"
context
.
bot
.
send_message
(
chat_id
=
update
.
effective_chat
.
id
,
text
=
f"Твой выбор:{user_choice}\nВыбор бота:{bot_choice}\n{result}"
)
def
main
(
)
:
updater
=
Updater
(
token
=
"YOUR_TELEGRAM_BOT_TOKEN"
,
use_context
=
True
)
dispatcher
=
updater
.
dispatcher
start_handler
=
CommandHandler
(
"start"
,
start
)
play_handler
=
CommandHandler
(
"play"
,
play
)
dispatcher
.
add_handler
(
start_handler
)
dispatcher
.
add_handler
(
play_handler
)
updater
.
start_polling
(
)
if
__name__
==
'__main__'
:
main
(
)
|