-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathImageBot.py.save
124 lines (100 loc) · 4.02 KB
/
ImageBot.py.save
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import logging
import os
from pprint import pprint
from subprocess import run
from telegram.ext import Updater, CommandHandler, MessageHandler, Filters, ConversationHandler
import ImageEngine
# Enable logging
logging.basicConfig(
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s', level=logging.INFO
)
logger = logging.getLogger(__name__)
# Define a few command handlers. These usually take the two arguments update and
# context. Error handlers also receive the raised TelegramError object in error.
def start(update, context):
"""Send a message when the command /start is issued."""
update.message.reply_text('hi!!!')
def shutdown_command(update, context):
"""shutdown the bot process"""
update.message.reply_text(str(getpid()) + " will shutdown")
os.kill(os.getpid(), 2)
def help_command(update, context):
"""Send a message when the command /help is issued."""
update.message.reply_text('send image dumb ape!')
def process_command(update, context):
pass
def pano_command(update, context):
update.message.reply_text('start sending images and send \'end\' to stop and process')
return 'getimg'
def pano_image_recieve(update, context):
if update.message.document is None:
file=update.message.photo[-1].get_file()
else:
file=update.message.document.get_file()
update.message.reply_text("got it")
file.download(custom_path=str(file.file_unique_id)+".jpg")
if 'pano' in context.chat_data:
context.chat_data['pano'] += [str(file.file_unique_id)+".jpg"]
else:
context.chat_data['pano'] = [str(file.file_unique_id)+".jpg"]
def process_pano(update, context):
update.message.reply_text("now wait")
outName=context.chat_data['pano'][0]+'-'+context.chat_data['pano'][-1]+'.jpg'
ptoName=context.chat_data['pano'][0]+'-'+context.chat_data['pano'][-1]+'.pto'
print(context.chat_data['pano'])
print(*context.chat_data['pano'])
log=""
log+=run(['pto_gen', *context.chat_data['pano'], '-o', ptoName], capture_output=True,text=True).stdout
log+=run(['hugin_executor', '-a', ptoName], capture_output=True,text=True).stdout
log+=run(['pano_modify', '--ldr-file=JPG', ptoName, '-o', ptoName], capture_output=True,text=True).stdout
log+=run(['hugin_executor', '-p', outName, '-s', ptoName], capture_output=True,text=True).stdout
with open(ptoName+".log", "r") as f:
f.write(log)
with open(ptoName+".log", "wb") as f:
update.message.reply_document(f)
try:
with open(outName, 'rb') as f:
update.message.reply_photo(f)
update.message.reply_text("there you go!")
except:
update.message.reply_text("couldn't make the pano")
context.chat_data['pano']=[]
return ConversationHandler.END
def on_image_recieve(update, context):
"""saves a file id and name to file"""
if update.message.document is None:
file=update.message.photo[-1].get_file()
else:
file=update.message.document.get_file()
update.message.reply_text("got it")
file.download()
classify_image()
def main():
"""Start the bot."""
updater = Updater("1745807704:AAESSfSoSIoJZH30l4l3wU-QAP-dbQMT8ew", use_context=True)
# Get the dispatcher to register handlers
dp = updater.dispatcher
# on different commands - answer in Telegram
dp.add_handler(CommandHandler("start", start))
dp.add_handler(CommandHandler("help", help_command))
dp.add_handler(CommandHandler("process", process_command))
conv_handler = ConversationHandler(
entry_points=[CommandHandler("pano", pano_command)],
states={
'getimg': [MessageHandler(Filters.document.image | Filters.photo & ~Filters.command, pano_image_recieve), MessageHandler(Filters.text, process_pano)]
},
fallbacks=[],
allow_reentry=True
)
dp.add_handler(conv_handler)
dp.add_handler(MessageHandler(Filters.document.image | Filters.photo & ~Filters.command, on_image_recieve))
# Start the Bot
updater.start_polling()
# Run the bot until you press Ctrl-C or the process receives SIGINT,
# SIGTERM or SIGABRT. This should be used most of the time, since
# start_polling() is non-blocking and will stop the bot gracefully.
updater.idle()
if __name__ == '__main__':
main()