2

Twilio docs for Java lib

MVN for this lib

I'm trying to use Twilio from Clojure. Pretty new to Clojure dev, so I'm trying to get to grips with importing libs, in general.

My project.clj looks like this:

(defproject magical-new-project "0.1.0-SNAPSHOT"
  :description "FIXME: write description"
  :url "http://example.com/FIXME"
  :license {:name "EPL-2.0 OR GPL-2.0-or-later WITH Classpath-exception-2.0"
            :url "https://www.eclipse.org/legal/epl-2.0/"}
  :dependencies [[org.clojure/clojure "1.10.0"]
                 [com.twilio.sdk/twilio "7.47.5"]]
  :repl-options {:init-ns magical-new-project.core})

I think I'm correctly importing the Twilio Java lib, but I'm not sure.

My core.clj looks like:

(ns magical-new-project.core
  (:import [com.twilio Twilio]
           ;[com.twilio.http Request Response]
           [com.twilio.rest.api.v2010.account Message]
           [com.twilio.rest.api.v2010.account Call]
           [com.twilio.type PhoneNumber]))

(defn foo
  "I don't do a whole lot."
  [x]
  (println x "Peacefully disengage, World!"))

As far as I can tell, this should be correctly importing the Twilio class shown here.

So then I try (from REPL) to initialize the Twilio object that I hope I have successfully imported, but it fails.

$ lein repl
nREPL server started on port 62356 on host 127.0.0.1 - nrepl://127.0.0.1:62356
REPL-y 0.4.3, nREPL 0.6.0
Clojure 1.10.0
OpenJDK 64-Bit Server VM 12.0.1+12
    Docs: (doc function-name-here)
          (find-doc "part-of-name-here")
  Source: (source function-name-here)
 Javadoc: (javadoc java-object-or-class-here)
    Exit: Control+D or (exit) or (quit)
 Results: Stored in vars *1, *2, *3, an exception in *e

magical-new-project.core=> (Twilio. "My API or Account SID goes here" "My AUTH_TOKEN or API secret goes here")
Syntax error (IllegalArgumentException) compiling new at (form-init14687807219308370487.clj:1:1).
No matching ctor found for class com.twilio.Twilio

As far as I can tell, the No matching ctor thing means there is no constructor function that accepts the arguments I'm presenting, but line 39 of the Twilio.java file seems to take two strings, and then if you look here, you'll see that I'm sending the right arguments (the ACCOUNT_SID and the AUTH_TOKEN).

At the moment, I'm not sure whether I'm (1) correctly importing the Twilio class, (2) adding the dependencies to the project correctly, (2) correctly using the REPL, or (4) correctly using the Twilio SDK. Maybe I'm doing all of these incorrectly.

I would really appreciate any help or advice that you could afford me.

7
  • 1
    the function you linked to is not a constructor but a static function on the class. You call it like (Twilio/init "foo" "bar"). Your syntax is correct if it were actually a constructor. aside: it is amazing that they designed their java api so that it can only ever be a static singleton. wow. Commented Mar 4, 2020 at 22:54
  • @bfabry: You, my friend, are a generous genius. You appear to have solved my problem and helped me take a step along the path to Clojure competence. Commented Mar 4, 2020 at 23:01
  • Many thanks for your help. Commented Mar 4, 2020 at 23:01
  • Thanks @AlanThompson. I’ve bookmarked that and will try it out. Commented Mar 5, 2020 at 2:03
  • @bfabry perhaps you could make your comment an actual answer and Matt Lally could mark that as accepted one? Commented Mar 5, 2020 at 7:53

1 Answer 1

3

the function you linked to is not a constructor but a static function on the class. You call a static function on a class in clojure like (Twilio/init "foo" "bar"). Your syntax is correct if it were actually a constructor.

Thanks @juraj. I wasn't sure if this actually qualified as an answer :-)

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.