|
| 1 | +import weechat |
| 2 | +import requests |
| 3 | +import json |
| 4 | + |
| 5 | +# Configuración inicial |
| 6 | +SCRIPT_NAME = "ollama_bot" |
| 7 | +SCRIPT_AUTHOR = "teraflops" |
| 8 | +SCRIPT_VERSION = "1.5" |
| 9 | +SCRIPT_LICENSE = "MIT" |
| 10 | +SCRIPT_DESC = "Responde automáticamente a menciones con Ollama y permite consultas manuales, incluyendo PMs" |
| 11 | +OLLAMA_API_URL = "http://localhost:11434/api/generate" |
| 12 | + |
| 13 | +# Registrar el script |
| 14 | +weechat.register(SCRIPT_NAME, SCRIPT_AUTHOR, SCRIPT_VERSION, SCRIPT_LICENSE, SCRIPT_DESC, "", "") |
| 15 | + |
| 16 | +# Configuración del script en Weechat |
| 17 | +def setup_config(): |
| 18 | + if not weechat.config_is_set_plugin("highlight_response"): |
| 19 | + weechat.config_set_plugin("highlight_response", "on") # Responder a menciones y PMs por defecto |
| 20 | +setup_config() |
| 21 | + |
| 22 | +def ask_ollama(message): |
| 23 | + """Consulta a Ollama enviando un mensaje y devuelve la respuesta completa.""" |
| 24 | + try: |
| 25 | + data = {"model": "gemma", "prompt": message, "stream": False} |
| 26 | + response = requests.post(OLLAMA_API_URL, json=data) |
| 27 | + response_json = response.json() |
| 28 | + return response_json.get("response", "No se recibió respuesta de Ollama.") |
| 29 | + except Exception as e: |
| 30 | + return f"Error al conectar con Ollama: {str(e)}" |
| 31 | + |
| 32 | +def command_ollama(data, buffer, args): |
| 33 | + """Comando /ollama para preguntar manualmente a Ollama.""" |
| 34 | + if not args: |
| 35 | + weechat.prnt(buffer, "[Ollama] Uso: /ollama <pregunta>") |
| 36 | + return weechat.WEECHAT_RC_OK |
| 37 | + |
| 38 | + response = ask_ollama(args) |
| 39 | + weechat.prnt(buffer, f"[Ollama] {response}") |
| 40 | + return weechat.WEECHAT_RC_OK |
| 41 | + |
| 42 | +def message_callback(data, buffer, date, tags, displayed, highlight, prefix, message): |
| 43 | + """Detecta menciones en canales o mensajes privados y responde automáticamente con Ollama.""" |
| 44 | + if weechat.config_get_plugin("highlight_response") == "off": |
| 45 | + return weechat.WEECHAT_RC_OK # No responde si la opción está deshabilitada |
| 46 | + |
| 47 | + buffer_type = weechat.buffer_get_string(buffer, "localvar_type") |
| 48 | + is_private = buffer_type == "private" |
| 49 | + username = weechat.info_get("irc_nick", "") # Obtiene tu nombre de usuario en IRC |
| 50 | + is_mentioned = username.lower() in message.lower() |
| 51 | + |
| 52 | + if int(highlight) or is_private or is_mentioned: |
| 53 | + response = ask_ollama(message) |
| 54 | + weechat.command(buffer, f"/msg {prefix} {response}") |
| 55 | + return weechat.WEECHAT_RC_OK |
| 56 | + |
| 57 | +def config_callback(data, option, value): |
| 58 | + """Callback para cambios en la configuración de Weechat.""" |
| 59 | + weechat.prnt("", f"[Ollama] Configuración cambiada: {option} = {value}") |
| 60 | + return weechat.WEECHAT_RC_OK |
| 61 | + |
| 62 | +# Registrar configuración con /set |
| 63 | +weechat.config_set_desc_plugin("highlight_response", "Responde automáticamente a menciones y PMs (on/off)") |
| 64 | +weechat.hook_config("plugins.var.python.ollama_bot.highlight_response", "config_callback", "") |
| 65 | + |
| 66 | +# Registrar comandos y hooks |
| 67 | +weechat.hook_command("ollama", "Pregunta algo a Ollama", "<pregunta>", "Ejemplo: /ollama ¿Qué es Python?", "", "command_ollama", "") |
| 68 | +weechat.hook_print("", "notify_highlight", "", 1, "message_callback", "") |
| 69 | +weechat.hook_print("", "notify_message", "", 1, "message_callback", "") # Captura mensajes normales |
| 70 | +weechat.hook_print("", "notify_private", "", 1, "message_callback", "") |
| 71 | + |
0 commit comments