Tip/Question About Encoding Temporary Data For Storage In Asterisk Variable To Use In AGI

Home » Asterisk Users » Tip/Question About Encoding Temporary Data For Storage In Asterisk Variable To Use In AGI
Asterisk Users No Comments

I wanted to store a JSON object between agi requests for the duration of a call.

Turns out asterisk does NOT like a stringified JSON object! AGI complains of “520-Invalid command syntax”

So, I just base64 encode/decode it.

Assuming I don’t need to manipulate the JSON object within Asterisk itself, and I don’t want to use a DB or memcache, is this the best/correct method?
WARNING! Asterisk truncates long variables – seems to be about 3000
characters or so(?), so make sure your base64 encoded object is small!

The following is a simple example which just encodes and decodes the agi variables themselves as they’re an object in ts-agi
(I’m using the ts-agi node package here as an example –
https://github.com/sergey12313/ts-agi/ )

await ctx.setVariable(‘testvar2’, Buffer.from(JSON.stringify(ctx.variables), ‘utf8’).toString(‘base64’))
const testvar2 = (await ctx.getVariable(‘testvar2’)).value
const decoded = JSON.parse(Buffer.from(testvar2,
‘base64’).toString(‘utf8’))
console.log(‘decoded is’, decoded)

Hope this helps someone!