Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python
- # -*- coding: utf-8 -*-
- #
- # Simple Bot to reply to Telegram messages
- # This program is dedicated to the public domain under the CC0 license.
- import urllib
- import os
- import logging
- import urllib
- import telegram
- from telegram.error import NetworkError, Unauthorized
- from time import sleep
- command_list = dict()
- command_list['/start'] = 'bot.sendMessage(chat_id=chat_id, text="Hi, to view a list of availible commands type /help")'
- command_list['/help'] = 'bot.sendMessage(chat_id=chat_id, text = "/paint - to repaint your photo in a Red Vineyard style")'
- command_list['/paint'] = 'bot.sendMessage(chat_id=chat_id, text="Send me the photo (like a file)");' \
- 'photo, photo_url = get_picture(bot, update_id);' \
- 'download_picture(photo, photo_url);' \
- 'computing_pictures.append(photo);' \
- 'compute_image(photo, bot, chat_id)'
- computing_pictures = []
- def main():
- # Telegram Bot Authorization Token
- bot = telegram.Bot('237691898:AAGrl5OxVqzPrR-X9YMI0705fCsZVAt1jyk')
- # get the first pending update_id, this is so we can skip over it in case
- # we get an "Unauthorized" exception.
- try:
- update_id = bot.getUpdates()[0].update_id
- except IndexError:
- update_id = None
- logging.basicConfig(format='%(asctime)s - %(name)s - %(levelname)s - %(message)s')
- while True:
- try:
- update_id = echo(bot, update_id)
- except NetworkError:
- sleep(1)
- except Unauthorized:
- # The user has removed or blocked the bot.
- update_id += 1
- def echo(bot, update_id):
- # Request updates after the last update_id
- for update in bot.getUpdates(offset=update_id, timeout=10):
- # chat_id is required to reply to any message
- chat_id = update.message.chat_id
- update_id = update.update_id + 1
- message = update.message.text
- message = message.split()
- check_for_pictures(bot, chat_id)
- if message and message[0][0] != '/':
- # Reply to the message
- bot.sendMessage(chat_id=chat_id, text='Please use command /start to get started')
- elif message and message[0] in command_list.keys():
- exec(command_list[message[0]])
- elif message:
- bot.sendMessage(chat_id=chat_id, text='Unknown command, for the list of availible commands type /help')
- return update_id
- def get_picture(bot, update_id):
- for update in bot.getUpdates(offset=update_id, timeout=600):
- # chat_id is required to reply to any message
- chat_id = update.message.chat_id
- update_id = update.update_id + 1
- file = update.message.document
- if file:
- file = bot.getFile(update.message.document.file_id)
- return (file.file_id, file.file_path)
- else:
- bot.sendMessage(chat_id=chat_id, text="This is not a photo")
- main()
- def download_picture(picture, picture_url):
- os.makedirs(picture)
- urllib.urlretrieve(picture_url, picture +'/' + "picture.jpg")
- def check_for_pictures(bot, chat_id):
- for picture in computing_pictures:
- if os.path.isfile(picture + '/painted_picture.jpg'):
- bot.send_photo(chat_id=chat_id, photo=open(picture + '/painted_picture.jpg'))
- os.remove(picture + '/picture.jpg')
- os.remove(picture + '/painted_picture.jpg')
- os.rmdir(picture)
- computing_pictures.pop(computing_pictures.index(picture))
- def compute_image(picture, bot, chat_id):
- bot.sendMessage(chat_id=chat_id, text="Computation has started, wait for a little while")
- os.system("python neural_artistic_style.py --subject " + picture + '/picture.jpg --style style.jpg --iterations 100 --output ' + picture + "/painted_picture.png")
- bot.send_photo(chat_id=chat_id, photo=open(picture + '/painted_picture.png'))
- os.remove(picture + '/picture.jpg')
- os.remove(picture + '/painted_picture.png')
- os.rmdir(picture)
- if __name__ == '__main__':
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement