Skip to content

joeyagreco/pythontextnow

Repository files navigation



pythontextnow logo

Send SMS messages from Python!
A Python wrapper for TextNow.

TextNow Website

PyPi Version
Main Build Last Commit

Table of Contents

Installation

Use the package manager pip to install.

pip install pythontextnow

Usage

Make sure you have the following before you begin:

  • TextNow username
  • SID cookie

For a guide on how to obtain these for your account, check here.

Configure Client

Before you can call any methods, you must first set up your client config.

from pythontextnow import Client

USERNAME = "{your_username}"
SID_COOKIE = "{your_sid_cookie}"

Client.set_client_config(username=USERNAME, sid_cookie=SID_COOKIE)

The ConversationService is how you will perform any action.

It takes a list of phone numbers which define the conversation you would like to perform your actions on.

All phone numbers must be given in E.164 format.

from pythontextnow import ConversationService

PHONE_NUMBER_1 = "{some_phone_number}"
PHONE_NUMBER_2 = "{some_other_phone_number}"
conversation_service = ConversationService(conversation_phone_numbers=[PHONE_NUMBER_1, PHONE_NUMBER_2])

Get Messages

The get_messages() method will return a generator object.

This will return the messages from most -> least recent.

You can call next() with the returned generator each time you want to get the next group of messages.

messages_generator = conversation_service.get_messages()

messages = next(messages_generator)

You can also use a for loop to get all messages in a conversation.

messages_generator = conversation_service.get_messages()

all_messages = list()
for message_list in messages_generator:
    all_messages += message_list

You can specify how many messages back you would like to be retrieved by using the num_messages keyword argument.

messages_generator = conversation_service.get_messages(num_messages=10)

last_10_messages = list()
for message_list in messages_generator:
    last_10_messages += message_list

Send a Message

To send a text message, use the send_message() method.

conversation_service.send_message(message="Hello World!")

Send Media

To send media, use the send_media() method.

You can send:

  • Images
  • Videos
  • GIFs
conversation_service.send_message(file_path="C:\\my_media.png")

Mark a Message as Read

To mark a message as read, use the mark_as_read() method.

Mark a single message as read.

# assume you had a Message object saved to the variable "message_obj"

conversation_service.mark_as_read(message=message_obj)

Mark a list of messages as read.

# assume you had a list of Message objects saved to the variable "message_list"

conversation_service.mark_as_read(messages=message_list)

Delete a Message

To delete a message, use the delete_message() method.

Delete a message by its ID.

conversation_service.delete_message(message_id="123456")

Delete a message with its Message object.

# assume you had a Message object saved to the variable "message_obj"

conversation_service.delete_message(message=message_obj)

Delete a Conversation

To delete a conversation, use the delete_conversation() method.

conversation_service.delete_conversation()

Setup

Obtaining Your Username

You will need to know your TextNow username to utilize this library.

This is the same username that you would use to log in.

To find this username:

  • Go to TextNow's messaging page (make sure you are logged in)
  • Click "Settings"
  • Your username will be listed under "Account"

Obtaining Your Cookie

You will need an SID cookie to utilize this library.

To find this cookie:

In Chrome:

  • Access Developer Tools in your browser
  • Click on the "Application" tab
  • Click on the "Network" tab
  • Find a request under "Fetch/XHR"
  • Go to the "Headers" tab in the given request
  • Find the "Request Headers" section
  • Locate the "Cookie" field

In Firefox

  • Access Developer Tools in your browser
  • Click on the "Network" tab
  • Find a request under "XHR"
  • Go to the "Headers" tab in the given request
  • Find the "Request Headers" section
  • Locate the "Cookie" field
  • Locate the "connect.sid" field, the value will be your SID cookie

Running Tests

To run tests, run the following command:

  pytest

Contributing

Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.

Please make sure to update tests as appropriate.

License

MIT