Created
February 11, 2025 18:20
-
-
Save tobySolutions/cbcbcc59834a43effd6a99e9f4d64cbe to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from telegram.ext import Application, CommandHandler, MessageHandler, filters, CallbackQueryHandler | |
from telegram import InlineKeyboardButton, InlineKeyboardMarkup | |
from telegram.error import ChatMigrated | |
import logging | |
import requests | |
import re | |
logging.basicConfig(level=logging.INFO) | |
TOKEN = '' | |
GAIA_NODE_URL = 'http://localhost:8080' | |
GAIA_INFO_TRIGGERS = [ | |
r'what\s+is\s+gaia', | |
r'how\s+is\s+gaia\s+different', | |
r'tell\s+me\s+about\s+gaia' | |
] | |
async def start(update, context): | |
await update.message.reply_text('Hello! I\'m a DevRel at Gaia. Send me a message or use /gaia command!') | |
async def gaia_command(update, context): | |
message = ' '.join(context.args) if context.args else "Hi, how are you?" | |
await handle_completion(update, message) | |
async def message_handler(update, context): | |
text = update.message.text.lower() | |
if any(re.search(pattern, text, re.IGNORECASE) for pattern in GAIA_INFO_TRIGGERS): | |
keyboard = [[ | |
InlineKeyboardButton( | |
"๐ Learn More About Gaia", | |
url="https://docs.gaianet.ai/intro" | |
) | |
]] | |
reply_markup = InlineKeyboardMarkup(keyboard) | |
await safe_reply(update, | |
"*Welcome to Gaia!* ๐\nDiscover the future of decentralized AI.", | |
parse_mode='Markdown', | |
reply_markup=reply_markup | |
) | |
return | |
await handle_completion(update, text) | |
async def safe_reply(update, text, **kwargs): | |
try: | |
await update.message.reply_text(text, **kwargs) | |
except ChatMigrated as e: | |
# Get the new chat ID from the error | |
new_chat_id = e.migrate_to_chat_id | |
# Use the bot instance to send the message to the new chat ID | |
await update.message.bot.send_message( | |
chat_id=new_chat_id, | |
text=text, | |
**kwargs | |
) | |
async def handle_completion(update, message): | |
payload = { | |
"messages": [ | |
{"role": "system", "content": "You are a Gaia DevRel Agent."}, | |
{"role": "user", "content": message} | |
] | |
} | |
try: | |
response = requests.post( | |
f'{GAIA_NODE_URL}/v1/chat/completions', | |
headers={'Content-Type': 'application/json'}, | |
json=payload | |
) | |
print("Response:", response.text) | |
if response.status_code == 200: | |
response_data = response.json() | |
response_text = response_data.get('choices', [{}])[0].get('message', {}).get('content', 'No response') | |
await safe_reply(update, response_text) | |
else: | |
await safe_reply(update, f"Error: Received status code {response.status_code}") | |
except Exception as e: | |
print(f"Error: {e}") | |
await safe_reply(update, "Sorry, I encountered an error while processing your request.") | |
async def button_callback(update, context): | |
query = update.callback_query | |
await query.answer() | |
def main(): | |
application = Application.builder().token(TOKEN).build() | |
application.add_handler(CommandHandler('start', start)) | |
application.add_handler(CommandHandler('gaia', gaia_command)) | |
application.add_handler(MessageHandler(filters.TEXT & ~filters.COMMAND, message_handler)) | |
application.add_handler(CallbackQueryHandler(button_callback)) | |
print("Bot is starting...") | |
application.run_polling() | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment