Spaces:
Runtime error
Runtime error
| import requests | |
| from telegram import Update | |
| from telegram.ext import Updater, CommandHandler, MessageHandler, Filters, CallbackContext | |
| # === KONFIGURACJA === | |
| TELEGRAM_TOKEN = "7867210021:AAHnyjxJcgPbFYP0GuD2olh6FNQwg-iwkBw" | |
| XAI_API_KEY = "xai-6N4bz8HESJC4CtazvNkpMUx8F5W4By0oI3xFgsCRb4q63bCIKyDlA1RRWKMkWBj2NUoQvCQGyzC0z7Jx" | |
| XAI_ENDPOINT = "https://api.xai.com/v1/chat" # Przykładowy endpoint, sprawdź oficjalny API | |
| # === FUNKCJA WYSYŁAJĄCA PYTANIE DO XAI === | |
| def ask_xai(question): | |
| headers = {"Authorization": f"Bearer {XAI_API_KEY}", "Content-Type": "application/json"} | |
| payload = {"query": question} | |
| response = requests.post(XAI_ENDPOINT, json=payload, headers=headers) | |
| data = response.json() | |
| return data.get("response", "Nie udało się uzyskać odpowiedzi od AI.") | |
| # === OBSŁUGA WIADOMOŚCI === | |
| def start(update: Update, context: CallbackContext): | |
| update.message.reply_text("Witaj! Jestem asystentem AI. Zadaj mi pytanie, a odpowiem!") | |
| def chat(update: Update, context: CallbackContext): | |
| user_input = update.message.text | |
| response = ask_xai(user_input) | |
| update.message.reply_text(response) | |
| # === START BOTA === | |
| def main(): | |
| updater = Updater(TELEGRAM_TOKEN, use_context=True) | |
| dp = updater.dispatcher | |
| dp.add_handler(CommandHandler("start", start)) | |
| dp.add_handler(MessageHandler(Filters.text & ~Filters.command, chat)) | |
| updater.start_polling() | |
| updater.idle() | |
| if __name__ == "__main__": | |
| main() | |