User:Jasper van Loenen/Prototyping/message

From XPUB & Lens-Based wiki

This form takes a name and a message as its input and uses Prowl [1] and Prowlpy [2] to send the message to my iPhone.

You'll find an active version of the form over here.

message.cgi

#!/usr/bin/env python
#-*- coding:utf-8 -*-

import cgi
import cgitb; cgitb.enable()
query = cgi.FieldStorage()
msg = query.getvalue("msg", "") #getvalue is both GET and POST ($_REQUEST in PHP)
usr = query.getvalue("usr", "") #getvalue is both GET and POST ($_REQUEST in PHP)

#if a message and username have been found, the form was filled so import the Prowl library and send the message
if msg!="your message" and msg!="" and usr!="your name" and usr!="":
    import prowlpy
    apikey = '1234512345123451234512345123451234512345' #needs a valid Prowl API key, which you can generate on the website
    p = prowlpy.Prowl(apikey)
    try:
        p.add('CGI',usr,msg, 1, None)
        print "Location: done.cgi\n\n"; #landing page for successful sending
    except Exception,msg:
        print "Location: fail.cgi\n\n"; #landing page for failures :(

else: #the form wasn't filled (correctly) so show it (again)
    print "Content-type: text/html"
    print ""
    print """
    <html>
    <head>
    <style>
    textarea {{
        font-size: 32px;
        width: 100%;
        border: 4px solid black;
        height: 400px;        
    }}
    </style>
    </head>
    <body>
    <form action="" method="POST">
        <textarea name="usr" onclick="this.value=''" onfocus="this.value=''">your name</textarea>
        <textarea name="msg" onclick="this.value=''" onfocus="this.value=''">your message</textarea>
        <input type="submit">
    </form>
    </body>
    </html>
    """
  1. Prowl, IOS push notification API - https://www.prowlapp.com/
  2. Prowlpy, Python library for Prowl - https://github.com/jacobb/prowlpy