Navigation X
ALERT
Click here to register with a few steps and explore all our cool stuff we have to offer!



 426

Telegram Bot - Need Help

by AnnaStecka - 30 March, 2024 - 10:17 PM
This post is by a banned member (AnnaStecka) - Unhide
54
Posts
13
Threads
4 Years of service
#1
Hello!
I want to make a Telegram bot but I don't know how to go about it. Do you know any tutorials about this?
Do Something To Break Something Else   lol2
This post is by a banned member (kenthompson) - Unhide
35
Posts
4
Threads
#2
(This post was last modified: 25 April, 2024 - 12:15 PM by kenthompson.)
If you struggle with official documentation, I'd suggest you try to build something first with code examples. You probably using some python telegram library, I bet you can find directory with code examples in the git repository.

I'll give you few examples with AIOGRAM:

Echo Bot Example with AIOGRAM in Python
Code:
import asyncio
import logging
import sys
from os import getenv

from aiogram import Bot, Dispatcher, html
from aiogram.client.default import DefaultBotProperties
from aiogram.enums import ParseMode
from aiogram.filters import CommandStart
from aiogram.types import Message

# Bot token can be obtained via https://t.me/BotFather
TOKEN = getenv("BOT_TOKEN")

# All handlers should be attached to the Router (or Dispatcher)
dp = Dispatcher()


@dp.message(CommandStart())
async def command_start_handler(message: Message) -> None:
    """
    This handler receives messages with `/start` command
    """
    # Most event objects have aliases for API methods that can be called in events' context
    # For example if you want to answer to incoming message you can use `message.answer(...)` alias
    # 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, ...)`
    await message.answer(f"Hello, {html.bold(message.from_user.full_name)}!")


@dp.message()
async def echo_handler(message: Message) -> None:
    """
    Handler will forward receive a message back to the sender

    By default, message handler will handle all message types (like a text, photo, sticker etc.)
    """
    try:
        # Send a copy of the received message
        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!")


async def main() -> None:
    # Initialize Bot instance with default bot properties which will be passed to all API calls
    bot = Bot(token=TOKEN, default=DefaultBotProperties(parse_mode=ParseMode.HTML))
    # And the run events dispatching
    await dp.start_polling(bot)


if __name__ == "__main__":
    logging.basicConfig(level=logging.INFO, stream=sys.stdout)
    asyncio.run(main())

You can learn how to build telegram bots by examples, and then if you don't understand some line of code you google it  [Image: pissed.gif]
[Image: PjqhpK9.gif]

[Image: JQWD1Cz.gif]

Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
or
Sign in
Already have an account? Sign in here.


Forum Jump:


Users browsing this thread: 1 Guest(s)