Skip to content

Instantly share code, notes, and snippets.

@devStepsize
devStepsize / twilio_client_set_up.py
Last active April 21, 2016 21:41
Code snippets giving an example on how to start the Twilio client with given credendials.
from twilio.rest import TwilioRestClient
# find the following at https://www.twilio.com/user/account
account = "ACXXXXXXXXXXXXXXXXX"
token = "YYYYYYYYYYYYYYYYYY"
client = TwilioRestClient(account, token)
@devStepsize
devStepsize / twilio_make_phone_call.py
Last active April 22, 2016 01:39
Method to make a phone call with Twilio. The full list of available options can be found here: https://twilio-python.readthedocs.org/en/latest/api/rest/resources.html#calls
call = client.calls.create(
to="9991231234",
from_="9991231234",
url="http://twimlets.com/holdmusic?Bucket=com.twilio.music.ambient"
)
@devStepsize
devStepsize / twilio_call_operations.py
Created April 21, 2016 21:40
Various ways to operate with a call resources. Retrieve, delete, or just manipulate.
sid = "CA12341234"
# Delete call record
client.calls.delete(sid)
# Retrieve phone call
call = client.calls.get(sid)
# Access call resources
call.notifications.list()
@devStepsize
devStepsize / twilio_finding_live_calls.py
Created April 21, 2016 21:50
Various methods to modify live calls with Twilio
calls = client.calls.list(status=Call.IN_PROGRESS)
for c in calls:
# Redirect the calls
c.route(
"http://twimlets.com/holdmusic?Bucket=com.twilio.music.ambient",
method="POST"
)
# Or just slam the phones down
@devStepsize
devStepsize / slack_webhook_post.py
Last active December 14, 2024 22:20
POST a JSON payload to a Slack Incoming Webhook using Python requests
'''
This is an example of how to send data to Slack webhooks in Python with the
requests module.
Detailed documentation of Slack Incoming Webhooks:
https://api.slack.com/incoming-webhooks
'''
import json
import requests
@devStepsize
devStepsize / twilio_twiML_response_creation.py
Created April 21, 2016 22:09
Various example on how to compose valid XML TwiML Responses (calls, message and sms)
from twilio import twiml
r = twiml.Response()
# Lots of options here for you to play with
r.say("University of Sussex #1")
r.play("https://api.twilio.com/cowbell.mp3", loop=42)
# For messages
m = r.message("Hello MMS Monkey!", sender="+14155551234")
@devStepsize
devStepsize / slack_webhook_json_fields.md
Created April 21, 2016 22:16
The following fields can be sent in your JSON payload when creating Incoming Webhooks with the Slack API

Fields you can send in your JSON payload:

  • text – the message to be posted to Slack. To send a link, enclose the URL in <> angle brackets. To display hyperlinked text, use the pipe character like so <https://slack.com|slack>
  • username – override the default identity you've set up
  • icon_url – override the bot icon
  • icon_emoji – override the bot icon with an emoji (e.g. "icon_emoji": ":ghost:")
  • channel – override the default channel you've set up (e.g. "channel": "#other-channel")
@devStepsize
devStepsize / slack_slash_cmd.py
Created April 21, 2016 23:12
Server-side logic to handle a Slack slash command using Python and Flask
'''
This is an example of the server-side logic to handle slash commands in
Python with Flask.
Detailed documentation of Slack slash commands:
https://api.slack.com/slash-commands
Slash commands style guide:
https://medium.com/slack-developer-blog/slash-commands-style-guide-4e91272aa43a#.6zmti394c
'''
@devStepsize
devStepsize / smiley_emoji.py
Created April 21, 2016 23:20
Unicode for smiley emoji
u'\U0001f604'
@devStepsize
devStepsize / twilio_flask_respond_sms.py
Last active April 22, 2016 23:14
Simple example of a Flask endpoint that processes incoming messages redirected by Twilio and answers with a text. Only supports plain text.
from twilio import twiml
@app.route('/endpoint', methods=['GET'])
def process_sms():
body = request.args.get('Body', None)
msg = "thanks for texting me"
r = twiml.Response()
r.message(msg=msg, sender=TWILIO_NUMBER)
return str(r)