ouhenio commited on
Commit
17145bf
·
verified ·
1 Parent(s): a62f86a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +36 -0
app.py CHANGED
@@ -18,6 +18,42 @@ for webhook in client.webhooks:
18
  print(f"Deleting webhook: {webhook.url}")
19
  webhook.delete()
20
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
21
 
22
  # Create a webhook for record events
23
  @rg.webhook_listener(events=["record.deleted", "record.completed"])
 
18
  print(f"Deleting webhook: {webhook.url}")
19
  webhook.delete()
20
 
21
+ @webhook_listener(events="response.created")
22
+ async def update_validation_space_on_answer(event):
23
+ """
24
+ Webhook listener that triggers when a new response is added to an answering space.
25
+ It will automatically update the corresponding validation space with the new response.
26
+
27
+ Args:
28
+ event: The webhook event containing the user response data
29
+ """
30
+ response = event.response
31
+
32
+ record = response.record
33
+
34
+ dataset_name = record.dataset.name
35
+ if not dataset_name.endswith("_responder_preguntas"):
36
+ return # Not an answering space, ignore
37
+
38
+ # Extract the country from the dataset name
39
+ country = dataset_name.replace("_responder_preguntas", "")
40
+
41
+ validation_dataset_name = f"{country}_validar_respuestas"
42
+ try:
43
+ validation_dataset = client.datasets(validation_dataset_name)
44
+ except Exception as e:
45
+ print(f"Error connecting to validation dataset: {e}")
46
+ validation_dataset = create_validation_space(country)
47
+
48
+ validation_record = {
49
+ "question": record.fields["question"],
50
+ "answer": response.value,
51
+ }
52
+
53
+ validation_dataset.records.log(records=[validation_record])
54
+
55
+ print(f"Added new response to validation space for {country}")
56
+
57
 
58
  # Create a webhook for record events
59
  @rg.webhook_listener(events=["record.deleted", "record.completed"])