Fixed crashing on facebook connector

This commit is contained in:
Jakub Filo 2023-04-20 00:14:24 +02:00
parent 47d1356d4b
commit d5e3db11ff

View File

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