or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

Files

docs

channel-management.mdcli-utilities.mdconfiguration.mdexceptions.mdindex.mdmanager-api.md

channel-management.mddocs/

0

# Channel Management

1

2

Channel objects provide convenient interfaces for manipulating active call channels in Asterisk. They act as proxies for channel-specific operations and variable access.

3

4

## Capabilities

5

6

### Channel Classes

7

8

#### BaseChannel

9

10

```python { .api }

11

class BaseChannel:

12

"""

13

Represents a living Asterisk channel with shortcut methods for operating on it.

14

Acts as a mapping for getting/setting channel variables.

15

"""

16

17

def __init__(manager: BaseManager, id: str):

18

"""

19

Initialize channel object.

20

21

Args:

22

manager: Manager instance for API communication

23

id: Channel identifier (e.g., 'SIP/1001-00000001')

24

"""

25

26

def __eq__(other: BaseChannel) -> bool:

27

"""Check if channels are equal (same ID and manager)."""

28

29

def __str__() -> str:

30

"""Return channel ID as string."""

31

32

def __getitem__(key: str) -> str:

33

"""Get channel variable value (translates to Getvar action)."""

34

35

def __setitem__(key: str, value: str):

36

"""Set channel variable (translates to Setvar action)."""

37

```

38

39

#### ZapChannel

40

41

```python { .api }

42

class ZapChannel(BaseChannel):

43

"""

44

Specialized channel class for Zapata/DAHDI channels.

45

Inherits all BaseChannel functionality with Zapata-specific features.

46

"""

47

```

48

49

### Channel Operations

50

51

#### Call Control

52

53

```python { .api }

54

def AbsoluteTimeout(timeout: int):

55

"""Set absolute timeout for this channel in seconds."""

56

57

def Hangup():

58

"""Hang up this channel."""

59

```

60

61

#### Monitoring and Recording

62

63

```python { .api }

64

def Monitor(pathname: str = None, format: str = 'wav', mix: bool = False):

65

"""Start monitoring/recording this channel."""

66

67

def StopMonitor():

68

"""Stop monitoring this channel."""

69

70

def ChangeMonitor(pathname: str):

71

"""Change monitor filename for this channel."""

72

```

73

74

### Getting Channel Objects

75

76

Channel objects are typically obtained through the Manager's `get_channel()` method:

77

78

```python { .api }

79

def get_channel(channel_id: str) -> BaseChannel:

80

"""

81

Get channel object for the given channel ID.

82

Returns ZapChannel for Zapata/DAHDI channels, BaseChannel otherwise.

83

"""

84

```

85

86

## Usage Examples

87

88

### Basic Channel Operations

89

90

```python

91

from Asterisk.Manager import Manager

92

93

manager = Manager(('127.0.0.1', 5038), 'admin', 'secret')

94

95

# Get channel object

96

channel = manager.get_channel('SIP/1001-00000001')

97

98

# Hang up the channel

99

channel.Hangup()

100

101

# Set timeout

102

channel.AbsoluteTimeout(300) # 5 minutes

103

```

104

105

### Working with Channel Variables

106

107

```python

108

# Get channel variable

109

caller_id = channel['CALLERID(name)']

110

print(f"Caller ID: {caller_id}")

111

112

# Set channel variable

113

channel['MY_CUSTOM_VAR'] = 'some_value'

114

115

# Common channel variables

116

print(f"Channel state: {channel['CHANNEL(state)']}")

117

print(f"Call duration: {channel['CDR(duration)']}")

118

print(f"Source: {channel['CDR(src)']}")

119

print(f"Destination: {channel['CDR(dst)']}")

120

```

121

122

### Channel Monitoring

123

124

```python

125

# Start monitoring call

126

channel.Monitor('/var/spool/asterisk/monitor/call_123', 'wav', mix=True)

127

128

# Change recording filename

129

channel.ChangeMonitor('/var/spool/asterisk/monitor/important_call_123')

130

131

# Stop monitoring

132

channel.StopMonitor()

133

```

134

135

### Working with Multiple Channels

136

137

```python

138

# Get all active channels

139

channels_info = manager.ShowChannels()

140

141

# Process each channel

142

for channel_data in channels_info.get('channels', []):

143

channel_id = channel_data['Channel']

144

channel = manager.get_channel(channel_id)

145

146

# Check if channel has been active too long

147

duration = int(channel.get('CDR(billsec)', 0))

148

if duration > 3600: # 1 hour

149

print(f"Long call detected: {channel_id} ({duration}s)")

150

151

# Optionally hang up or take other action

152

# channel.Hangup()

153

```

154

155

### Zapata/DAHDI Channels

156

157

```python

158

# For Zapata/DAHDI channels, you get a ZapChannel object

159

zap_channel = manager.get_channel('Zap/1-1')

160

161

# All BaseChannel methods work

162

zap_channel.Hangup()

163

zap_channel['CALLERID(name)'] = 'Updated Name'

164

165

# ZapChannel automatically returned for channels starting with 'Zap'

166

assert isinstance(zap_channel, ZapChannel)

167

```