Skip to content
This repository was archived by the owner on Mar 2, 2024. It is now read-only.

channel API

Gocho Mugo edited this page Aug 8, 2014 · 4 revisions

A Programmer's cheat sheet.

Channel Definition

Required:

  • __Name__: 'name_of_channel',
  • __short__: 'short_abbreviation',
  • __description__: 'short description of channel',
  • __doc__: 'documentation of the channel',
  • An initialization function of the channel named init

Optional:

  • An installation function of channel named install

Topics

  • initialization_function(aim, msg, channel)

    This function is run everytime your channel is targetted by user's commands. It serves as an entry point into your channel code execution.

    • aim that may be passed include:

      • fetch: check for new messages
      • send: send new messages
      • broadcast: broadcast message to all users
      • users: show users user can message
    • msg is a dictionary with the following keys:

      • to: List of Users to send message to. e.g. msg['to']
      • content: be a String of the message or None if none has been passed. e.g. msg['content']
    • channel is an object with the following attributes/methods:

      • username: string representing the username registered to account. e.g. myUsername = channel.username
      • read(prompt): get input from user. e.g. channel.read('Do I ask for time over there?')
      • listing(heading, items): items must be iterable. e.g. channel.listing(AllUsersList)
      • msg(sender, content): handles showing new messages to user. e.g. channel.msg('moonServer', message)
      • getpass(prompt): get password from user. e.g. key = channel.getpass('I need the key')
      • success(msg): show a success message to user. e.g. channel.success('All sent')
      • error(msg): show an error message to user. e.g. channel.error('Failed to send')
      • read_config(key): read configuration e.g. channel.read_config('isAlien')
      • store_config(key, value): store a configuration. e.g. channel.store_config('isAlien', True)

    A handler should return a tuple: True/False, 'message to show to user'

  • installation_function(channel)

    This function will be run when user is registering a new account for the channel. The function is passed a channel instance similar to the one passed to the initialization function.

    It should Return True for Success. False for Errors.

Notes/Tips:

  1. It is __Name__ and NOT __name__
  • __doc__ may either be explicitly set, for example, __doc__ = ''' ... ''' or just inferred by having the documentation (''' ... ''') appear at the beginning of the script.
  • Do Not name your script with the same name as a module you are importing.
  • It is acceptable not to handle an aim. It will just be taken as unsupported.
  • Do NOT directly output messages to user. Use the channel object to allow consistent user interface.
  • We are Consenting Adults.

Example

__Name__ = 'moonServer'
__short__ = 'mS'
__description__ = 'my Server on the Moon'
__doc__ = '''
moonServer
----------
You might wonder how I got a server on the moon? Well, lets just say "am a tourist on planet Earth"

You Ping Ping my Server, my Server Pong Pong you.
'''
__version__ = '0.99.0'
__author__ = '@gochomugo'

def init(aim, msg, channel):
    if aim is 'fetch':
        # call some function here to fetch messages from wherever
        msgs = fetch()
        for message in msgs:
            channel.msg(message['sender'], message['content'])
        return True, 'Got ALL new messages'
    elif aim is 'broadcast':
        # broadcast the message content to everyone. here we just know
        # know the moonServer
        status = sendToAllReceipeints(msg['content'])
        if status:
            return True, 'All messages sent successfully'
        else:
            return False, 'Sending failed.'
    elif aim is 'send':
        # sending a message to some one(s). Hurry...
        status = sendToManyReceipeints(msg['to'], msg['content'])
        if status:
            return True, 'All messages sent successfully'
        else:
            return False, 'Sending failed.'
    elif aim is 'users':
        # who do we know about?
        channel.listing('All users', myUsers)
        return True, 'All Users shown'

For more sensible and working scripts, check the channels branch.

Clone this wiki locally