moobius.core.sdk#
Module-level functions#
(No module-level functions)
Class Moobius#
This is the main core class of Moobius. CCS services inherit this class. This is a big switchyard for handling HTTP, the Websocket, and sending callbacks back to the CCS app. This has a complex lifecycle with server initialization steps, etc.
Moobius.true_channel_list#
Gets the list of channels the self will end up bound to. Only used if self.service_mode.
**Moobius.true_channel_list**(self)
__(this class constructor accepts no arguments):__
The channel id list.
(this function does not raise any notable errors)
Moobius.create_new_service#
Creates a new service and sets self.client_id to it..
**Moobius.create_new_service**(self, description)
__description=’Generated by MoobiusService’:__ N optional description.
The service_id.
(this function does not raise any notable errors)
Moobius.start#
- Starts the service and calls start() fns are called with wand.run. There are 6 steps:
Authenticate.
Connect to the websocket server.
Bind the service to the channels, if a service. If there is no service_id in the config file, create a new service and update the config file.
Start the scheduler and run refresh(), authenticate(), and send_heartbeat() periodically.
Call the on_start() callback (override this method to perform your own initialization tasks).
Start listening to the websocket and the Wand.
**Moobius.start**(self)
__(this class constructor accepts no arguments):__
None.
(this function does not raise any notable errors)
Moobius.user_join_service_channels#
Joins service channels given a service config dict or JSON filename (use in user mode).
**Moobius.user_join_service_channels**(self)
__(this class constructor accepts no arguments):__
None.
(this function does not raise any notable errors)
Moobius.fetch_service_id_each_channel#
**Moobius.fetch_service_id_each_channel**(self)
__(this class constructor accepts no arguments):__
The dict describing which service_id each channel_id is bound to.
Channels can only be bound to a single service. Channels not bound to any service will not be in the dict.
(this function does not raise any notable errors)
Moobius.fetch_bound_channels#
**Moobius.fetch_bound_channels**(self)
__(this class constructor accepts no arguments):__
The list of channels that are bound to this service.
(this function does not raise any notable errors)
Moobius.fetch_characters#
**Moobius.fetch_characters**(self, channel_id)
__channel_id:__ Channel id.
The list of Character objects.
- This list includes:
Real members (ids for a particular user-channel combination) who joined the channel with the given channel_id. Agent characters that have been created by this service; agent characters are not bound to any channel.
(this function does not raise any notable errors)
Moobius.send_message#
Sends a message down (or up if in user-mode). This function is very flexible.
**Moobius.send_message**(self, message, channel_id, sender, recipients, subtype, text, path, image, audio, link, title, button, len_limit, file_display_name, context)
- __message:__ The message to send.
- If a string, the message will be a text message unless subtype is set.
If not a text message, the string must either be a local file_path or an http(s) file_path.
If a MessageBody or dict, the message sent will depend on it’s fields/attributes as well as the overrides specified. If a pathlib.Path, will be a file/audio/image message by default.
- __channel_id=None:__ The channel ids, if None message must be a MessageBody with the channel_id.
Overrides message if not None.
- __sender=None:__ The character/user who’s avatar appears to “speak” this message.
Overrides message if not None.
- __recipients=None:__ List of characters or character_ids.
Overrides message if not None.
- __subtype=None:__ Can be set to types.TEXT, types.IMAGE, types.AUDIO, types.FILE, or types.CARD
If None, the subtype will be inferred.
__text=None:__ Text which will override message.
__path=None:__ The filepath or URL of the message’s content. Not needed for text messages.
__image=None:__ Equivalent to path except for also setting the subtype to types.IMAGE.
__audio=None:__ Equivalent to path except for also setting the subtype to types.AUDIO.
__link=None:__ For card messages, the URL that the link links to.
__title=None:__ For card messages, the card title.
__button=None:__ For card messages, the text that appears in the button.
__len_limit=None:__ Limit the length of large text messages.
- __file_display_name=None:__ The name shown for downloadable files can be set to a value different than the filename.
Sets the subtype to “types.FILE” if subtype is not specified.
__context=None:__ Optional metadata.
None.
(this function does not raise any notable errors)
Moobius.send#
- Sends any kind of payload to the websocket. Example payload types:
message_down, update, update_characters, update_canvas, update_buttons, update_style, and heartbeat.
Rarely used except internally, but provides the most flexibility for those special occasions.
**Moobius.send**(self, payload_type, payload_body)
__payload_type:__ The type of the payload.
- __payload_body:__ The body of the payload.
Strings will be converted into a Payload object.
None.
(this function does not raise any notable errors)
Moobius.create_channel#
Creates a channel. By default bind is True, which means the service connects itself to the channel.
**Moobius.create_channel**(self, channel_name, channel_desc, bind)
__channel_name:__ Channel name.
__channel_desc:__ The channel description.
__bind=True:__ Whether to bind to the new channel.
The channel id.
(this function does not raise any notable errors)
Moobius.send_canvas#
Updates the canvas.
**Moobius.send_canvas**(self, canvas_items, channel_id, recipients)
__canvas_items:__ List of CanvasItems (which have text and/or images).
__channel_id:__ A channel_id.
__recipients:__ The recipients.
The message.
(this function does not raise any notable errors)
Moobius.send_heartbeat#
Sends a heartbeat to the server.
**Moobius.send_heartbeat**(self)
__(this class constructor accepts no arguments):__
None.
(this function does not raise any notable errors)
Moobius.send_refresh#
Sends a refresh.
**Moobius.send_refresh**(self, channel_id)
__channel_id:__ Channel_id.
The message sent. A user function.
(this function does not raise any notable errors)
Moobius.do_member_sync#
Syncs a member.. This is the most common way to send buttons, etc.
**Moobius.do_member_sync**(self, channel_id, character)
__channel_id:__ Channel_id.
__character:__ Character/character_id.
None.
(this function does not raise any notable errors)
Moobius.before_channel_init#
A global init called right before the channels are initialized.
**Moobius.before_channel_init**(self)
__(this class constructor accepts no arguments):__
None.
(this function does not raise any notable errors)
Moobius.on_channel_checkin#
CCS apps often override this, syncing members, etc..
**Moobius.on_channel_checkin**(self, channel_id)
__channel_id:__ Channel id.
None.
(this function does not raise any notable errors)
Moobius.send_service_login#
Logs in. Much like the HTTP api, this needs to be sent before any other messages. Fills in: the id of this service; the https token the service has for authentication; set to False.
**Moobius.send_service_login**(self)
__(this class constructor accepts no arguments):__
The message as a dict.
(this function does not raise any notable errors)
Moobius.refresh_authentication#
Refreshes the access token.
**Moobius.refresh_authentication**(self)
__(this class constructor accepts no arguments):__
The new token.
(this function does not raise any notable errors)
Moobius.authenticate#
Authenticates using self.username andself.password. Needs to be called before any other API calls.
**Moobius.authenticate**(self)
__(this class constructor accepts no arguments):__
(the access token, the refresh token).
Raises an Exception if doesn’t receive a valid response. Like most GET and POST functions it will raise any errors thrown by the http API.
(this function does not raise any notable errors)
Moobius.sign_up#
Signs up and sends the confirmation code to the email. After confirming the account, self.authenticate() can be used to retrieve access and refresh tokens.
**Moobius.sign_up**(self)
__(this class constructor accepts no arguments):__
None.
(this function does not raise any notable errors)
Moobius.sign_out#
Signs out using the access token obtained from signing in.
**Moobius.sign_out**(self)
__(this class constructor accepts no arguments):__
None.
(this function does not raise any notable errors)
Moobius.update_current_user#
Updates the user info. Used by user mode.
**Moobius.update_current_user**(self, avatar, description, name)
__avatar:__ Link to image or local file_path to upload.
__description:__ Of the user.
__name:__ The name that shows in chat.
None.
(this function does not raise any notable errors)
Moobius.update_agent#
Updates the characters name, avatar, etc for a FAKE user, for real users use update_current_user. Fills in: the id of this service.
**Moobius.update_agent**(self, character, avatar, description, name)
__character:__ Who to update. Can also be a Character object or character_id. Cannot be a list.
__avatar:__ A link to user’s image or a local file_path to upload.
__description:__ The description of user.
__name:__ The name that will show in chat.
The Data about the user as a dict.
(this function does not raise any notable errors)
Moobius.update_channel#
Updates the name and desc of a channel.
**Moobius.update_channel**(self, channel_id, channel_name, channel_desc)
__channel_id:__ Which channel to update.
__channel_name:__ The new channel name.
__channel_desc:__ The new channel description.
None.
(this function does not raise any notable errors)
Moobius.bind_service_to_channel#
Binds a service to a channel. This function is unusual in that it. Fills in: the id of this service.
**Moobius.bind_service_to_channel**(self, channel_id)
__channel_id:__ Channel IDs.
Whether it was sucessful rather than raising errors if it fails.
(this function does not raise any notable errors)
Moobius.unbind_service_from_channel#
Unbinds a service to a channel. Fills in: the id of this service.
**Moobius.unbind_service_from_channel**(self, channel_id)
__channel_id:__ Channel IDs.
None.
(this function does not raise any notable errors)
Moobius.create_agent#
Creates a character with a given name, avatar, and description. The created user will be bound to the given service. Fills in: the id of this service.
**Moobius.create_agent**(self, name, avatar, description)
__name:__ The name of the user.
__avatar=None:__ The image URL of the user’s picture OR a local file path.
__description=’No description’:__ The description of the user.
The Character object representing the created user.
(this function does not raise any notable errors)
Moobius.fetch_popular_channels#
Fetches the popular channels,.
**Moobius.fetch_popular_channels**(self)
__(this class constructor accepts no arguments):__
The list of channel_id strings.
(this function does not raise any notable errors)
Moobius.fetch_channel_list#
Fetches all? channels,.
**Moobius.fetch_channel_list**(self)
__(this class constructor accepts no arguments):__
The list of channel_id strings.
(this function does not raise any notable errors)
Moobius.fetch_member_ids#
Fetches the member ids of a channel which coorespond to real users. Fills in: the id of this service.
**Moobius.fetch_member_ids**(self, channel_id, raise_empty_list_err)
__channel_id:__ The channel ID.
__raise_empty_list_err=False:__ Raises an Exception if the list is empty.
The list of character_id strings.
An Exception (empty list) if raise_empty_list_err is True and the list is empty.
Moobius.fetch_character_profile#
**Moobius.fetch_character_profile**(self, character)
__character:__ String-valued (or list-valued) character_id/character.
The Character object (or list therof).
It works for both member_ids and agent_ids.
(this function does not raise any notable errors)
Moobius.fetch_service_id_list#
**Moobius.fetch_service_id_list**(self)
__(this class constructor accepts no arguments):__
The list of service_id strings of the user.
(this function does not raise any notable errors)
Moobius.fetch_agents#
Fills in: the id of this service.
**Moobius.fetch_agents**(self)
__(this class constructor accepts no arguments):__
The list of non-user Character objects bound to this service.
(this function does not raise any notable errors)
Moobius.fetch_message_history#
Returns the message chat history.
**Moobius.fetch_message_history**(self, channel_id, limit, before)
__channel_id:__ Channel with the messages inside of it.
__limit=1024:__ Max number of messages to return (messages further back in time, if any, will not be returned).
__before=’null’:__ Only return messages older than this.
The list of dicts.
(this function does not raise any notable errors)
Moobius.upload#
Uploads the file at local path file_path to the Moobius server. Automatically calculates the upload URL and upload fields.
**Moobius.upload**(self, file_path)
__file_path:__ File_path.
The uploaded URL. Raises an Exception if the upload fails.
(this function does not raise any notable errors)
Moobius.download#
Downloads a file from a url or other source to a local filename, automatically creating dirs if need be.
**Moobius.download**(self, source, file_path, auto_dir, overwrite, bytes, headers)
__source:__ The url to download the file from. OR a MessageBody which has a .content.path in it.
- __file_path=None:__ The file_path to download to.
None will create a file based on the timestamp + random numbers. If no extension is specified, will infer the extension from the url if one exists.
- __auto_dir=None:__ If no file_path is specified, a folder must be choosen.
Defaults to ‘./downloads’.
__overwrite=True:__ Allow overwriting pre-existing files. If False, will raise an Exception on name collision.
__bytes=False:__ If True, will return bytes instead of saving a file.
- __headers=None:__ Optional headers. Use these for downloads that require auth.
Can set to “self” to use the same auth headers that this instance is using.
The full filepath if bytes if false, otherwise the file’s content bytes if bytes=True.
(this function does not raise any notable errors)
Moobius.create_channel_group#
Creates a channel group.
**Moobius.create_channel_group**(self, channel_id, group_name, characters)
__channel_id:__ The id of the group leader?.
__group_name:__ What to call it.
__characters:__ A list of characters or character_id strings that will be inside the group.
The group_id string.
(this function does not raise any notable errors)
Moobius.create_service_group#
Creates a group containing the list of characters_ids and returns this Group object. This group can then be used in send_message_down payloads.
**Moobius.create_service_group**(self, characters)
__characters:__ A list of character_id strings or Characters that will be inside the group.
The Group object.
(this function does not raise any notable errors)
Moobius.character_ids_of_channel_group#
Gets a list of character ids belonging to a channel group. Websocket payloads contain these channel_groups which are shorthand for a list of characters.
**Moobius.character_ids_of_channel_group**(self, sender_id, channel_id, group_id)
__sender_id:__ The message’s sender.
__channel_id:__ The message specified that it was sent in this channel.
__group_id:__ The messages recipients.
The character_id list.
(this function does not raise any notable errors)
Moobius.character_ids_of_service_group#
**Moobius.character_ids_of_service_group**(self, group_id)
__group_id:__ Group_id.
The list of character ids belonging to a service group.
- Note that the ‘recipients’ in ‘on message up’ might be None:
To avoid requiring checks for None this function will return an empty list given Falsey inputs or Falsey string literals.
(this function does not raise any notable errors)
Moobius.fetch_channel_temp_group#
Like fetch_channel_group_list but for TEMP groups.. Fills in: the id of this service.
**Moobius.fetch_channel_temp_group**(self, channel_id)
__channel_id:__ Channel_id.
The list of groups.
(this function does not raise any notable errors)
Moobius.fetch_channel_group_list#
Similar to fetch_channel_group_dict.. Fills in: the id of this service.
**Moobius.fetch_channel_group_list**(self, channel_id)
__channel_id:__ Channel_id.
The raw data.
(this function does not raise any notable errors)
Moobius.fetch_user_from_group#
Not yet implemented! Fetches the user profile of a user from a group.
**Moobius.fetch_user_from_group**(self, user_id, channel_id, group_id)
__user_id:__ The user ID.
__channel_id:__ The channel ID. (TODO: of what?).
__group_id:__ The group ID.
The user profile Character object.
An Exception because it is unused, unimplemented, and may be removed.
Moobius.fetch_target_group#
Not yet implemented! Fetches info about the group.
**Moobius.fetch_target_group**(self, user_id, channel_id, group_id)
__user_id:__ The user id of the user bieng fetched (is this needed?).
__channel_id:__ The channel_id of the channel bieng fetched.
__group_id:__ Which group to fetch.
The data-dict data.
An Exception because it is unused, unimplemented, and may be removed.
Moobius.send_user_login#
Logs-in a user. Every 2h AWS will force-disconnect, so it is a good idea to send this on connect. Fills in: the https token the service has for authentication; set to False.
**Moobius.send_user_login**(self)
__(this class constructor accepts no arguments):__
The message as a dict.
(this function does not raise any notable errors)
Moobius.send_update#
A generic update function that is rarely used. Fills in: the id of this service; set to False.
**Moobius.send_update**(self, data, target_client_id)
__data:__ The content of the update.
__target_client_id:__ The target client id (TODO: not currently used).
The message as a dict.
(this function does not raise any notable errors)
Moobius.send_characters#
Updates the characters that the recipients see. Fills in: the id of this service; set to False.
**Moobius.send_characters**(self, characters, channel_id, recipients)
__characters:__ The group id to represent the characters who are updated.
__channel_id:__ The channel id.
__recipients:__ The group id to send to.
The message as a dict.
(this function does not raise any notable errors)
Moobius.send_style#
Updates the style (whether the canvas is expanded, other look-and-feel aspects) that the recipients see. Fills in: the id of this service; set to False.
**Moobius.send_style**(self, style_items, channel_id, recipients)
__style_items:__ The style content to be updated. Dicts are converted into 1-elemnt lists.
__channel_id:__ The channel id.
__recipients:__ The group id to send to.
The message as a dict.
(this function does not raise any notable errors)
Start from here.
This is a Button, which most channels have
" >>> }] >>> ws_client.update_style("service_id", "channel_id", style_items, ["user1", "user2"])Moobius.send_join_channel#
A user joins the channel with channel_id, unless dry_run is True.. Fills in: the id of this service when in user mode, which is a user id; set to False.
**Moobius.send_join_channel**(self, channel_id)
__channel_id:__ The channel_id.
The message sent.
(this function does not raise any notable errors)
Moobius.send_leave_channel#
A user leaves the channel with channel_id, unless dry_run is True.. Fills in: the id of this service when in user mode, which is a user id; set to False.
**Moobius.send_leave_channel**(self, channel_id)
__channel_id:__ The channel_id.
The message sent.
(this function does not raise any notable errors)
Moobius.listen_loop#
Listens to the wand in an infinite loop, polling self.queue (which is an aioprocessing.AioQueue). This allows the wand to send “spells” (messages) to the services at any time.
**Moobius.listen_loop**(self)
__(this class constructor accepts no arguments):__
The Never.
(this function does not raise any notable errors)
Moobius.handle_received_payload#
Decodes the received websocket payload JSON and calls the handler based on p[‘type’],. Example methods called:
on_message_up(), on_action(), on_button_click(), on_copy_client(), on_unknown_payload()
- Example use-case:
>>> self.ws_client = WSClient(ws_server_uri, on_connect=self.send_service_login, handle=self.handle_received_payload).
**Moobius.handle_received_payload**(self, payload)
__payload:__ Payload string.
None.
(this function does not raise any notable errors)
Moobius.on_action#
Calls the corresponding method to handle different subtypes of action. This callback is rarely overriden; it is more common to override the other callbacks that the calls. Example methods called:
on_button_click(), on_join().
**Moobius.on_action**(self, action_data)
__action_data:__ Action data (as a dict) from a user.
None.
(this function does not raise any notable errors)
Moobius.on_update#
Dispatches it to one of various callbacks. Use for user mode. It is recommended to overload the invididual callbacks instead of this function.
**Moobius.on_update**(self, update)
__update:__ N Update object from the socket.
None.
(this function does not raise any notable errors)
Moobius.on_start#
Called when the service is initialized.
**Moobius.on_start**(self)
__(this class constructor accepts no arguments):__
None.
(this function does not raise any notable errors)
Moobius.on_channel_init#
Called once per channel on startup.. By default, if the db has been set, a MoobiusStorage is created in self.channels, otherwise it is set to None. Also does a channel sync by default.
**Moobius.on_channel_init**(self, channel_id)
__channel_id:__ Channel ID.
None.
(this function does not raise any notable errors)
Moobius.on_spell#
Called when a “spell” from the wand is received, which can be any object but is often a string..
**Moobius.on_spell**(self, obj)
__obj:__ The wand sent this process.
None.
(this function does not raise any notable errors)
Moobius.on_message_up#
Example MessageBody object: >>> moobius.MessageBody(subtype=”text”, channel_id=<channel id>, content=MessageContent(…), timestamp=1707254706635, >>> recipients=[<user id 1>, <user id 2>], sender=<user id>, message_id=<message-id>, >>> context={‘group_id’: <group-id>, ‘channel_type’: ‘ccs’}).
**Moobius.on_message_up**(self, message)
__message:__ A message from a user.
None.
(this function does not raise any notable errors)
Moobius.on_copy_client#
Example Copy object: >>> moobius.Copy(request_id=<id>, origin_type=message_down, status=True, context={‘message’: ‘Message received’}).
**Moobius.on_copy_client**(self, copy)
__copy:__ A “Copy” request from the user.
None.
(this function does not raise any notable errors)
Moobius.on_refresh#
**Moobius.on_refresh**(self, action)
__action:__ A “Copy” request from the user.
None.
(this function does not raise any notable errors)
Moobius.on_join#
This callback happens when the user joins a channel.. Commonly used to inform everyone about this new user and update everyone’s character list.
**Moobius.on_join**(self, action)
__action:__ Channel and member id.
None.
(this function does not raise any notable errors)
Moobius.on_leave#
Called when the user leaves a channel.. Commonly used to update everyone’s character list.
**Moobius.on_leave**(self, action)
__action:__ Channel and member id.
None.
(this function does not raise any notable errors)
Moobius.on_unknown_payload#
A catch-all for handling unknown payloads..
**Moobius.on_unknown_payload**(self, payload_data)
__payload_data:__ Payload-as-dict that has not been recognized by the other handlers and may not have a format listed in types.
None.
(this function does not raise any notable errors)
Moobius.on_message_down#
Callback when the user recieves a message.. Use for user mode.
**Moobius.on_message_down**(self, message)
__message:__ Service’s MessageBody.
None.
(this function does not raise any notable errors)
Moobius.on_update_characters#
Callback when the user recieves the character list.. One of the multiple update callbacks. Use for user mode.
**Moobius.on_update_characters**(self, update)
__update:__ Service’s Update.
None.
(this function does not raise any notable errors)
Moobius.on_update_channel_info#
Callback when the user recieves the channel info.. One of the multiple update callbacks. Use for user mode.
**Moobius.on_update_channel_info**(self, update)
__update:__ Service’s Update.
None.
(this function does not raise any notable errors)
Moobius.on_update_canvas#
Callback when the user recieves the canvas content.. One of the multiple update callbacks. Use for user mode.
**Moobius.on_update_canvas**(self, update)
__update:__ Service’s Update.
None.
(this function does not raise any notable errors)
Moobius.on_update_style#
Callback when the user recieves the style info (look and feel).. One of the multiple update callbacks. Use for user mode.
**Moobius.on_update_style**(self, update)
__update:__ Service’s Update.
None.
(this function does not raise any notable errors)