#!/usr/bin/python
# Developed by Shantanu Goel. http://tech.shantanugoel.com/
import socket
import argparse
import time
#Defaults
port = 52235
receiver_no = "1234567890"
sender_no = "1234567890"
receiver = "Receiver"
sender = "Sender"
epochtime = time.mktime(time.localtime())
def buildMessage(args):
body = "<?xml version=\"1.0\" encoding=\"utf-8\"?>"\
"<s:Envelope xmlns:s=\"http://schemas.xmlsoap.org/soap/envelope/\" s:encodingStyle=\"http://schemas.xmlsoap.org/soap/encoding/\">" \
"<s:Body>" \
" <u:AddMessage xmlns:u=\"urn:samsung.com:service:MessageBoxService:1\">" \
" <MessageType>text/xml</MessageType>" \
" <MessageID>MessageId</MessageID>" + "<Message>" \
"<Category>Incoming Call</Category>" \
"<DisplayType>Maximum</DisplayType>" \
"<CallTime>" + "<Date>" + time.strftime('%Y-%m-%d', time.localtime(args.time)) + "</Date>" \
"<Time>" + time.strftime('%H:%M:%S', time.localtime(args.time)) + "</Time>" \
"</CallTime>" \
"<Callee>" \
"<Number>" + args.receiver_no + "</Number>" \
"<Name>" + args.receiver + "</Name>" \
"</Callee>" \
"<Caller>" \
"<Number>" + args.sender_no + "</Number>" \
"<Name>" + args.sender + "</Name>" \
"</Caller>" \
"<Body>" + args.msg + "</Body>" \
"</Message>" + " </u:AddMessage>" + " </s:Body>" + "</s:Envelope>";
#print ('Hello World')
host = socket.gethostname()
length = len(body)
header = "POST /PMR/control/MessageBoxService HTTP/1.0\r\n" \
"Content-Type: text/xml; charset=\"utf-8\"\r\n" + "HOST: " + host + "\r\n" \
"Content-Length: " + str(length) + "\r\n" \
"SOAPACTION: \"uuid:samsung.com:service:MessageBoxService:1#AddMessage\"\r\n" \
"Connection: close\r\n" + "\r\n"
message = header + body
print (message)
return message
def sendMessage(args, message):
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM, 0)
s.connect((args.ip, args.port))
#msgbytes = bytes([0x43, 0x44, 0x45, 0x47, 0x48, 0x49])
#s.send(msgbytes)
sent = s.send(message.encode('ascii'))
#sent = s.sendmsg(message)
if (sent <= 0):
print('Error Sending Message')
s.close()
return
recv = s.recv(100000)
print (recv)
s.close()
def main():
parser = argparse.ArgumentParser(description='Send an arbitrary text message to Samsung TVs which is displayed onscreen. Developed by Shantanu Goel (http://tech.shantanugoel.com/) version 1.0', add_help = False)
flags = parser.add_argument_group('Arguments')
flags.add_argument('-i', '--ip', dest = 'ip', default = '192.168.0.40', help = 'Required. IP Address of the TV')
flags.add_argument('-m', '--msg', dest = 'msg', default = 'Telefon-Anruf', help = 'Required. Message body text to be sent to TV')
flags.add_argument('-p', '--port', dest = 'port', default = port, type = int, help = 'Optional. Port on which message should be sent')
flags.add_argument('-t', '--time', dest = 'time', default = epochtime, type = float, help = 'Optional. Receive date and time in epoch/unix format')
flags.add_argument('-r', '--receiver', dest = 'receiver', default = receiver, help = 'Optional. Receiver Name')
flags.add_argument('-x', '--receiverno', dest = 'receiver_no', default = receiver_no, help = 'Optional. Receiver Number')
flags.add_argument('-s', '--sender', dest = 'sender', default = sender, help = 'Optional. Sender Name')
flags.add_argument('-y', '--senderno', dest = 'sender_no', default = sender_no, help = 'Optional. Sender Number')
flags.add_argument('-h', '--help', action='help')
args = parser.parse_args()
message = buildMessage(args)
sendMessage(args, message)
main()