Skip to content

Commit

Permalink
address code review comments
Browse files Browse the repository at this point in the history
  • Loading branch information
pashafateev committed Oct 3, 2024
1 parent ad23c19 commit 0443d9f
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 39 deletions.
35 changes: 13 additions & 22 deletions samples/google/gmail/README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@

# Gmail Sample

This AutoKitteh project demonstrates 2-way integration with
Expand All @@ -12,12 +13,11 @@ This AutoKitteh project demonstrates 2-way integration with

1. Install and start a
[self-hosted AutoKitteh server](https://docs.autokitteh.com/get_started/quickstart),
or use AutoKitteh Cloud
or use AutoKitteh Cloud.

2. Optional for self-hosted servers (preconfigured in AutoKitteh Cloud):

- [Enable Google connections to use OAuth 2.0](https://docs.autokitteh.com/integrations/google/config)
- [Enable Slack connections to use an OAuth v2 app](https://docs.autokitteh.com/integrations/slack/config)

3. Run these commands to deploy this project's manifest file:

Expand All @@ -29,33 +29,24 @@ This AutoKitteh project demonstrates 2-way integration with
4. Initialize this project's connections:

- Google Sheets: with user impersonation using OAuth 2.0 (based on step 2),
or a GCP service account's JSON key
- Slack: with an OAuth v2 app (based on step 2), or a Socket Mode app
or a GCP service account's JSON key.

> [!TIP]
> The exact CLI commands to do so (`ak connection init ...`) will appear in
> the output of the `ak deploy` command from step 3 when you create the
> project on the server, i.e. when you run that command for the first time.
> project on the server, i.e., when you run that command for the first time.
## Usage Instructions

1. Run a slash command of the Slack app that you initialized in step 4 above,
with any of these commands as the slash command's text:

- `gmail get profile`
- `gmail drafts list [optional query]`
- `gmail drafts get <draft ID>`
- `gmail messages list [optional query]`
- `gmail messages get <message ID>`
- `gmail messages send <short message to yourself>`

2. See the Slack app's DM responses to you
1. Run these commands to interact with Gmail via HTTP trigger using query parameters:

3. Send to yourself an email, using the Slack slash command with this text:

```
gmail messages send <Slack channel name or ID>
```shell
curl -i "http://localhost:9980/webhooks/SLUG?cmd=get_profile"
curl -i "http://localhost:9980/webhooks/SLUG?cmd=list_drafts&query=optional_query"
curl -i "http://localhost:9980/webhooks/SLUG?cmd=get_draft&draft_id=<draft_ID>"
curl -i "http://localhost:9980/webhooks/SLUG?cmd=list_messages&query=optional_query"
curl -i "http://localhost:9980/webhooks/SLUG?cmd=get_message&message_id=<message_ID>"
curl -i "http://localhost:9980/webhooks/SLUG?cmd=send_message&text=<message_text>"
```

4. See the resulting message in the specified Slack channel - which is a
result of handling a mailbox change event from Gmail
2. View the responses in your terminal after making the requests.
35 changes: 18 additions & 17 deletions samples/google/gmail/program.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,29 +34,30 @@ def on_http_get(event):
params = event.data.url.query
cmd = params.get("cmd")

if cmd == "get_profile":
_get_profile()
elif cmd == "list_drafts":
_drafts_list(params.get("query", ""))
elif cmd == "get_draft":
_drafts_get(params.get("draft_id"))
elif cmd == "list_messages":
_messages_list(params.get("query", ""))
elif cmd == "get_message":
_messages_get(params.get("message_id"))
elif cmd == "send_message":
_messages_send(params.get("text"))
else:
return "Unknown command"
match cmd:
case "get_profile":
_get_profile()
case "list_drafts":
_drafts_list(params.get("query", ""))
case "get_draft":
_drafts_get(params.get("draft_id"))
case "list_messages":
_messages_list(params.get("query", ""))
case "get_message":
_messages_get(params.get("message_id"))
case "send_message":
_messages_send(params.get("text"))
case _:
return "Unknown command"


def _get_profile():
"""https://developers.google.com/resources/api-libraries/documentation/gmail/v1/python/latest/gmail_v1.users.html#getProfile"""
resp = gmail.getProfile(userId="me").execute()
print(resp["emailAddress"])
print(f"Total no. of messages: {resp['messagesTotal']}")
print(f"Total no. of threads: {resp['threadsTotal']}")
print(f"Current History record ID: {resp['historyId']}")
print("Total no. of messages:", resp["messagesTotal"])
print("Total no. of threads:", resp["threadsTotal"])
print("Current History record ID:", resp["historyId"])


def _drafts_get(id):
Expand Down

0 comments on commit 0443d9f

Please sign in to comment.