Skip to content

Add commands and notifications

derpicknicker1 edited this page May 19, 2016 · 2 revisions

TMSG class

Here you find the known notification messages and their handles. The only way to start a messageHandle should be via on_event() in init.py If you want to add/remove notifications read the following:

telegramMsgDict {  }

	telegramMsgDict contains the Message Settings. For each message there is a object as follows:
	<MessageName>: {'text': <MessageText>, 'image': <Boolean>, 'markup': <markupStr> [,'no_setting':<Boolean>, 'bind_msg': <BindMessageName>]}
	
	- MessageName: 	The name of the Message. You can hook on octoprint events by choosing the name 
					of the event you want to handle. You can find a list of events here:
					http://docs.octoprint.org/en/master/events/index.html#sec-events-available-events

	- 'text': <MessageText>: 	The text which will be sent with the notification. yo can use some 
								variables in the text via .format(**locals()). Even emojis are possible
								See _sendNotification() below for more details

	- 'image': <Boolean>: 	if this is true a snapshot will be send with the message	

    - 'markup': <markupStr>: This setting determines if markup is used in message text. 
                             Set markup string to one of the following:
                              - 'HTML' for using html markup
                              - 'Markdown' to use markdown markup
                              - 'off' when no markup is used
                              Everything else than 'HTML' or 'Markdown' is interpreted as 'off'
	
	Optional:

	- 'no_setting': <Boolean>: if this value exists (true or false does not matter) no checkbox for 
					enable/disable of this notification will be showen in the notification settings 
					in octoprint. This is used for messages which will be triggered by the user, so
					he wants to remove them when he triggers them. If you use this messages, you have
					to - YOU HAVE TO - pass the chat_id when calling them or it will be send to all 
					users with notifications enabled. Can cause strange behavior.
					In telegrammCommands.py cmdStatus() you will see how to use it.
					StatusPrinting ans StatusNotPrinting are using this feature. 

	- 'bind_msg': <bindMessageName>: This option will bind this message to an other message. So it shares
									 text and image setting with the message given by it's name as string.
									 So when this notification is sent, it will contain the same content
									 as the message bound to will. Also no extra edit box is shown in the
									 settings dialog, but the messageName will be shown beside the box of
									 the bound message.
									 StatusPrinting and ZChange are using this feature.




The TMSG class handles the messages. There is an other dict - msgCmdDict{} - which will bind the messageName 
to the corresponding messageHandler.

	Each messageHandler must look something like this:

	def msg<messageName>(self, event, payload, **kwargs):
		kwargs['event'] = self._prepMsg(event)
		#your handler code
		self._sendNotification(payload, **kwargs)

	This means you should name your hanlder like your message with 'msg' as prefix
	If your message is named MyMessage, then your handler should be named msgMyMessage
	'event' argument contains the name of the event which called the handler
	In 'payload' argument you get the payload of the octoprint event if it is one. See link mentioned above.
	it is !important! to include the two lines of code shown above
		- the call on _prepMsg will do message binding. should be done as first line.
		  even if your message is not bound, call it. so if you ever change it in 
		  telegramMsgDict you won't forget. Also it will setup z-info for message parsing

		- at the end of the function you have to call _sendNotification with the shown arguments
                      to start parsing and sending of the message



!!!!!!!!!!!!!!!!!!! IT IS IMPORTANT TO DO THIS !!!!!!!!!!!!!!!!!
If you add and or del one ore more notifications, you have to increment the settings
version counter in get_ettings_version() in __init__.py. This will start settings migration
to add/remove notification settings to/from users and updates message settings on next startup of octoprint.
!!!!!!!!!!!!!!!!!!! IT IS IMPORTANT TO DO THIS !!!!!!!!!!!!!!!!!



To ADD a Notification:
	- add an message object to telegramMsgDict
	- add a handler bind to msgCmdDict in class TMSG
	- add a handler method to the TMSG class

To REMOVE a Notivication:
	- do above in reverse

ON BOTH DO:
- increment plugin version

TCMD class

This class handles received commands/messages (commands in the following). commandDict{} holds the commands and their behavior. Each command has its own handler. If you want to add/del commands, read the following:

commandDict{}

The commandDict holds the command objects which define the known commands. 
Each object looks like the following:
<commandName>: { 'cmd': <commandHandle> [, 'id': <Integer> 'bind_none': <Boolean>, 'bind_cmd': <bindCmdName>]}

	- <commandName>: The name of the command corresponds to the message text received.
					 If you use emojis in commands, than you ALWAYS have to define
					 a second object and set the command name to the same text without emojis.
					 Otherwise the 'Send emojis" option won't work properly.
					 The other values of the two objects should be the same.
					 See " Enter height" ans " Enter time" as example.

	- 'cmd': <commandHandle>: Here you have to point to the message handle method

	Optional:

    - 'id': <Integer>: This id is needed if there is a translation for this command (gettext("...")). 
                       It will be used to update user commands wich depend on commandname. 

	- 'bind_none': <Boolean>: Sets the authorization for the command ALWAYS TRUE for EVERY user/group.
							  So if accept_commands is activated for a user/group he is automatically 
							  able to do this command.
							  This is used to hide the command in user command authorization settings.
							  It is only used for non critical commands: Yes, No and Cancel which only
							  respond with a text message and wont cause any unwanted action.

	- 'bind_cmd': <bindCmdName>: This binds the authorization for the command to the authorization of
								 the given bind command. So if the bind command is changed in settings,
								 this command will be changed to the same value. This applies individual
								 each user/group setting.
								 The command is also hidden in user command authorization settings.
								 This otion is for commands which depend on other commands like
								 the settings. See "/settings", "Change height", "Change time" etc.


commandHandler

This is a method to handle the command and to execute desired actions. It should always at minimum look as follows:

def cmd<commandName>(self,chat_id,**kwargs):
	self.main.send_msg(responseMessage,chatID=chat_id)

The Name of the method should be the name of the command with 'cmd' as prefix.
At least a response should be send to the user. so do a call on self.main.send_msg()
with a responseMessage. YOU HAVE TO pass also the chat_id the handler got when called.
This ensures that always the right user gets the response.

!!!!!!!!!!!!!!!!!!! IT IS IMPORTANT TO DO THIS !!!!!!!!!!!!!!!!!
If you add and or del one ore more commands, you have to increment the settings
version counter in get_ettings_version() in __init__.py.
This will start settings migration to add/remove command settings to/from users.
!!!!!!!!!!!!!!!!!!! IT IS IMPORTANT TO DO THIS !!!!!!!!!!!!!!!!!



To ADD a command:
	- add an command object to commandDict
	- add a handler method to the TCMD class

To REMOVE a Notivication:
	- do above in reverse

ON BOTH DO:
- increment plugin version
Clone this wiki locally