Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: port twilio to python #78

Merged
merged 4 commits into from
Oct 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 5 additions & 7 deletions samples/twilio/autokitteh.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,12 @@ project:
name: twilio_sample
vars:
- name: FROM_PHONE_NUMBER
value:
value:
connections:
- name: slack_conn
integration: slack
- name: twilio_conn
integration: twilio
triggers:
- name: slack_slash_command
connection: slack_conn
event_type: slash_command
call: program.star:on_slack_slash_command
- name: http_get
type: webhook
event_type: get
call: program.py:on_http_get
54 changes: 54 additions & 0 deletions samples/twilio/program.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
"""This program demonstrates AutoKitteh's Twilio integration.

This program implements a single entry-point function triggered by an
HTTP GET request event, as defined in the "autokitteh.yaml" manifest file.

API details:
- Messaging API overview: https://www.twilio.com/docs/messaging/api
- Voice API overview: https://www.twilio.com/docs/voice/api

It also demonstrates using constant values which are set for each
AutoKitteh environment in the "autokitteh.yaml" manifest file.
"""

import os

from autokitteh.twilio import twilio_client

FROM_PHONE_NUMBER = os.getenv("FROM_PHONE_NUMBER")

t = twilio_client("twilio_conn")


def on_http_get(event):
"""Entry-point for workflow.

This function is triggered by an HTTP GET request event and is used to
send SMS and WhatsApp messages via Twilio.

Example usage:
curl "http://localhost:9980/webhooks/<webhook_slug>?to=+15551234567"

The phone number to send the message to must be provided in the query
parameter 'to'. The message will be sent both as an SMS and a WhatsApp
message to the specified number.

Args:
event (object): An event object containing the request data.
"""
to = event.data["url"]["query"]["to"]
# Send SMS text via Twilio
message = t.messages.create(
from_=FROM_PHONE_NUMBER,
to=to,
body="This is an AutoKitteh demo message, meow!",
)
print(f"SMS message sent: {message.sid}")

# Send a WhatsApp message to the same number
whatsapp_message = t.messages.create(
from_="whatsapp:" + FROM_PHONE_NUMBER,
to="whatsapp:" + to,
body="This is an AutoKitteh demo message, meow!",
)
print(f"WhatsApp message sent: {whatsapp_message.sid}")