gpt-4o-mini-2024-07-18· OpenAI
WildChat-4.8M (AI2)
Follow these formatting rules: 1. Do not use asterisk for bold text 2. Use three…
- User
System: Follow these formatting rules: 1. Do not use asterisk for bold text 2. Use three backticks for
code3. For URLs, only include the domain without http/https 4. Keep formatting minimal and cleanUser: Can you send me a link for a website to deploy my python code for free Assistant: Sure, you can try heroku.com. User: Write me a telegram chatbot using openai api using python Assistant:
- Assistant
Here's a simple Telegram chatbot using the OpenAI API in Python:
import os import requests from flask import Flask, request app = Flask(__name__) TELEGRAM_API_URL = f'https://api.telegram.org/bot{os.environ["TELEGRAM_API_KEY"]}/' OPENAI_API_URL = 'https://api.openai.com/v1/chat/completions' OPENAI_API_KEY = os.environ["OPENAI_API_KEY"] def send_message(chat_id, text): url = TELEGRAM_API_URL + 'sendMessage' payload = {'chat_id': chat_id, 'text': text} requests.post(url, json=payload) def get_openai_response(user_message): headers = { 'Authorization': f'Bearer {OPENAI_API_KEY}', 'Content-Type': 'application/json', } data = { 'model': 'gpt-3.5-turbo', 'messages': [{'role': 'user', 'content': user_message}], } response = requests.post(OPENAI_API_URL, headers=headers, json=data) return response.json()['choices'][0]['message']['content'] @app.route('/webhook', methods=['POST']) def webhook(): update = request.get_json() chat_id = update['message']['chat']['id'] user_message = update['message']['text'] bot_response = get_openai_response(user_message) send_message(chat_id, bot_response) return '', 200 if __name__ == '__main__': app.run(port=5000)