Package Tools¶
Here we will list some methods that may be useful during the development of a Jaspion project.
Filtrate¶
This method is a decorator. When added to a function that will be used as a handler for FreeSwitch events, it ensures that this function will only process events that have the entered key and that have the value associated with that key.
Argument |
Type |
Required |
Default |
key |
str |
True |
N/A |
value |
str |
True |
N/A |
regex |
bool |
False |
False |
Example whit key and value¶
1 2 3 4 5 6 7 8 | @app.handle('sofia::register')
@filtrate('from-user', '1000')
def register(event):
domain = event['from-host']
username = event['from-user']
date = event['Event-Date-Local']
print(f'[{date}] {username}@{domain} - Registred.')
|
Example whit key only¶
1 2 3 4 5 6 7 8 | @app.handle('sofia::register')
@filtrate('from-user')
def register(event):
domain = event['from-host']
username = event['from-user']
date = event['Event-Date-Local']
print(f'[{date}] {username}@{domain} - Registred.')
|
Example whit key and regex in value¶
1 2 3 4 5 6 7 8 | @app.handle('sofia::register')
@filtrate('from-user', '^1[0-9]{3}$', regex=True)
def register(event):
domain = event['from-host']
username = event['from-user']
date = event['Event-Date-Local']
print(f'[{date}] {username}@{domain} - Registred.')
|
AbstractBaseHandler¶
Abstract Base Bandler is an ABC for those who want to create more robust handlers for events.
Where is?¶
You can use ABC you should import the same from jaspion.abc
. See an example below.
Show me the code¶
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 | from jaspion.abc import AbstractBaseHandler
from jaspion import Jaspion
class SimpleHandler(AbstractBaseHandler):
def setup(self):
print('Hello, I am a setup.')
def handle(self):
print('event:')
print(self.event)
def finish(self):
print('Finishing the processing.')
app = Jaspion(host='127.0.0.1',
port=8021,
password='ClueCon')
app['HEARTBEAT'] = SimpleHandler
if __name__ == '__main__':
app.run()
|
Understanding the example¶
First of all let’s understand the SimpleHandler class:
It is a subclass of our ABC AbstractBaseHandler.
Three methods were implemented: setup, handle and finish.
the SetUp method is called before starting the event processing and is optional.
The handle method is mandatory and event processing must be contained in it.
Similar to the setup method, the finish method serves to finalize event processing and is also an optional method.
Similar to a dictionary, to add a class to a certain event you should use
app[event] = class
.
Comments¶
The event instance to be processed will always be a dictionary referenced in
self.event
.To add more than one handler to a given event, use one list.
Command¶
The command method serves as a simple abstraction for sending commands to freeswitch using the API.
Example¶
1 2 3 4 5 6 7 8 | @app.handle("HEARTBEAT", client=True)
def heartbeat(client, event):
now = event["Event-Date-Local"]
job = agent.command("show_users", background=True)
print('[%s] Start job "%s".' % (now, job))
# Output
# [20XX-XX-XX XX:XX:XX] Start job "a422df1f-080d-4d50-a362-be80676bd2ed".
|