2023-02-19 18:10:11 +02:00
|
|
|
import asyncio
|
2021-05-25 00:56:44 +03:00
|
|
|
import logging
|
2023-08-10 22:10:30 +03:00
|
|
|
import sys
|
|
|
|
|
from os import getenv
|
2021-01-26 21:20:52 +02:00
|
|
|
|
2023-02-19 18:10:11 +02:00
|
|
|
from aiogram import Bot, Dispatcher, Router, types
|
2023-08-06 16:59:29 +03:00
|
|
|
from aiogram.enums import ParseMode
|
2023-08-10 22:10:30 +03:00
|
|
|
from aiogram.filters import CommandStart
|
2021-05-25 00:56:44 +03:00
|
|
|
from aiogram.types import Message
|
2023-08-10 22:10:30 +03:00
|
|
|
from aiogram.utils.markdown import hbold
|
2021-01-26 21:20:52 +02:00
|
|
|
|
2023-04-16 21:31:33 +00:00
|
|
|
# Bot token can be obtained via https://t.me/BotFather
|
Update echo_bot.py
Error TOKEN
File "D:\HlamProjects\Python\TelegramBot\main.py", line 57, in <module>
asyncio.run(main())
File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.11_3.11.1776.0_x64__qbz5n2kfra8p0\Lib\asyncio\runners.py", line 190, in run
return runner.run(main)
^^^^^^^^^^^^^^^^
File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.11_3.11.1776.0_x64__qbz5n2kfra8p0\Lib\asyncio\runners.py", line 118, in run
return self._loop.run_until_complete(task)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.11_3.11.1776.0_x64__qbz5n2kfra8p0\Lib\asyncio\base_events.py", line 653, in run_until_complete
return future.result()
^^^^^^^^^^^^^^^
File "D:\HlamProjects\Python\TelegramBot\main.py", line 50, in main
bot = Bot(TOKEN, parse_mode=ParseMode.HTML)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\User\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.11_qbz5n2kfra8p0\LocalCache\local-packages\Python311\site-packages\aiogram\client\bot.py", line 258, in __init__
validate_token(token)
File "C:\Users\User\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.11_qbz5n2kfra8p0\LocalCache\local-packages\Python311\site-packages\aiogram\utils\token.py", line 17, in validate_token
raise TokenValidationError(
aiogram.utils.token.TokenValidationError: Token is invalid! It must be 'str' type instead of <class 'NoneType'> type.
Need TOKEN = "BOT_TOKEN"
2023-10-15 17:13:21 +07:00
|
|
|
#TOKEN = getenv("BOT_TOKEN") # ERROR geteven
|
|
|
|
|
TOKEN = "BOT_TOKEN"
|
2021-01-26 21:20:52 +02:00
|
|
|
|
2023-02-19 18:10:11 +02:00
|
|
|
# All handlers should be attached to the Router (or Dispatcher)
|
2023-08-21 01:13:19 +03:00
|
|
|
dp = Dispatcher()
|
2021-01-26 21:20:52 +02:00
|
|
|
|
2021-05-25 00:56:44 +03:00
|
|
|
|
2023-08-21 01:13:19 +03:00
|
|
|
@dp.message(CommandStart())
|
2021-05-25 00:56:44 +03:00
|
|
|
async def command_start_handler(message: Message) -> None:
|
2021-01-26 21:20:52 +02:00
|
|
|
"""
|
2023-08-06 16:59:29 +03:00
|
|
|
This handler receives messages with `/start` command
|
2021-01-26 21:20:52 +02:00
|
|
|
"""
|
2022-08-14 01:07:52 +03:00
|
|
|
# Most event objects have aliases for API methods that can be called in events' context
|
2021-05-25 00:56:44 +03:00
|
|
|
# For example if you want to answer to incoming message you can use `message.answer(...)` alias
|
2022-08-14 01:07:52 +03:00
|
|
|
# and the target chat will be passed to :ref:`aiogram.methods.send_message.SendMessage`
|
|
|
|
|
# method automatically or call API method directly via
|
|
|
|
|
# Bot instance: `bot.send_message(chat_id=message.chat.id, ...)`
|
2023-08-10 22:10:30 +03:00
|
|
|
await message.answer(f"Hello, {hbold(message.from_user.full_name)}!")
|
2021-01-26 21:20:52 +02:00
|
|
|
|
|
|
|
|
|
2023-08-21 01:13:19 +03:00
|
|
|
@dp.message()
|
2022-03-26 15:30:46 +00:00
|
|
|
async def echo_handler(message: types.Message) -> None:
|
2021-01-26 21:20:52 +02:00
|
|
|
"""
|
2023-08-06 16:59:29 +03:00
|
|
|
Handler will forward receive a message back to the sender
|
2021-01-26 21:20:52 +02:00
|
|
|
|
2023-08-06 16:59:29 +03:00
|
|
|
By default, message handler will handle all message types (like a text, photo, sticker etc.)
|
2021-01-26 21:20:52 +02:00
|
|
|
"""
|
2021-05-25 00:56:44 +03:00
|
|
|
try:
|
2023-08-06 16:59:29 +03:00
|
|
|
# Send a copy of the received message
|
2021-05-25 00:56:44 +03:00
|
|
|
await message.send_copy(chat_id=message.chat.id)
|
|
|
|
|
except TypeError:
|
|
|
|
|
# But not all the types is supported to be copied so need to handle it
|
|
|
|
|
await message.answer("Nice try!")
|
2021-01-26 21:20:52 +02:00
|
|
|
|
|
|
|
|
|
2023-02-19 18:10:11 +02:00
|
|
|
async def main() -> None:
|
|
|
|
|
# Initialize Bot instance with a default parse mode which will be passed to all API calls
|
2023-08-06 16:59:29 +03:00
|
|
|
bot = Bot(TOKEN, parse_mode=ParseMode.HTML)
|
2021-05-25 00:56:44 +03:00
|
|
|
# And the run events dispatching
|
2023-02-19 18:10:11 +02:00
|
|
|
await dp.start_polling(bot)
|
2021-01-26 21:20:52 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
2023-08-10 22:10:30 +03:00
|
|
|
logging.basicConfig(level=logging.INFO, stream=sys.stdout)
|
2023-08-21 01:13:19 +03:00
|
|
|
asyncio.run(main())
|