Repository navigation
Expand file tree
/
Copy pathsample_code_integration.py
More file actions
114 lines (93 loc) · 3.88 KB
/
Copy pathsample_code_integration.py
File metadata and controls
114 lines (93 loc) · 3.88 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
from typing import TYPE_CHECKING, Any, List
if TYPE_CHECKING:
from bytechef_workflow_api import IntegrationDefinition, TaskContext
def summarize(context: "TaskContext") -> str:
email = context.input("newEmail")
summary = f"New email from {email['from']}: {email['subject']}"
context.log("info", summary)
return summary
def fetch_emails(context: "TaskContext") -> List[Any]:
request = context.input().get("request") or {}
body = request.get("body") or {}
result = context.component.googleMail.searchEmail(
{"from": body.get("from"), "maxResults": body.get("maxResults") or 10}
)
messages: List[Any] = result.get("messages") or []
context.log("info", f"Found {len(messages)} emails")
return messages
def post_to_slack(context: "TaskContext") -> Any:
email = context.input("newEmail")
text = f"New email from {email['from']}: {email['subject']}"
message = context.component.slack.sendChannelMessage({"channel": context.input("channel"), "text": text}, "slack")
context.log("info", f"Posted to Slack: {text}")
return message
integration: "IntegrationDefinition" = {
"componentName": "googleMail",
"componentVersion": 1,
"version": "0.0.1",
"description": "A sample code integration built on the Gmail component.",
"workflows": [
{
"name": "summarizeNewEmail",
"label": "Summarize New Email",
"description": "Runs for every new email in the connected user's inbox and logs who sent it.",
"triggers": [
{"name": "newEmail", "type": "googleMail/v1/newEmailPolling", "parameters": {"format": "SIMPLE"}}
],
"outputs": [{"name": "summary", "task": "summarize"}],
"tasks": [
{
"name": "summarize",
"label": "Summarize",
"description": "Builds a one-line summary of the new email.",
"perform": summarize,
}
],
},
{
"name": "fetchRecentEmails",
"label": "Fetch Recent Emails",
"description": "Returns the connected user's most recent emails, optionally only those from one sender.",
"triggers": [{"name": "request", "type": "request/v1/awaitWorkflowAndRespond"}],
"outputs": [{"name": "emails", "task": "fetchEmails"}],
"tasks": [
{
"name": "fetchEmails",
"label": "Fetch Emails",
"description": "Searches the inbox with the Gmail Search Email action.",
"perform": fetch_emails,
}
],
},
{
"name": "notifySlackOnNewEmail",
"label": "Notify Slack on New Email",
"description": "Posts every new email in the connected user's inbox to a Slack channel.",
"inputs": [
{
"name": "channel",
"label": "Slack Channel",
"type": "STRING",
"required": True,
"componentName": "slack",
"componentVersion": 1,
"groupName": "channel",
}
],
"triggers": [
{"name": "newEmail", "type": "googleMail/v1/newEmailPolling", "parameters": {"format": "SIMPLE"}}
],
"outputs": [{"name": "message", "task": "postToSlack"}],
"tasks": [
{
"name": "postToSlack",
"label": "Post to Slack",
"description": "Posts the sender and subject with the Slack Send Channel Message action.",
"connections": [{"componentName": "slack", "name": "slack"}],
"perform": post_to_slack,
}
],
},
],
}
integration