Advertisement
Guest User

Untitled

a guest
May 27th, 2016
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.12 KB | None | 0 0
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. #
  4. # Simple Bot to reply to Telegram messages
  5. # This program is dedicated to the public domain under the CC0 license.
  6.  
  7. import urllib
  8. import os
  9. import logging
  10. import urllib
  11. import telegram
  12. from telegram.error import NetworkError, Unauthorized
  13. from time import sleep
  14.  
  15. command_list = dict()
  16. command_list['/start'] = 'bot.sendMessage(chat_id=chat_id, text="Hi, to view a list of availible commands type /help")'
  17. command_list['/help'] = 'bot.sendMessage(chat_id=chat_id, text = "/paint - to repaint your photo in a Red Vineyard style")'
  18. command_list['/paint'] = 'bot.sendMessage(chat_id=chat_id, text="Send me the photo (like a file)");' \
  19.                          'photo, photo_url = get_picture(bot, update_id);' \
  20.                          'download_picture(photo, photo_url);' \
  21.                          'computing_pictures.append(photo);' \
  22.                          'compute_image(photo, bot, chat_id)'
  23.  
  24. computing_pictures = []
  25.  
  26. def main():
  27.     # Telegram Bot Authorization Token
  28.     bot = telegram.Bot('237691898:AAGrl5OxVqzPrR-X9YMI0705fCsZVAt1jyk')
  29.  
  30.     # get the first pending update_id, this is so we can skip over it in case
  31.     # we get an "Unauthorized" exception.
  32.     try:
  33.         update_id = bot.getUpdates()[0].update_id
  34.     except IndexError:
  35.         update_id = None
  36.  
  37.     logging.basicConfig(format='%(asctime)s - %(name)s - %(levelname)s - %(message)s')
  38.  
  39.     while True:
  40.         try:
  41.             update_id = echo(bot, update_id)
  42.         except NetworkError:
  43.             sleep(1)
  44.         except Unauthorized:
  45.             # The user has removed or blocked the bot.
  46.             update_id += 1
  47.  
  48.  
  49. def echo(bot, update_id):
  50.  
  51.     # Request updates after the last update_id
  52.     for update in bot.getUpdates(offset=update_id, timeout=10):
  53.         # chat_id is required to reply to any message
  54.         chat_id = update.message.chat_id
  55.         update_id = update.update_id + 1
  56.         message = update.message.text
  57.         message = message.split()
  58.  
  59.         check_for_pictures(bot, chat_id)
  60.  
  61.         if message and message[0][0] != '/':
  62.             # Reply to the message
  63.             bot.sendMessage(chat_id=chat_id, text='Please use command /start to get started')
  64.  
  65.         elif message and message[0] in command_list.keys():
  66.                 exec(command_list[message[0]])
  67.  
  68.         elif message:
  69.             bot.sendMessage(chat_id=chat_id, text='Unknown command, for the list of availible commands type /help')
  70.  
  71.  
  72.  
  73.     return update_id
  74.  
  75. def get_picture(bot, update_id):
  76.      for update in bot.getUpdates(offset=update_id, timeout=600):
  77.         # chat_id is required to reply to any message
  78.         chat_id = update.message.chat_id
  79.         update_id = update.update_id + 1
  80.         file = update.message.document
  81.         if file:
  82.             file = bot.getFile(update.message.document.file_id)
  83.             return (file.file_id, file.file_path)
  84.         else:
  85.             bot.sendMessage(chat_id=chat_id, text="This is not a photo")
  86.             main()
  87.  
  88. def download_picture(picture, picture_url):
  89.     os.makedirs(picture)
  90.     urllib.urlretrieve(picture_url, picture +'/' + "picture.jpg")
  91.  
  92.  
  93. def check_for_pictures(bot, chat_id):
  94.     for picture in computing_pictures:
  95.         if os.path.isfile(picture + '/painted_picture.jpg'):
  96.             bot.send_photo(chat_id=chat_id, photo=open(picture + '/painted_picture.jpg'))
  97.             os.remove(picture + '/picture.jpg')
  98.             os.remove(picture + '/painted_picture.jpg')
  99.             os.rmdir(picture)
  100.             computing_pictures.pop(computing_pictures.index(picture))
  101.  
  102.  
  103. def compute_image(picture, bot, chat_id):
  104.     bot.sendMessage(chat_id=chat_id, text="Computation has started, wait for a little while")
  105.     os.system("python neural_artistic_style.py --subject " + picture + '/picture.jpg --style style.jpg --iterations 100 --output ' + picture + "/painted_picture.png")
  106.     bot.send_photo(chat_id=chat_id, photo=open(picture + '/painted_picture.png'))
  107.     os.remove(picture + '/picture.jpg')
  108.     os.remove(picture + '/painted_picture.png')
  109.     os.rmdir(picture)
  110.  
  111.  
  112. if __name__ == '__main__':
  113.     main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement