Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
okanten committed Feb 5, 2021
0 parents commit ad7878e
Show file tree
Hide file tree
Showing 6 changed files with 201 additions and 0 deletions.
22 changes: 22 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
MIT License

Copyright (c) 2021 Ola Kanten

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

36 changes: 36 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
# Talkmore

Uses requests to interact with the talkmore web interface.

Allows for sending sms, sending sms to all registered contacts, as well as retrieving contacts, and adding contacts.

## Examples

```python
talkmore = Talkmore('phone_number', 'password')
talkmore.login()
talkmore.send_sms('receiver', 'message')

# Contacts
contacts = talkmore.get_contacts()
talkmore.add_contact('first_name', 'last_name', 'phone_number')
talkmore_as = talkmore.find_contact_id_by_phone_number('91509915')
talkmore.delete_contact(talkmore_as)

# Delete multiple contacts
contacts = ['91509915', '02800']
talkmore.delete_contacts(contacts)

# Send sms to multiple numbers
contacts = ['91509915', '02800']
talkmore.send_sms(contacts, 'message')

# Send sms to all contacts
talkmore.send_sms_to_all_contacts('message')

# Alternatively, include yourself
talkmore.send_sms_to_all_contacts('message', send_to_self=True)

```


2 changes: 2 additions & 0 deletions setup.cfg
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[metadata]
description-file = README.md
29 changes: 29 additions & 0 deletions setup.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
from distutils.core import setup
setup(
name = 'talkmore',
packages = ['talkmore'],
version = '0.1',
license='MIT',
description = 'Send sms, add and read contacts with through the talkmore web interface',
author = 'Ola Kanten',
author_email = 'okgit@protonmail.com',
url = 'https://github.com/okanten/talkmore',
download_url = 'https://github.com/okanten/talkmore/archive/v_01.tar.gz',
keywords = ['talkmore', 'talkmore sms', 'talkmore-sms', 'sms', 'norge'],
install_requires=[
'requests',
],
classifiers=[
'Development Status :: 4 - Beta',
'Intended Audience :: Developers',
'Topic :: Software Development :: Build Tools',
'License :: OSI Approved :: MIT License',
'Programming Language :: Python :: 3',
'Programming Language :: Python :: 3.4',
'Programming Language :: Python :: 3.5',
'Programming Language :: Python :: 3.6',
'Programming Language :: Python :: 3.7',
'Programming Language :: Python :: 3.8',
'Programming Language :: Python :: 3.9',
],
)
1 change: 1 addition & 0 deletions talkmore/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from talkmore.talkmore import Talkmore
111 changes: 111 additions & 0 deletions talkmore/talkmore.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
import requests
import json
import re

class UserNotLoggedIn(Exception):
pass

class ContactDeletionException(Exception):
pass

class Talkmore:

def __init__(self, username, password):
self.session = requests.session()
self.login_data = {'item': '', 'scController': 'Login', 'scAction': 'Login', 'redirectUrl': ''}
self.login_data.update([('phoneNumber', username), ('password', password)])
self.username = username

def login(self):
self.session.post('https://talkmore.no/login', data=self.login_data)
return self.is_logged_in()

def is_logged_in(self):
return ('accesstoken-prod' in self.session.cookies.keys())

def send_sms(self, recipients, message):
if not self.is_logged_in():
raise UserNotLoggedIn('User not logged in. Call .login() first')
if isinstance(recipients, str):
recipients = [recipients]
if len(message) >= 1600:
first_message = message[:1599]
self.send_sms(recipients, first_message)
next_message = message[1599:]
self.send_sms(recipients, next_message)
data = {"Recipients": recipients, "Message": message}
r = self.session.post('https://talkmore.no/tmapi/sms/send', data=data)
return (r.status_code == 200)

def send_sms_to_all_contacts(self, message, send_to_self=False):
contacts = self.get_contacts()
clean_contacts = list()
for contact in contacts:
clean_contacts.append(contact.get('phone'))
if send_to_self is False:
try:
clean_contacts.remove(self.username)
except:
pass
return self.send_sms(clean_contacts, message)

def add_contact(self, first_name, last_name, phone_number):
data = {
'scController': 'SendSms',
'scAction': 'AddSmsContact',
'firstName': first_name,
'lastName': last_name,
'phoneNumber': phone_number
}
r = self.session.post('https://talkmore.no/minesider/sms', data=data)
return r

def get_contacts(self):
r = self.add_contact('Captain', 'Placeholder', self.username)
rendered_contact_list = r.json().get('RenderedContactList')
c_id = self.find_contact_id_by_name_and_phone_number('Captain Placeholder', self.username, rendered_contact_list)
self.delete_contact(c_id)
return r.json().get('ContactItems')

def find_contact_id_by_name_and_phone_number(self, name, phone_number, contact_list):
c_id = re.search(f'label for="(.*)">{name} - {phone_number}</label>', contact_list).group(1)
return c_id

def find_contact_id_by_phone_number(self, phone_number, contact_list=None):
if contact_list is None:
contact_list = self.get_contacts()
contact_ids = list()
for div in contact_list.split('</div>'):
if phone_number in div:
contact_id = re.search('id="(.*)"', div).group(1)
contact_ids.append(contact_id)
return contact_ids

def delete_contact(self, contact_id):
if isinstance(contact_id, list()):
contact_id = contact_id[0]
data = {
'scController': 'SendSms',
'scAction': 'DeleteSmsContact',
'contactId': contact_id
}
r = self.session.post('https://talkmore.no/minesider/sms', data=data)
return r.json().get('Success')

def delete_contacts(self, contacts):
for contact in contacts:
self.delete_contact(contact)

def delete_duplicate_contacts(self, contact_list, phone_number):
for div in contact_list.split('</div>'):
if phone_number in div:
contact_id = re.search('id="(.*)"', div).group(1)
if not self.delete_contact(contact_id):
raise ContactDeletionException(f'Failed to delete contact {contact_id}')
return True

if __name__ == '__main__':
talkmore = Talkmore('99999999', 'password')
talkmore.login()
talkmore.send_sms('98989898', 'Message goes here')

0 comments on commit ad7878e

Please sign in to comment.