Guest User

Untitled

a guest
Oct 22nd, 2017
35
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #!/usr/bin/python
  2.  
  3. import telegram
  4. import argparse
  5. import sys
  6. import logging
  7.  
  8. import urllib2
  9.  
  10. logging.basicConfig(filename='/home/pi/mqtt/example.log',filemode='a',format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',level=logging.INFO)
  11. logging.info("logging %s",sys.argv)
  12.    
  13. if __name__ == "__main__":
  14.  
  15.     try:
  16.         # { "message":"test" }
  17.         parser = argparse.ArgumentParser(description='chatid and message')
  18.         requiredNamed = parser.add_argument_group('required named arguments')
  19.         requiredNamed.add_argument('-i', help='chatid', required=False,metavar='')
  20.         requiredNamed.add_argument('-m', help='message', required=False,metavar='')
  21.         requiredNamed.add_argument('-p', help='picture', required=False,metavar='')
  22.         args = parser.parse_args()
  23.  
  24.         logging.info("chat_id: %s | message: %s",args.i,args.m)
  25.         bot = telegram.Bot(token='APIKEY')
  26.         logger = logging.getLogger()
  27.         logger.setLevel(logging.DEBUG)
  28.        
  29.         try:       
  30.             if args.p is not None:
  31.                 logging.info("sending photo '%s' with caption '%s'",args.p,args.m)
  32.                 try:
  33.                     with open('snapshot.jpg','wb') as f:
  34.                         f.write(urllib2.urlopen(args.p).read())
  35.                         f.close()
  36.                     bot.sendPhoto(chat_id=args.i, caption=args.m, photo=open('snapshot.jpg', 'rb'))
  37.                 except Exception as e:
  38.                     bot.sendMessage(chat_id=args.i, text=args.m)
  39.             else:
  40.                 logging.info("sending text '%s'",args.m)
  41.                 bot.sendMessage(chat_id=args.i, text=args.m)
  42.         except Exception as e:
  43.             logger.exception("message1")
  44.            
  45.         #updater.stop()
  46.     except Exception as e:
  47.         logging.exception("message2")
RAW Paste Data