Posts

How to Give Your Telegram Bot Conversation Memory with python-telegram-bot and OpenAI

A Telegram bot that forgets every message the moment it replies is barely more useful than a search bar. If you're forwarding user messages to OpenAI's Chat Completions API, you need to maintain a per-user message history array — otherwise the model has zero context, and multi-turn conversations are impossible. This is a common gap in beginner implementations, and fixing it cleanly requires understanding both where to store state and how to structure the messages payload. 🧠 Why the Bot Forgets OpenAI's Chat Completions API is stateless. Every request you send must include the full conversation history in the messages array. If you only send the latest user message, the model treats it as the first message in a brand-new conversation. Your bot isn't broken — it's just not accumulating history before each API call. The fix has two parts: Maintain an in-memory (or persistent) list of message dicts per user Appen...

System, User, and Assistant Roles in the OpenAI Chat API Explained

Image
If you've ever called the ChatCompletion API and stared at the messages array wondering what system , user , and assistant actually do — you're not alone. These three roles are the backbone of every conversation you build with models like GPT-4o, and understanding them deeply unlocks everything from simple chatbots to production-grade AI assistants. By the end of this post, you'll know exactly what each role does, why it matters, and how to use them together in real code. Table of Contents What Is the Messages Array? The System Role The User Role The Assistant Role How the Three Roles Work Together Practical Code Examples Common Mistakes and How to Avoid Them Production Patterns Closing Summary 🗂️ What Is the Messages Array? When you call the ChatCompletion API, you don't send a single string — you send a list of message objects. Each object has two required fields: role and content . The...

Setting Up OpenAI API Key Securely: The Right Way to Store and Use It in Python (2026)

Image
You just got your OpenAI API key — exciting! But before you paste it directly into your Python script, stop. That single habit is responsible for thousands of leaked credentials every year, leading to unexpected bills, account bans, and security breaches. This guide walks you through exactly why hardcoding API keys is dangerous and shows you the clean, professional way to handle secrets in any Python project, whether you're just starting out or building production-grade applications. Table of Contents Why Hardcoding Your API Key Is Dangerous The .env File Approach: Your First Line of Defense Using python-dotenv to Load Your Key System Environment Variables: No Extra Libraries Needed Protecting Your .env with .gitignore Verifying Your Setup End-to-End Advanced: Secrets Management for Production Quick Reference Checklist ⚠️ Why Hardcoding Your API Key Is Dangerous Hardcoding means writing your key directly in...