@@ -45,7 +45,7 @@ def __init__(self, main):
45
45
def run (self ):
46
46
self ._logger .debug ("Try first connect." )
47
47
self .tryFirstContact ()
48
- # repeat fetching and processing messages unitil thread stopped
48
+ # repeat fetching and processing messages until thread stopped
49
49
self ._logger .debug ("Listener is running." )
50
50
try :
51
51
while not self .do_stop :
@@ -208,7 +208,7 @@ def handleDocumentMessage(self, message, chat_id, from_id):
208
208
#stream = octoprint.filemanager.util.StreamWrapper(target_filename, bytes_reader_class(data))
209
209
#self.main._file_manager.add_folder(self.get_plugin_data_folder() , "tmpzip", ignore_existing=True)
210
210
zip_filename = self .main .get_plugin_data_folder ()+ "/tmpzip/" + file_name
211
- with open (zip_filename , 'w ' ) as f :
211
+ with open (zip_filename , 'wb ' ) as f :
212
212
f .write (data )
213
213
#self.main._file_manager.add_file(octoprint.filemanager.FileDestinations.LOCAL, target_filename, stream, allow_overwrite=True)
214
214
except Exception as ex :
@@ -269,7 +269,7 @@ def handleDocumentMessage(self, message, chat_id, from_id):
269
269
os .remove (zip_filename )
270
270
else :
271
271
path = self .main .get_plugin_data_folder ()+ "/tmpzip/" + file_name
272
- with open (path , 'w ' ) as f :
272
+ with open (path , 'wb ' ) as f :
273
273
f .write (data )
274
274
file_wrapper = octoprint .filemanager .util .DiskFileWrapper (target_filename , path )
275
275
added_file = self .main ._file_manager .add_file (octoprint .filemanager .FileDestinations .LOCAL ,file_wrapper .filename ,file_wrapper ,allow_overwrite = True )
@@ -1002,7 +1002,8 @@ def get_api_commands(self):
1002
1002
return dict (
1003
1003
testToken = ["token" ],
1004
1004
testEvent = ["event" ],
1005
- delChat = ["ID" ]
1005
+ delChat = ["ID" ],
1006
+ setCommandList = ["force" ]
1006
1007
)
1007
1008
1008
1009
def on_api_get (self , request ):
@@ -1064,6 +1065,16 @@ def on_api_command(self, command, data):
1064
1065
return json .dumps ({'ok' : True , 'error_msg' : None })
1065
1066
except Exception as ex :
1066
1067
return json .dumps ({'ok' : False , 'username' : None , 'error_msg' : str (ex )})
1068
+ elif command == "setCommandList" :
1069
+ self ._logger .debug ("Set default command for bot" )
1070
+ try :
1071
+ self .setMyCommands (True )
1072
+ self ._logger .debug ("Set default command for bot done will return ok" )
1073
+ return json .dumps ({'ok' : True , 'setMyCommands_state_str' : gettext ("SetMyCommands done" ,), 'error_msg' : None })
1074
+ except Exception as ex :
1075
+ return json .dumps ({'ok' : False , 'setMyCommands_state_str' : gettext ("Error: %(error)s" , error = ex ), 'error_msg' : str (ex )})
1076
+
1077
+
1067
1078
##########
1068
1079
### Telegram API-Functions
1069
1080
##########
@@ -1141,7 +1152,7 @@ def _send_msg(self, message="", with_image=False,with_gif=False,responses=None,
1141
1152
self ._logger .debug ("start _send_msg" )
1142
1153
1143
1154
try :
1144
- if with_image or with_gif :
1155
+ if ( with_image or with_gif ) :
1145
1156
premethod = self ._settings .get (["PreImgMethod" ])
1146
1157
self ._logger .debug ("PreImgMethod {}" .format (premethod ))
1147
1158
precommand = self ._settings .get (["PreImgCommand" ])
@@ -1197,53 +1208,57 @@ def _send_msg(self, message="", with_image=False,with_gif=False,responses=None,
1197
1208
data ['disable_notification' ] = silent
1198
1209
if with_gif : #giloser 05/05/19
1199
1210
try :
1200
- self ._logger .info ("Will try to create a gif " )
1201
1211
sendOneInLoop = False
1202
- #requests.get(self.main.bot_url + "/sendChatAction", params = {'chat_id': chat_id, 'action': 'upload_document'})
1203
- if self ._plugin_manager .get_plugin ("multicam" ,True ) and self ._settings .get (["multicam" ]):
1204
- try :
1205
- curr = self ._settings .global_get (["plugins" ,"multicam" ,"multicam_profiles" ])
1206
- self ._logger .debug ("multicam_profiles : " + str (curr ))
1207
- ListGif = []
1208
- for li in curr :
1209
- try :
1210
- self ._logger .debug ("multicam profile : " + str (li ))
1211
- url = li .get ("URL" )
1212
- self ._logger .debug ("multicam URL : " + str (url ))
1213
- ret = self .create_gif_new (chatID ,0 ,li )
1214
- if ret != "" :
1215
- ListGif .append (ret )
1216
- #if not sendOneInLoop:
1217
- # self.send_file(chatID, ret,message)
1218
- #else:
1219
- # self.send_file(chatID, ret,"")
1220
- #sendOneInLoop = True
1221
- except Exception as ex :
1222
- self ._logger .exception ("Exception loop multicam URL to create gif: " + str (ex ) )
1223
- ret = ListGif [- 1 ]
1224
- self ._logger .debug ("ListGif: " + str (ListGif ))
1225
- for x in ListGif :
1226
- try :
1227
- if x != ret :
1228
- self ._logger .debug ("send_file whithout message: " + str (x ))
1229
- self .send_file (chatID , x ,"" )
1230
- except Exception as ex :
1231
- self ._logger .exception ("Exception loop multicam URL to send gif: " + str (ex ) )
1232
-
1233
- except Exception as ex :
1234
- self ._logger .exception ("Exception occured on getting multicam options: " + str (ex ) )
1212
+ if kwargs ['event' ] == "MovieDone" :
1213
+ ret = kwargs ['movie' ]
1235
1214
else :
1236
- ret = self .create_gif_new (chatID ,0 ,0 )
1215
+ self ._logger .info ("Will try to create a gif " )
1216
+
1217
+ #requests.get(self.main.bot_url + "/sendChatAction", params = {'chat_id': chat_id, 'action': 'upload_document'})
1218
+ if self ._plugin_manager .get_plugin ("multicam" ,True ) and self ._settings .get (["multicam" ]):
1219
+ try :
1220
+ curr = self ._settings .global_get (["plugins" ,"multicam" ,"multicam_profiles" ])
1221
+ self ._logger .debug ("multicam_profiles : " + str (curr ))
1222
+ ListGif = []
1223
+ for li in curr :
1224
+ try :
1225
+ self ._logger .debug ("multicam profile : " + str (li ))
1226
+ url = li .get ("URL" )
1227
+ self ._logger .debug ("multicam URL : " + str (url ))
1228
+ ret = self .create_gif_new (chatID ,0 ,li )
1229
+ if ret != "" :
1230
+ ListGif .append (ret )
1231
+ #if not sendOneInLoop:
1232
+ # self.send_file(chatID, ret,message)
1233
+ #else:
1234
+ # self.send_file(chatID, ret,"")
1235
+ #sendOneInLoop = True
1236
+ except Exception as ex :
1237
+ self ._logger .exception ("Exception loop multicam URL to create gif: " + str (ex ) )
1238
+ ret = ListGif [- 1 ]
1239
+ self ._logger .debug ("ListGif: " + str (ListGif ))
1240
+ for x in ListGif :
1241
+ try :
1242
+ if x != ret :
1243
+ self ._logger .debug ("send_file whithout message: " + str (x ))
1244
+ self .send_file (chatID , x ,"" )
1245
+ except Exception as ex :
1246
+ self ._logger .exception ("Exception loop multicam URL to send gif: " + str (ex ) )
1247
+
1248
+ except Exception as ex :
1249
+ self ._logger .exception ("Exception occured on getting multicam options: " + str (ex ) )
1250
+ else :
1251
+ ret = self .create_gif_new (chatID ,0 ,0 )
1237
1252
1238
- if ret == "" :
1239
- ret = self .create_gif_new (chatID ,0 ,0 )
1253
+ if ret == "" :
1254
+ ret = self .create_gif_new (chatID ,0 ,0 )
1240
1255
1241
1256
if ret != "" and not sendOneInLoop :
1242
1257
self ._logger .debug ("send_file whith message: " + str (ret ))
1243
1258
self .send_file (chatID , ret ,message )
1244
- #ret = self.create_gif_new(chatID,0,0)
1245
- #if ret != "":
1246
- # self.send_file(chatID, ret,message)
1259
+ #ret = self.create_gif_new(chatID,0,0)
1260
+ #if ret != "":
1261
+ # self.send_file(chatID, ret,message)
1247
1262
except Exception as ex :
1248
1263
self ._logger .info ("Caught an exception trying send gif: " + str (ex ))
1249
1264
self .send_msg (self .gEmo ('dizzy face' ) + " Problem creating gif, please check log file" , chatID = chatID )#and make sure you have installed libav-tools or ffmpeg with command : `sudo apt-get install libav-tools`",chatID=chat_id)
@@ -1523,6 +1538,45 @@ def test_token(self, token=None):
1523
1538
else :
1524
1539
return "@" + json ['result' ]['username' ]
1525
1540
1541
+
1542
+ # sets bot own list of commands
1543
+ def setMyCommands (self , force = False ):
1544
+ if not self .send_messages :
1545
+ return
1546
+ try :
1547
+ shallRun = force
1548
+ if not force :
1549
+ # check if a list of commands was already set
1550
+ resp = requests .get (self .bot_url + "/getMyCommands" ).json ()
1551
+ self ._logger .debug ("getMyCommands returned " + str (resp ))
1552
+ shallRun = (len (resp ['result' ]) == 0 )
1553
+ if shallRun :
1554
+ commands = []
1555
+ commands .append ({"command" :"status" ,"description" :"Displays the current status including a capture from the camera" })
1556
+ commands .append ({"command" :"togglepause" ,"description" :"Pauses/Resumes current print" })
1557
+ commands .append ({"command" :"files" ,"description" :"Lists all the files available for printing" })
1558
+ commands .append ({"command" :"print" ,"description" :"Lets you start a print (confirmation required)" })
1559
+ commands .append ({"command" :"tune" ,"description" :"Sets feed and flow rate, control temperatures" })
1560
+ commands .append ({"command" :"ctrl" ,"description" :"Activates self defined controls from Octoprint" })
1561
+ commands .append ({"command" :"con" ,"description" :"Connects or disconnects the printer" })
1562
+ commands .append ({"command" :"sys" ,"description" :"Executes Octoprint system commands" })
1563
+ commands .append ({"command" :"abort" ,"description" :"Aborts the currently running print (confirmation required)" })
1564
+ commands .append ({"command" :"off" ,"description" :"Turn off the printer" })
1565
+ commands .append ({"command" :"on" ,"description" :"Turn on the printer" })
1566
+ commands .append ({"command" :"settings" ,"description" :"Displays notification settings and lets change them" })
1567
+ commands .append ({"command" :"upload" ,"description" :"Stores a file into the Octoprint library" })
1568
+ commands .append ({"command" :"filament" ,"description" :"Shows filament spools and lets you change it (requires Filament Manager Plugin)" })
1569
+ commands .append ({"command" :"user" ,"description" :"Gets user info" })
1570
+ commands .append ({"command" :"gcode" ,"description" :"call gCode commande with /gcode_XXX where XXX is the gcode command" })
1571
+ commands .append ({"command" :"gif" ,"description" :"Sends a gif from the current video" })
1572
+ commands .append ({"command" :"supergif" ,"description" :"Sends a bigger gif from the current video" })
1573
+ commands .append ({"command" :"shutup" ,"description" :"Disables automatic notifications until the next print ends" })
1574
+ commands .append ({"command" :"dontshutup" ,"description" :"Makes the bot talk again (opposite of `/shutup`)" })
1575
+ commands .append ({"command" :"help" ,"description" :"Shows this help message" })
1576
+ resp = requests .post (self .bot_url + "/setMyCommands" , data = {'commands' :json .dumps (commands )}).json ()
1577
+ self ._logger .debug ("setMyCommands returned " + str (resp ))
1578
+ except Exception as ex :
1579
+ pass
1526
1580
##########
1527
1581
### Helper methods
1528
1582
##########
@@ -1618,8 +1672,9 @@ def calculate_ETA(self,printTime = 0):
1618
1672
currentData = self ._printer .get_current_data ()
1619
1673
current_time = datetime .datetime .today ()
1620
1674
if not currentData ["progress" ]["printTimeLeft" ]:
1621
- if printTime == 0 :
1675
+ if printTime == 0 or str ( printTime ) == "11:11:00" :
1622
1676
return "" # maybe put something like "nothing to print" in here
1677
+ self ._logger .debug ("printTime=" + str (printTime ))
1623
1678
finish_time = current_time + datetime .timedelta (0 , printTime )
1624
1679
else :
1625
1680
finish_time = current_time + datetime .timedelta (0 , currentData ["progress" ]["printTimeLeft" ])
@@ -1880,7 +1935,6 @@ def hook_gcode_sent(self, comm_instance, phase, cmd, cmd_type, gcode, *args, **k
1880
1935
if gcode and cmd [:4 ] == "M600" :
1881
1936
self ._logger .info ("M600 registered" )
1882
1937
try :
1883
- #self.on_event("plugin_pause_for_user_event_notify", {})
1884
1938
self .on_event ("gCode_M600" , {})
1885
1939
except Exception as ex :
1886
1940
self ._logger .error ("exception on event M600: " + str (ex ))
@@ -1891,7 +1945,9 @@ def recv_callback(self, comm_instance, line, *args, **kwargs):
1891
1945
if not self .triggered :
1892
1946
self .on_event ("plugin_pause_for_user_event_notify" , {})
1893
1947
self .triggered = True
1894
- # Other text, we may fire another event if we encounter "paused for user" again
1948
+ elif "echo:UserNotif" in line :
1949
+ self .on_event ("UserNotif" , {"UserNotif" :line [15 :]})
1950
+ # Other text, we may fire another event if we encounter "paused for user" again
1895
1951
else :
1896
1952
self .triggered = False
1897
1953
0 commit comments