Package: reach-talkylabs
reach-pythonDocumentationThe documentation for the Reach@Talkylabs API can be foundhere.The Python library documentation can be foundhere.Versionsreach-pythonuses a modified version ofSemantic Versioningfor all changes.See this documentfor details.Supported Python VersionsThis library supports the following Python implementations:Python 3.7Python 3.8Python 3.9Python 3.10Python 3.11InstallationInstall from PyPi usingpip, a
package manager for Python.pip3installreach-talkylabsIf pip install fails on Windows, check the path length of the directory. If it is greater 260 characters then enableLong Pathsor choose other shorter location.Don't have pip installed? Try installing it, by running this from the command
line:curlhttps://bootstrap.pypa.io/get-pip.py|pythonOr, you candownload the source code
(ZIP)forreach-python, and then run:python3setup.pyinstallInfoIf the command line gives you an error message that says Permission Denied, try running the above commands withsudo(e.g.,sudo pip3 install reach-talkylabs).Test your installationTry sending yourself an SMS message. Save the following code sample to your computer with a text editor. Be sure to update thesrcphone number with one that you verified in the web application. Thedestphone number will be your own mobile phone.fromtalkylabs.reach.restimportReachClientapi_user="XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"api_key="your_api_key"client=ReachClient(api_user,api_key)message=client.messaging.messaging_items.send(dest="+15558675309",src="+15017250604",body="Hello from Python!")print(message)Save the file assend_sms.py. In the terminal,cdto the directory containing the file you just saved then run:python3send_sms.pyAfter a brief delay, you will receive the text message on your phone.WarningIt's okay to hardcode your credentials when testing locally, but you should use environment variables to keep them secret before committing any code or deploying to production.Use the helper libraryAPI CredentialsThe client needs your Reach credentials. You can either pass these directly to the constructor (see the code below) or via environment variables.Authenticating with Account SID and Auth Token:fromtalkylabs.reach.restimportReachClientapi_user="XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"api_key="your_api_key"client=ReachClient(api_user,api_key)Alternatively, aClientconstructor without these parameters will
look forREACH_TALKYLABS_API_USERandREACH_TALKYLABS_API_KEYvariables inside the
current environment.We suggest storing your credentials as environment variables. Why? You'll never
have to worry about committing your credentials and accidentally posting them
somewhere public.fromtalkylabs.reach.restimportReachClientclient=ReachClient()Iterate through recordsThe library automatically handles paging for you. Collections, such ascallsandmessages, havelistandstreammethods that page under the hood. With bothlistandstream, you can specify the number of records you want to receive (limit) and the maximum size you want each page fetch to be (page_size). The library will then handle the task for you.listeagerly fetches all records and returns them as a list, whereasstreamreturns an iterator and lazily retrieves pages of records as you iterate over the collection. You can also page manually using thepagemethod.Use thelistmethodfromtalkylabs.reach.restimportReachClientapi_user="XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"api_key="your_api_key"client=ReachClient(api_user,api_key)forsmsinclient.messaging.messaging_items.list():print(sms.dest)Asynchronous API RequestsBy default, the Client will make synchronous requests to the API. To allow for asynchronous, non-blocking requests, we've included an optional asynchronous HTTP client. When used with the Client and the accompanying*_asyncmethods, requests made to the API will be performed asynchronously.fromtalkylabs.reach.http.async_http_clientimportAsyncReachHttpClientfromtalkylabs.reach.restimportReachClientasyncdefmain():api_user="XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"api_key="your_api_key"http_client=AsyncReachHttpReachClient()client=ReachClient(api_user,api_key,http_client=http_client)message=awaitclient.messaging.messaging_items.send_async(dest="+12316851234",src="+15555555555",body="Hello there!")asyncio.run(main())Enable Debug LoggingLog the API request and response data to the console:importloggingclient=ReachClient(api_user,api_key)logging.basicConfig()client.http_client.logger.setLevel(logging.INFO)Log the API request and response data to a file:importloggingclient=ReachClient(api_user,api_key)logging.basicConfig(filename='./log.txt')client.http_client.logger.setLevel(logging.INFO)Handling Exceptionsreach-pythonexports an exception class to help you handle exceptions that are specific to Reach methods. To use it, importReachRestExceptionand catch exceptions as follows:fromtalkylabs.reach.restimportReachClientfromtalkylabs.reach.base.exceptionsimportReachRestExceptionapi_user="XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"api_key="your_api_key"client=ReachClient(api_user,api_key)try:message=client.messaging.messaging_items.send(dest="+12316851234",src="+15555555555",body="Hello there!")exceptReachRestExceptionase:print(e)Other advanced examplesLearn how to create your own custom HTTP clientDocker ImageTheDockerfilepresent in this repository and its respectivetalkylabs/reach-pythonDocker image are currently used by us for testing purposes only.Getting helpIf you've found a bug in the library or would like new features added, go ahead and open issues or pull requests against this repo!