Fixed crashing on facebook connector

This commit is contained in:
Jakub Filo 2023-04-20 00:14:24 +02:00
parent 47d1356d4b
commit d5e3db11ff
1 changed files with 62 additions and 50 deletions

View File

@ -63,38 +63,46 @@ async def api_conversation(opsdroid, config, message):
connector_matrix - the object obtained from opsdroid.get_connector
"""
# Get connector_matrix object and start the typing notification
if message.connector.name == "matrix":
connector_matrix = opsdroid.get_connector("matrix")
await connector_matrix.connection.room_typing(message.target,
typing_state=True)
try:
if message.connector.name == "matrix":
connector_matrix = opsdroid.get_connector("matrix")
await connector_matrix.connection.room_typing(message.target,
typing_state=True)
except (NameError,KeyError):
pass
api_to_use = None
if message.connector.name == "matrix" and 'm.relates_to' in message.raw_event['content']:
# Load conversation_context for current thread_id if it exists
question_text = message.text
thread_id = message.raw_event['content']['m.relates_to']['event_id']
conversation_context = await opsdroid.memory.get(thread_id)
api_to_use = conversation_context["api_to_use"]
else:
# This is a new message, the first word is the hot-word
hot_word = message.text.split()[0]
# Then comes the question
question_text = ' '.join(message.text.split()[1:])
# Set thread_id for starting a new thread
thread_id = message.event_id
try:
if message.connector.name == "matrix" and 'm.relates_to' in message.raw_event['content']:
# Load conversation_context for current thread_id if it exists
question_text = message.text
thread_id = message.raw_event['content']['m.relates_to']['event_id']
conversation_context = await opsdroid.memory.get(thread_id)
api_to_use = conversation_context["api_to_use"]
else:
# This is a new message, the first word is the hot-word
hot_word = message.text.split()[0]
# Then comes the question
question_text = ' '.join(message.text.split()[1:])
# Set thread_id for starting a new thread
thread_id = message.event_id
for key in config.get("apis"):
if hot_word == config.get("apis")[key]["hot-word"]:
api_to_use = key
break
if api_to_use is None:
# Nothing matched. End typing notice and quit the script
if message.connector.name == "matrix":
await connector_matrix.connection.room_typing(
message.target, typing_state=False)
return
# Generate empty conversation_context
conversation_context = {"api_to_use": api_to_use}
except (NameError,KeyError):
pass
for key in config.get("apis"):
if hot_word == config.get("apis")[key]["hot-word"]:
api_to_use = key
break
if api_to_use is None:
# Nothing matched. End typing notice and quit the script
if message.connector.name == "matrix":
await connector_matrix.connection.room_typing(
message.target, typing_state=False)
return
# Generate empty conversation_context
conversation_context = {"api_to_use": api_to_use}
api_params = config.get("apis")[api_to_use]
@ -108,26 +116,30 @@ async def api_conversation(opsdroid, config, message):
except KeyError:
response_value = "No such response key was found. Check configuration"
if message.connector.name == "matrix":
# Construct and send a response and save conversation context for matrix
message_dict = {
"msgtype": "m.text",
"body": response_value,
"formatted_body": markdown(response_value,
extensions=['fenced_code']),
"format": "org.matrix.custom.html",
"m.relates_to": {
"rel_type": "m.thread",
"event_id": thread_id,
try:
if message.connector.name == "matrix":
# Construct and send a response and save conversation context for matrix
message_dict = {
"msgtype": "m.text",
"body": response_value,
"formatted_body": markdown(response_value,
extensions=['fenced_code']),
"format": "org.matrix.custom.html",
"m.relates_to": {
"rel_type": "m.thread",
"event_id": thread_id,
}
}
}
await connector_matrix.connection.room_send(message.target,
"m.room.message",
message_dict)
await connector_matrix.connection.room_typing(message.target,
typing_state=False)
await opsdroid.memory.put(thread_id, conversation_context)
else:
# For non-matrix connectors send a response
await message.respond(response_value)
await connector_matrix.connection.room_send(message.target,
"m.room.message",
message_dict)
await connector_matrix.connection.room_typing(message.target,
typing_state=False)
await opsdroid.memory.put(thread_id, conversation_context)
else:
# For non-matrix connectors send a response
await message.respond(response_value)
except (NameError,KeyError):
pass