Initial neo4j collector code
This commit is contained in:
parent
8d778c4afc
commit
349999a432
|
|
@ -1,164 +0,0 @@
|
|||
import requests
|
||||
import discord
|
||||
import json
|
||||
import queue
|
||||
from discord.ext import commands, tasks
|
||||
from ovos_plugin_manager.phal import PHALPlugin
|
||||
from ovos_config.config import LocalConf
|
||||
from ovos_config.config import update_mycroft_config
|
||||
from ovos_config.locations import get_webcache_location
|
||||
from ovos_bus_client.util import get_mycroft_bus
|
||||
from ovos_utils.messagebus import Message
|
||||
from ovos_utils.log import LOG
|
||||
from ovos_utils import classproperty
|
||||
from ovos_utils.process_utils import RuntimeRequirements
|
||||
from json_database import JsonConfigXDG
|
||||
|
||||
|
||||
class DiscordBotPlugin(PHALPlugin):
|
||||
def __init__(self, bus=None, config=None):
|
||||
super().__init__(bus, "ovos-PHAL-plugin-discord-bot", config)
|
||||
#self.web_config = LocalConf(get_webcache_location())
|
||||
|
||||
# Initialize queue for Mycroft and user messages
|
||||
self.message_queue = queue.Queue()
|
||||
|
||||
# Initialize Discord bot and intents
|
||||
bot_intents = discord.Intents.all()
|
||||
self.bot = commands.Bot(command_prefix='!', intents=bot_intents)
|
||||
|
||||
self.bus.on("mycroft.internet.connected", self.on_reset)
|
||||
self.bus.on("recognizer_loop:utterance", self.on_utterance)
|
||||
self.bus.on("configuration.updated", self.init_configuration)
|
||||
reinstall_hint = "pip install --upgrade --no-deps --force-reinstall git+https://git.sebastianmaki.fi/sebastian/ovos-PHAL-plugin-discord-bot.git"
|
||||
LOG.info(f"Reinstall hint: {reinstall_hint}")
|
||||
|
||||
# Helper function to get Discord user by ID
|
||||
async def get_discord_user_by_id(user_id) -> discord.User:
|
||||
return await self.bot.fetch_user(user_id)
|
||||
|
||||
# Discord event: bot is ready
|
||||
@self.bot.event
|
||||
async def on_ready():
|
||||
myLoop.start()
|
||||
LOG.info(f"{self.bot.user} has connected to Discord!")
|
||||
|
||||
admin = await get_discord_user_by_id(self.config.get("admin_id"))
|
||||
if admin:
|
||||
LOG.info(f"Admin is {admin.name}")
|
||||
await admin.send("I'm now online")
|
||||
|
||||
# Discord event: Process incoming messages
|
||||
@self.bot.event
|
||||
async def on_message(message):
|
||||
if message.author == self.bot.user:
|
||||
return
|
||||
|
||||
await self.bot.process_commands(message)
|
||||
admin = await get_discord_user_by_id(self.config.get("admin_id"))
|
||||
|
||||
if message.author == admin:
|
||||
if message.content.lower().startswith("tell mycroft"):
|
||||
# The message starts with "tell mycroft"
|
||||
# Implement the action you want to take in response to this here.
|
||||
# For example, you can extract the command from the message and respond accordingly.
|
||||
command = message.content[len("tell mycroft"):].strip() # Remove "tell mycroft" and strip any leading/trailing spaces.
|
||||
# Now, you can execute the command or respond accordingly based on the extracted command.
|
||||
self.bus.emit(
|
||||
Message(
|
||||
"recognizer_loop:utterance",
|
||||
{
|
||||
'utterances': [command],
|
||||
'lang': 'en-us'
|
||||
}
|
||||
)
|
||||
)
|
||||
await message.channel.send(f"Telling mycroft {command}")
|
||||
|
||||
QUEUE_POLL_INTERVAL = 0.2
|
||||
@tasks.loop(seconds=QUEUE_POLL_INTERVAL)
|
||||
async def myLoop():
|
||||
admin = await get_discord_user_by_id(self.config.get("admin_id"))
|
||||
while not self.message_queue.empty():
|
||||
entity, message_content = self.message_queue.get()
|
||||
print(f"{entity} said: {message_content}")
|
||||
if entity == "user":
|
||||
await admin.send(f"User said: {message_content}")
|
||||
elif entity == "mycroft":
|
||||
await admin.send(f"Mycroft said: {message_content}")
|
||||
|
||||
|
||||
self.setup_configuration()
|
||||
|
||||
@classproperty
|
||||
def runtime_requirements(self):
|
||||
return RuntimeRequirements(internet_before_load=True,
|
||||
network_before_load=True,
|
||||
requires_internet=True,
|
||||
requires_network=True,
|
||||
no_internet_fallback=False,
|
||||
no_network_fallback=False)
|
||||
|
||||
def on_reset(self, message=None):
|
||||
LOG.info(f"On reset triggered")
|
||||
|
||||
def on_utterance(self, message=None):
|
||||
LOG.info(f"On utterance triggered: %s", message.serialize())
|
||||
for utterance in message.data['utterances']:
|
||||
self.message_queue.put(("user", utterance))
|
||||
|
||||
def on_speak(self, message=None):
|
||||
LOG.info(f"On speak triggered: %s", message.serialize())
|
||||
self.message_queue.put(("mycroft", message.data['utterance']))
|
||||
|
||||
|
||||
def setup_configuration(self):
|
||||
first_start = self.config.get("first_start", True)
|
||||
if first_start:
|
||||
self.config["first_start"] = False
|
||||
config_patch = {
|
||||
"PHAL": {
|
||||
"ovos-PHAL-plugin-discord-bot": {
|
||||
"first_start": False,
|
||||
"api_token": "",
|
||||
"admin_id": ""
|
||||
}
|
||||
}
|
||||
}
|
||||
update_mycroft_config(config=config_patch, bus=self.bus)
|
||||
self.init_configuration()
|
||||
|
||||
def init_configuration(self, message=None):
|
||||
""" Initialize instance configuration """
|
||||
configuration_first_start = self.config.get("first_start", True)
|
||||
configuration_api_token = self.config.get("api_token", "")
|
||||
configuration_admin_id = self.config.get("admin_id", "")
|
||||
config = {"api_token": configuration_api_token, "admin_id": configuration_admin_id }
|
||||
config_json = json.dumps(config)
|
||||
LOG.info(f"Configuration updated: %s", config_json)
|
||||
|
||||
uninitialized_settings = []
|
||||
if not configuration_api_token:
|
||||
uninitialized_settings.append("api_token")
|
||||
|
||||
if not configuration_admin_id:
|
||||
uninitialized_settings.append("admin_id")
|
||||
|
||||
if uninitialized_settings:
|
||||
self.bus.emit(Message("ovos.phal.plugin.discord_bot.requires.configuration"))
|
||||
config_issue = "The following configuration settings are uninitialized: " + ", ".join(uninitialized_settings)
|
||||
LOG.info(config_issue)
|
||||
else:
|
||||
self.api_token = configuration_api_token
|
||||
self.admin_id = configuration_admin_id
|
||||
LOG.info("All configuration settings are initialized.")
|
||||
self.init_bot()
|
||||
|
||||
def init_bot(self):
|
||||
# Start the Discord bot
|
||||
LOG.info("Starting the bot")
|
||||
self.bot.run(self.api_token)
|
||||
|
||||
|
||||
|
||||
|
||||
|
|
@ -0,0 +1,112 @@
|
|||
import requests
|
||||
|
||||
import json
|
||||
import queue
|
||||
|
||||
from ovos_plugin_manager.phal import PHALPlugin
|
||||
from ovos_config.config import LocalConf
|
||||
from ovos_config.config import update_mycroft_config
|
||||
from ovos_config.locations import get_webcache_location
|
||||
from ovos_bus_client.util import get_mycroft_bus
|
||||
from ovos_utils.messagebus import Message
|
||||
from ovos_utils.log import LOG
|
||||
from ovos_utils import classproperty
|
||||
from ovos_utils.process_utils import RuntimeRequirements
|
||||
from json_database import JsonConfigXDG
|
||||
|
||||
|
||||
class Neo4jCollectorPlugin(PHALPlugin):
|
||||
def __init__(self, bus=None, config=None):
|
||||
super().__init__(bus, "ovos-PHAL-plugin-neo4j-collector", config)
|
||||
#self.web_config = LocalConf(get_webcache_location())
|
||||
|
||||
# Initialize queue for Mycroft and user messages
|
||||
self.message_queue = queue.Queue()
|
||||
|
||||
|
||||
self.bus.on("mycroft.internet.connected", self.on_reset)
|
||||
self.bus.on("recognizer_loop:utterance", self.on_utterance)
|
||||
self.bus.on("configuration.updated", self.init_configuration)
|
||||
reinstall_hint = "pip install --upgrade --no-deps --force-reinstall git+https://git.sebastianmaki.fi/sebastian/ovos-PHAL-plugin-neo4j-collector.git"
|
||||
LOG.info(f"Reinstall hint: {reinstall_hint}")
|
||||
|
||||
self.setup_configuration()
|
||||
|
||||
@classproperty
|
||||
def runtime_requirements(self):
|
||||
return RuntimeRequirements(internet_before_load=True,
|
||||
network_before_load=True,
|
||||
requires_internet=True,
|
||||
requires_network=True,
|
||||
no_internet_fallback=False,
|
||||
no_network_fallback=False)
|
||||
|
||||
def on_reset(self, message=None):
|
||||
LOG.info(f"On reset triggered")
|
||||
|
||||
def on_utterance(self, message=None):
|
||||
LOG.info(f"On utterance triggered: %s", message.serialize())
|
||||
#for utterance in message.data['utterances']:
|
||||
# self.message_queue.put(("user", utterance))
|
||||
|
||||
def on_speak(self, message=None):
|
||||
LOG.info(f"On speak triggered: %s", message.serialize())
|
||||
#self.message_queue.put(("mycroft", message.data['utterance']))
|
||||
|
||||
|
||||
def setup_configuration(self):
|
||||
first_start = self.config.get("first_start", True)
|
||||
if first_start:
|
||||
self.config["first_start"] = False
|
||||
config_patch = {
|
||||
"PHAL": {
|
||||
"ovos-PHAL-plugin-neo4j-collector": {
|
||||
"first_start": False,
|
||||
"neo4j_uri": "",
|
||||
"neo4j_password": "",
|
||||
"neo4j_username": ""
|
||||
}
|
||||
}
|
||||
}
|
||||
update_mycroft_config(config=config_patch, bus=self.bus)
|
||||
self.init_configuration()
|
||||
|
||||
def init_configuration(self, message=None):
|
||||
""" Initialize instance configuration """
|
||||
configuration_first_start = self.config.get("first_start", True)
|
||||
configuration_neo4j_uri = self.config.get("neo4j_uri", "")
|
||||
configuration_neo4j_username= self.config.get("neo4j_username", "")
|
||||
configuration_neo4j_password = self.config.get("neo4j_password", "")
|
||||
config = {"neo4j_uri": configuration_neo4j_uri, "neo4j_username": configuration_neo4j_username, "neo4j_password": configuration_neo4j_password }
|
||||
config_json = json.dumps(config)
|
||||
LOG.info(f"Configuration updated: %s", config_json)
|
||||
|
||||
uninitialized_settings = []
|
||||
if not configuration_neo4j_uri:
|
||||
uninitialized_settings.append("NEO4J_URI")
|
||||
|
||||
if not configuration_neo4j_username:
|
||||
uninitialized_settings.append("NEO4J_USERNAME")
|
||||
|
||||
if not configuration_neo4j_password:
|
||||
uninitialized_settings.append("NEO4J_PASSWORD")
|
||||
|
||||
if uninitialized_settings:
|
||||
self.bus.emit(Message("ovos.phal.plugin.neo4j_collector.requires.configuration"))
|
||||
config_issue = "The following configuration settings are uninitialized: " + ", ".join(uninitialized_settings)
|
||||
LOG.info(config_issue)
|
||||
else:
|
||||
self.neo4j_uri = configuration_neo4j_uri
|
||||
self.neo4j_user = configuration_neo4j_username
|
||||
self.neo4j_pass = configuration_neo4j_password
|
||||
LOG.info("All configuration settings are initialized.")
|
||||
self.init_neo4j()
|
||||
|
||||
def init_neo4j(self):
|
||||
# Start the Connection to a neo4j instance
|
||||
LOG.info("Starting the bot")
|
||||
self.bot.run(self.api_token)
|
||||
|
||||
|
||||
|
||||
|
||||
|
|
@ -2,5 +2,11 @@ ovos-utils < 0.2.0, >=0.1.0a8
|
|||
ovos-plugin-manager~=0.0.23
|
||||
ovos-config < 0.2.0, >=0.0.12
|
||||
ovos-bus-client < 0.2.0, >=0.0.9a3
|
||||
discord.py~=2.3.2
|
||||
|
||||
langchain==0.1.9
|
||||
langchain-community==0.0.24
|
||||
langchain-core==0.1.26
|
||||
langchain-openai==0.0.7
|
||||
langgraph==0.0.26
|
||||
langsmith==0.1.7
|
||||
neo4j==5.15.0
|
||||
neomodel==5.2.1
|
||||
|
|
|
|||
14
setup.py
14
setup.py
|
|
@ -7,7 +7,7 @@ BASEDIR = os.path.abspath(os.path.dirname(__file__))
|
|||
|
||||
def get_version():
|
||||
""" Find the version of the package"""
|
||||
version_file = os.path.join(BASEDIR, 'ovos_PHAL_plugin_discord_bot', 'version.py')
|
||||
version_file = os.path.join(BASEDIR, 'ovos_PHAL_plugin_neo4j_collector', 'version.py')
|
||||
major, minor, build, alpha = (None, None, None, None)
|
||||
with open(version_file) as f:
|
||||
for line in f:
|
||||
|
|
@ -52,19 +52,19 @@ def get_description():
|
|||
return long_description
|
||||
|
||||
|
||||
PLUGIN_ENTRY_POINT = 'ovos-PHAL-plugin-discord-bot=ovos_PHAL_plugin_discord_bot:DiscordBotPlugin'
|
||||
PLUGIN_ENTRY_POINT = 'ovos-PHAL-plugin-neo4j-collector=ovos_PHAL_plugin_neo4j_collector:Neo4jCollectorPlugin'
|
||||
setup(
|
||||
name='ovos-PHAL-plugin-discord-bot',
|
||||
name='ovos-PHAL-plugin-neo4j-collector',
|
||||
version=get_version(),
|
||||
description='Discord bot PHAL plugin for OpenVoiceOS',
|
||||
description='Neo4j collector PHAL plugin for OpenVoiceOS',
|
||||
long_description=get_description(),
|
||||
long_description_content_type="text/markdown",
|
||||
url='https://github.com/neatbasis/ovos-PHAL-plugin-discord-bot',
|
||||
url='https://git.sebastianmaki.fi/sebastian/ovos-PHAL-plugin-neo4j-collector',
|
||||
author='Sebastian Mäki',
|
||||
author_email='sebastian.maki@gmail.com',
|
||||
license='Apache-2.0',
|
||||
packages=['ovos_PHAL_plugin_discord_bot'],
|
||||
package_data={'': package_files('ovos_PHAL_plugin_homeassistant')},
|
||||
packages=['ovos_PHAL_plugin_neo4j_collector'],
|
||||
package_data={'': package_files('ovos_PHAL_plugin_neo4j_collector')},
|
||||
install_requires=required("requirements.txt"),
|
||||
zip_safe=True,
|
||||
include_package_data=True,
|
||||
|
|
|
|||
Loading…
Reference in New Issue