Created
August 27, 2013 18:38
-
-
Save dpq/6357323 to your computer and use it in GitHub Desktop.
Send an sms, parse its parameters, create a document from a template and fax it away! Uses comtube.com
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This small program was written for a friend who had to fax papers granting access to a factory's | |
territory to automobiles and persons mentioned in his SMS messages of a specific format. | |
Don't ask why :) | |
The format of SMS for a car: | |
@<COMTUBE_ACCOUNT> car DD.MM.YY, марка, номер [, driver name, arrival HH:MM, departure HH:MM] | |
The format of SMS for a group of people: | |
@<COMTUBE_ACCOUNT> fio DD.MM.YY, Name 1 [, Name 2 .. Name 8] | |
Replace @<COMTUBE_ACCOUNT> with your account name, e.g. @myacc. How to setup email notifications | |
is left as an excercise to the reader |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python | |
# -*- coding: utf-8 -*- | |
# GMail account username and password (we check Comtube notifications via email) | |
user, pwd = "", "" | |
# Phone number to which send the fax to | |
faxnumber = "" | |
# List of mobile numbers (international without +, e.g. "7916xxxyyzz" for Russian MTS) | |
# from which command SMS are accepted | |
allowed_mobiles = [] | |
from_sms = "" # Email address of the SMS notification system | |
to_fax = "" # Address to which fax attachments are mailed | |
import Image | |
import ImageFont, ImageDraw, ImageOps | |
import email, getpass, imaplib, os, base64, quopri, smtplib | |
import mimetypes | |
from email.MIMEMultipart import MIMEMultipart | |
from email.MIMEBase import MIMEBase | |
from email.MIMEText import MIMEText | |
from email.MIMEAudio import MIMEAudio | |
from email.MIMEImage import MIMEImage | |
from email.Encoders import encode_base64 | |
import os | |
from secret import * | |
# connecting to the gmail imap server | |
m = imaplib.IMAP4_SSL("imap.gmail.com") | |
m.login(user,pwd) | |
m.select("INBOX") | |
resp, items = m.search(None, "ALL") | |
items = items[0].split() # get the email ids | |
os.chdir(os.path.dirname(os.path.abspath(__file__))) | |
for emailid in items: | |
resp, data = m.fetch(emailid, "(RFC822)") # fetching the mail, "`(RFC822)`" means "get the whole stuff", but you can ask for headers only, etc | |
email_body = data[0][1] # getting the mail content | |
mail = email.message_from_string(email_body) # parsing the mail content to get a mail object | |
if mail["From"] == from_sms: | |
number = quopri.decodestring(mail.get_payload()).split("<p>")[2].split(": ")[0].split()[-1] | |
message = quopri.decodestring(mail.get_payload()).split("<p>")[2].split(": ")[-1][1:-1] | |
if number in allowed_mobiles: | |
if message[:3].lower() == "car": | |
fields = message[3:].split(",") | |
if len(fields) < 3: | |
continue | |
date = fields[0] | |
[model, license] = fields[1:3] | |
if len(fields) > 3: | |
driver = fields[3] | |
else: | |
driver = "" | |
if len(fields) > 4: | |
arrival = fields[4] | |
else: | |
arrival = "" | |
if len(fields) > 5: | |
departure = fields[5] | |
else: | |
departure = "" | |
print "Request for a car", date, model, license, driver, arrival, departure | |
img = Image.open("car.png") | |
f = ImageFont.truetype("/usr/share/fonts/truetype/ttf-dejavu/DejaVuSans.ttf", 14) | |
d = ImageDraw.Draw(img) | |
d.text((560, 262), unicode(date,'UTF-8'), font=f, fill="black") | |
d.text((180, 368), unicode(model,'UTF-8'), font=f, fill="black") | |
d.text((206, 411), unicode(driver,'UTF-8'), font=f, fill="black") | |
d.text((656, 371), unicode(license,'UTF-8'), font=f, fill="black") | |
d.text((615, 501), unicode(arrival,'UTF-8'), font=f, fill="black") | |
d.text((690, 501), unicode(departure,'UTF-8'), font=f, fill="black") | |
img.save("outgoing.jpg", "JPEG") | |
elif message[:3].lower() == "fio": | |
fios = message[3:].split(",") | |
print "Request for people", fios | |
if len(fios) < 2 or len(fios) > 11: | |
continue | |
date = fios[0] | |
fios = fios[1:] | |
img = Image.open("person.png") | |
f = ImageFont.truetype("/usr/share/fonts/truetype/ttf-dejavu/DejaVuSans.ttf", 16) | |
d = ImageDraw.Draw(img) | |
d.text((330, 340), unicode(date,'UTF-8'), font=f, fill="black") | |
y = 475 | |
for fio in fios: | |
d.text((170, y), unicode(fio,'UTF-8'), font=f, fill="black") | |
y += 23 | |
img.save("outgoing.jpg", "JPEG") | |
print "Sending fax" | |
to = to_fax | |
gmail_user, gmail_pwd = user, pwd | |
msg = MIMEMultipart() | |
msg['To'] = to | |
msg['From'] = user | |
msg['Subject'] = 'mp ' + faxnumber | |
fp = open("outgoing.jpg", 'rb') | |
imgkk = MIMEImage(fp.read()) | |
imgkk.add_header('Content-Disposition', 'attachment', filename="fax.jpg") | |
fp.close() | |
msg.attach(imgkk) | |
smtpserver = smtplib.SMTP("smtp.gmail.com",587) | |
smtpserver.ehlo() | |
smtpserver.starttls() | |
smtpserver.ehlo() | |
smtpserver.login(gmail_user, gmail_pwd) | |
smtpserver.sendmail(gmail_user, to, msg.as_string()) | |
smtpserver.close() | |
print "Email sent successfully" | |
typ, data = m.search(None, 'ALL') | |
for num in data[0].split(): | |
m.store(num, '+FLAGS', '\\Seen') | |
m.store(num, '+FLAGS', '\\Deleted') | |
m.expunge() | |
try: | |
os.remove("outgoing.jpg") | |
except: | |
pass |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment