Skip to content

Instantly share code, notes, and snippets.

@diegogslomp
Last active January 3, 2023 01:47
Show Gist options
  • Save diegogslomp/da2a0571a4f99f382d613de8b7ae70b5 to your computer and use it in GitHub Desktop.
Save diegogslomp/da2a0571a4f99f382d613de8b7ae70b5 to your computer and use it in GitHub Desktop.
Chat GPT answer to 'write a telegram bot'
import logging
import telegram
from telegram.ext import Updater, CommandHandler, MessageHandler, Filters
# Enable logging
logging.basicConfig(format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
level=logging.INFO)
logger = logging.getLogger(__name__)
# Define a function to handle the /start command
def start(update, context):
update.message.reply_text('Hi! Use /echo to send a message that I will repeat back to you.')
# Define a function to handle the /echo command
def echo(update, context):
update.message.reply_text(update.message.text)
# Define a function to handle all other messages
def unknown(update, context):
update.message.reply_text('Sorry, I don't understand that command.')
# Create the Updater and pass it your bot's token
updater = Updater('YOUR_BOT_TOKEN', use_context=True)
# Get the dispatcher to register handlers
dispatcher = updater.dispatcher
# Add the start command handler
dispatcher.add_handler(CommandHandler('start', start))
# Add the echo command handler
dispatcher.add_handler(CommandHandler('echo', echo))
# Add the unknown command handler
dispatcher.add_handler(MessageHandler(Filters.command, unknown))
# Start the bot
updater.start_polling()
# Run the bot until you press Ctrl-C or the process receives SIGINT,
# SIGTERM or SIGABRT. This should be used most of the time, since
# start_polling() is non-blocking and will stop the bot gracefully.
updater.idle()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment