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

Modified the README.md to include a second example #40

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from 1 commit
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
48 changes: 45 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@ Documentation soon to be available on Hackage. For now, see [markandrus.github.i

For TwiML, see [twiml-haskell](http://github.com/markandrus/twiml-haskell).

Example
Example 1 - using environment variables
-------

You can create a REST API client and fetch the calls resources as follows
You can create a REST API client, fetch the calls, or send a message as follows:

```hs
{-#LANGUAGE OverloadedStrings #-}
Expand All @@ -38,11 +38,53 @@ main = runTwilio' (getEnv "ACCOUNT_SID")
liftIO $ print calls

-- Send a Message.
let body = PostMessage "+14158059869" "+14158059869" "Oh, hai"
let receivingPhone = "+14158059869"
let sendingPhone = "+14158059869"
let body = PostMessage receivingPhone sendingPhone "Oh, hai"
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What do you think about using the names to and from instead of receivingPhone and sendingPhone? This would better match the REST API.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep, works for me. I'll make the change and update the pull request tomorrow morning.

Aaron

message <- post body
liftIO $ print message
```

Example 2 - using SID and secret embedded in the code
-------

You can create a REST API client and send a message as follows:

```hs
{-#LANGUAGE OverloadedStrings #-}

module Main where
import Twilio.Types.SID
import Control.Monad.IO.Class (liftIO)
import Twilio
import Twilio.Messages
import qualified Data.Text as T
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since only Text is being used, what do you think about going with a simpler import, e.g.

import Data.Text (Text)

Do you mind alphabetizing the imports as well?


parseCredentials :: T.Text -> T.Text -> Maybe Credentials
parseCredentials accountSID authToken =
case parseAuthToken authToken of
Just authToken ->
(case parseSID accountSID of
Just accountSID ->
Just (accountSID, authToken)
Nothing -> Nothing)
Nothing -> Nothing
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here you are pattern matching on the success/failure of the parses to determine the next step. Instead of explicit pattern matching, we can rely on the behavior of Maybe's Functor and Applicative instances to write this more succinctly. For example,

parseCredentials :: Text -> Text -> Maybe Credentials
parseCredentials accountSID authToken =
  (,) <$> parseAccountSID accountSID <*> parseAuthToken authToken

I wonder if such a function should be include the library proper.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, agreed. We saw that you actually have this function built but haven't exported it.

Aaron

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, you're right! And it was written with a less-palatable combination of uncurry and liftM2... the <$>/<*> combination feels nicer, IMO.


main :: IO ()
main =
let accountSID = "youraccountSID"
authToken = "yourauthToken"
in case parseCredentials accountSID authToken of
Just credentialsPassed ->
runTwilio ( credentialsPassed ) $ do
let receivingPhone = "+14158059869"
let sendingPhone = "+14158059869"
let body = PostMessage receivingPhone sendingPhone "Oh, hai"
message <- post body
liftIO $ print message
Nothing -> print "Something bad happened, you have poorly formed credentials."
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you mind using a 2-space indent?

```

Contributing
------------

Expand Down