Create Telegram bot chatgpt

1. Create bot telegram
– Find BotFather on telegram

– Choose /newbot and name your bot

– Save bot’s address and token

2. Get Chatgpt token
– Visit https://platform.openai.com/api-keys and create new api key

3. Create bot.py
+ Install module:
apt install python3-pip
pip3 install python-telegram-bot==13.15
pip3 install openai==0.28

+ bot.py
import logging
from telegram import Update
from telegram.ext import Updater, CommandHandler, MessageHandler, Filters, CallbackContext
import openai

logging.basicConfig(
format=’%(asctime)s – %(name)s – %(levelname)s – %(message)s’,
level=logging.INFO
)

TELEGRAM_TOKEN = ‘PASTE TOKEN HERE’
OPENAI_API_KEY = ‘PASTE TOKEN HERE’

openai.api_key = OPENAI_API_KEY

def start(update: Update, context: CallbackContext) -> None:
update.message.reply_text(‘Hi! I’m CHATGPT bot’)

def handle_message(update: Update, context: CallbackContext) -> None:
user_message = update.message.text
response = openai.ChatCompletion.create( model=”gpt-4o-mini”, messages=[ {“role”: “system”, “content”: “You are a helpful assistant.”}, {“role”: “user”, “content”: user_message} ] ) bot_reply = response[‘choices’][0][‘message’][‘content’].strip()


update.message.reply_text(bot_reply)

def main() -> None:
updater = Updater(TELEGRAM_TOKEN)

dispatcher = updater.dispatcher

dispatcher.add_handler(CommandHandler("start", start))
dispatcher.add_handler(MessageHandler(Filters.text & ~Filters.command, handle_message))

updater.start_polling()

updater.idle()

if name == ‘main‘:
main()

4. Run
python3 bot.py

– To run file when you stop ssh session:
nohup python3 bot.py &

tail -f nohup.out => Check log

ps aux | grep bot.py
kill <PID>
=> stop bot.py

7 thoughts on “Create Telegram bot chatgpt

  1. The website design looks great—clean, user-friendly, and visually appealing! It definitely has the potential to attract more visitors. Maybe adding even more engaging content (like interactive posts, videos, or expert insights) could take it to the next level. Keep up the good work!

  2. This is an English text. Here’s a comment in English:

    Interesting read! I’ve been exploring bot creation recently, and this seems like a straightforward guide. Naming the bot and saving its address and token are crucial steps, but I wonder if there’s more to it in terms of customization. How do you ensure the bot’s functionality aligns with specific needs? Also, is there a way to test the bot before deploying it fully? I’d love to hear your thoughts on best practices for integrating the API key securely. What’s your experience been like with this process?

    1. Hi
      You don’t need any steps to set up the bot
      This uses the chatgpt api so it’s really useful if you don’t have access to chatgpt but telegram does

  3. – Interesting approach to setting up a bot!
    – I wonder how effective this method is for different types of bots.
    – Have you tested this with various APIs or just OpenAI?
    – The simplicity of the steps is appealing, but are there any hidden complexities?
    – I’d love to know if this works seamlessly across platforms.
    – What’s your experience with the bot’s performance so far?
    – Do you think this method could be improved or expanded for more advanced features?

    1. Hi
      I just used openai bot
      I have shared all steps no hidden step
      I have tried and it works perfectly with telegram app on phone, pc.
      I think there is room for further improvement because currently two consecutive lines of dialogue, the bot does not understand the context between these two lines of dialogue

  4. The website design is indeed clean and user-friendly, which makes the process of creating a bot seem less intimidating. I appreciate the emphasis on saving the bot’s address and token, as these are essential steps. However, I’m curious about the level of customization available—can the bot be tailored to handle specific tasks or industries? Testing the bot before full deployment is crucial; are there any tools or methods you’d recommend for this? Also, how do you ensure the API key remains secure during integration? I’d love to hear more about your experience and any tips you might have for someone just starting out. What’s the most challenging part of the process for you?

Leave a Reply

Your email address will not be published. Required fields are marked *