gpt-3.5-turbo-0125· OpenAI
WildChat-4.8M (AI2)
Python code for a telegram bot that sends the current time whenever i message the bot…
- User
Python code for a telegram bot that sends the current time whenever i message the bot "/time"
- Assistant
import telegram from datetime import datetime
Replace 'YOUR_BOT_TOKEN' with the token you get from BotFather
TOKEN = 'YOUR_BOT_TOKEN' bot = telegram.Bot(token=TOKEN)
def get_current_time(): now = datetime.now() current_time = now.strftime("%H:%M:%S") return current_time
def handle_message(update, context): chat_id = update.effective_chat.id text = update.message.text
if text == '/time': current_time = get_current_time() bot.send_message(chat_id=chat_id, text=f"The current time is: {current_time}")from telegram.ext import Updater, CommandHandler, MessageHandler, Filters
updater = Updater(token=TOKEN, use_context=True) dispatcher = updater.dispatcher
start_handler = CommandHandler('start', start) time_handler = MessageHandler(Filters.text & ~Filters.command, handle_message)
dispatcher.add_handler(time_handler)
updater.start_polling() updater.idle()