Overview
A simple bot that echoes back any message it receives in private chats, plus responds to the/start command.
Code
How It Works
- Create & Authenticate - Set up the client and login as a bot
- Private Message Handler - Listen for all messages in private chats using
telegram.OnMessageevent - Echo Response - Use
message.Respond(message)to send back the same message - Filter Private -
telegram.FilterPrivateensures the bot only echoes in private chats, not groups - Start Command - Special handler for
/startcommand that sends a greeting - Keep Running -
client.Idle()keeps the bot alive and listening for updates
Key Differences
message.Reply()- Replies to the original message (shows “in reply to”)message.Respond()- Sends a new message without replying- Event Pattern -
cmd:start"is a shorthand for handling the/startcommand
Running the Example
- Replace
YOUR_APP_HASHwith your API hash - Replace
YOUR_BOT_TOKENwith your bot token from @BotFather - Run the program:
- Send any message to your bot in private chat - it will echo it back!
- Try
/startto see the greeting message
Next Steps
Welcome Bot
Welcome new members to groups
Event Handlers
Learn more about handling updates