DEV Community

Cover image for Send an SMS with Guile and Twilio API
Red Nose Hacker
Red Nose Hacker

Posted on

Send an SMS with Guile and Twilio API

These last days, I tried to send an SMS with a Guile script.
TL;DR: I succeeded!

Since the code is just a simple HTTP POST request, the longest part should have been the creation of the Twilio account (with a Regulatory Bundle and a phone number).

But in the end, it took me about one evening[1] to set up a development environment with the right dependencies, and then two more evenings to figure out which HTTP headers were needed.

Anyway, here is the script:

;; send-happiness.scm

(use-modules (web client)
             (web http)
             (ice-9 receive)
             (ice-9 iconv)
             (gcrypt base64))

(define TWILIO_ACCOUNT_SID "your-account-sid")
(define TWILIO_ACCOUNT_TOKEN "your-account-token")
(define TWILIO_PHONE_NUMBER "your-twilio-phone-number")

(define TWILIO_CREDENTIALS
  (base64-encode
   (string->bytevector
    (simple-format #f "~A:~A" TWILIO_ACCOUNT_SID TWILIO_ACCOUNT_TOKEN) "utf-8"))

(define RECIPIENT_PHONE_NUMBER "a-phone-number")
(define TEXT_MESSAGE "Wish you so much happyness !")

(http-post
 (simple-format
  #f 
  "https://api.twilio.com/2010-04-01/Accounts/~A/Messages"
  TWILIO_ACCOUNT_SID)
 #:headers `((content-type . (application/x-www-form-urlencoded (charset . "utf-8"))
             (authorization . (basic . ,TWILIO_CREDENTIALS)))
 #:body (simple-format #f "Body=~A&To=~A&From=~A"
                       TEXT_MESSAGE
                       RECIPIENT_PHONE_NUMBER
                       TWILIO_PHONE_NUMBER))
Enter fullscreen mode Exit fullscreen mode

And how to execute it :

$ guix shell guile gnutls guile-gcrypt -- guile send-happiness.scm

All phone numbers must start with the country indicator. For example +33 for a French number.

[1] the time unit of my Guile tinkering

Thank you very much for reading this article!

Don't hesitate to give me your opinion, suggest an idea for improvement, or ask a question! To do so : contact me.

Don't miss out on the next ones...

And more importantly, share this blog and tell your friends it's the best blog in the history of Free Software! No kidding!

Top comments (0)