Friday, May 1, 2009

IPY as AMI client thru AMIConnector

One of the thing I like about IPY or python programming. If a class is professionally crafted, the info on how to use methods is readily available. On this post I explore the library AMIConnector.
An access to AMI(Asterisk Manager Interface) is needed to run a script that uses this library. But since I have no Asterisk access whatsoever, I will be contended exploring the methods use to originate a call.


>>> import clr
>>> clr.AddReferenceToFileAndPath("C:\IronPython 2.0.1\Lib\site-packages\AMI.Con
nector.14.dll")
>>> import AMI_Connector_14.AMI_Connector as Ami14
>>> dir(Ami14)
['ActionResponse_ExtensionState', 'ActionResponse_ExtensionStateEventHandler', '
Action_AgentCallBackLogin', 'Action_AgentLogoff', 'Action_ExtensionState', 'Acti
on_HangUp', 'Action_Originate', 'Action_ParkedCalls', 'Action_QueueAdd', 'Action
_QueuePause', 'Action_QueueRemove', 'Action_QueueStatus', 'Action_Redirect', 'Ag
ents', 'AsteriskVersion', 'CanRaiseEvents', 'Connect', 'Connected', 'Container',
'CreateObjRef', 'DesignMode', 'Disconnect', 'Disconnected', 'DisconnectedEventH
andler', 'Dispose', 'Disposed', 'Equals', 'Evaluation', 'Event_Agents', 'Event_A
gentsComplete', 'Event_AgentsCompleteEventHandler', 'Event_AgentsEventHandler',
'Event_Cdr', 'Event_CdrEventHandler', 'Event_Dial', 'Event_DialEventHandler', 'E
vent_ExtensionStatus', 'Event_ExtensionStatusEventHandler', 'Event_HangUp', 'Eve
nt_HangUpEventHandler', 'Event_Join', 'Event_JoinEventHandler', 'Event_Leave', '
Event_LeaveEventHandler', 'Event_Link', 'Event_LinkEventHandler', 'Event_Login',
'Event_LoginEventHandler', 'Event_MeetmeJoin', 'Event_MeetmeJoinEventHandler',
'Event_MeetmeLeave', 'Event_MeetmeLeaveEventHandler', 'Event_MeetmeStopTalking',
'Event_MeetmeStopTalkingEventHandler', 'Event_MeetmeTalking', 'Event_MeetmeTalk
ingEventHandler', 'Event_MusicOnHold', 'Event_MusicOnHoldEventHandler', 'Event_N
ewChannel', 'Event_NewChannelEventHandler', 'Event_NewState', 'Event_NewStateEve
ntHandler', 'Event_Newcallerid', 'Event_NewcalleridEventHandler', 'Event_Newexte
n', 'Event_NewextenEventHandler', 'Event_ParkedCall', 'Event_ParkedCallEventHand
ler', 'Event_PeerStatus', 'Event_PeerStatusEventHandler', 'Event_QueueMember', '
Event_QueueMemberAdded', 'Event_QueueMemberAddedEventHandler', 'Event_QueueMembe
rEventHandler', 'Event_QueueMemberPaused', 'Event_QueueMemberPausedEventHandler'
, 'Event_QueueMemberRemoved', 'Event_QueueMemberRemovedEventHandler', 'Event_Que
ueMemberStatus', 'Event_QueueMemberStatusEventHandler', 'Event_QueueParams', 'Ev
ent_QueueParamsEventHandler', 'Event_QueueStatusComplete', 'Event_QueueStatusCom
pleteEventHandler', 'Event_Registry', 'Event_RegistryEventHandler', 'Event_Renam
e', 'Event_RenameEventHandler', 'Event_UnParkedCall', 'Event_UnParkedCallEventHa
ndler', 'Event_Unlink', 'Event_UnlinkEventHandler', 'Events', 'GetHashCode', 'Ge
tLifetimeService', 'GetService', 'GetType', 'HostIP', 'HostPort', 'InitializeLif
etimeService', 'LicenceAmi', 'MemberwiseClone', 'ReferenceEquals', 'SearchInBloc
k', 'SendInformation', 'Site', 'ToString', 'User', '__class__', '__delattr__', '
__doc__', '__enter__', '__exit__', '__getattribute__', '__hash__', '__init__', '
__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__str__']
>>> dir(Ami14.Connect)
['Equals', 'GetHashCode', 'GetType', 'MemberwiseClone', 'ReferenceEquals', 'ToSt
ring', '__call__', '__class__', '__cmp__', '__delattr__', '__delete__', '__doc__
', '__get__', '__getattribute__', '__hash__', '__init__', '__name__', '__new__',
'__objclass__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__st
r__']

Of course it will be really difficult if you have no API documents (no examples) or knowledge what's the library all about. In my case, I'm an Asterisk fan way back 2003. So its pretty easy for me how to make this script connect and place(originate) a call.

>>> Ami14.HostIP.__doc__
'Get: str HostIP(self)\r\nSet: HostIP(self) = value\r\n'
>>> Ami14.HostPort.__doc__
'Get: str HostPort(self)\r\nSet: HostPort(self) = value\r\n'
>>> Ami14.Connect.__doc__
'Connect(self, str UserAMI, str PasswordAMI)'
>>>

The above code is straightforward its use to connect to the AMI. While the two method below places the call and ultimately disconnect the script from AMI.


>>> Ami14.Action_Originate.__doc__
'Action_Originate(self, str Channel, str Context, str Exten, str CallerID)'
>>>
>>> Ami14.Disconnect.__doc__
'Disconnect(self)'

On the next post I will do form and actual codes that uses AMIConnector library.

Thursday, March 19, 2009

IPY with Google Data Calendar API

Here's a code that I've done while learning the language. In this python code I explored the Google Calendar through its API. Creating an instance of the service,ClientLogin and retrieving entries were pretty much straightforward. But creating,updating and deleting calendar's event(or entries) really took me sometime to make it work. On creating entries, I had a hard time coding the this
Insert(TEntry)(Uri, TEntry)
to python. It took a help from IPY mailing list to get on with my programming. On updating and deleting entries, I thought it would be straightforward but it's not. But, anyway I figured it out how to delete or update entries.

Below is the class that explore the Google Calendar.

class googleCal:

def __init__(self,googleacct,googlepasswd):
"Login to google"
self.calsvc = GDCal.CalendarService("myapp")
self.calsvc.setUserCredentials(googleacct,googlepasswd)
return self.calsvc
def doentries(self,myURL,date2post,myTitle,myContent):
# define srch range for retrieval
postdate = DateTime.Parse(date2post)
myQuery = GDCal.EventQuery(myURL)
myQuery.StartTime = postdate
myQuery.EndTime = postdate.AddMinutes(1438)
myevFeed = self.calsvc.Query(myQuery)

if myevFeed.Entries.Count > 0:
for myentry in myevFeed.Entries:
# first entry and only entry need to update or delete
print "Old Entry: " + myentry.Title.Text
###########################################################
# to delete entry
#myentry.Delete <--- not working
###########################################################
self.calsvc.Delete(myentry)
print "Succesfully deleted\n"
############################################################
# to update entry
#myentry.Title.Text = myTitle
#myentry.Content.Content = myContent
#myentry.Update <-- not working
#self.mycalsvc.Update[type(myentry)](myentry) <-- working
############################################################
break
else:
pass

# delete and create that's the idea not update
myentry = GDCal.EventEntry()
myUri = Uri(myURL)
myentry.Title.Text = myTitle
myentry.Content.Content = myContent
if DateTime.Compare(postdate,DateTime.Today) == 0:
evWhento = DateTime.Now
else:
evWhento = postdate.AddHours(22)
evTime = GDxtn.When(evWhento,evWhento.AddMinutes(60))
myentry.Times.Add(evTime)
self.calsvc.Insert[type(myentry)](myUri,myentry)


and the rest of the code that uses the class

if len(sys.argv) < 2:
dtofs = 0
else:
dtofs = int(sys.argv[1])

mydate = CDRdate(dtofs)
print "Date: " + mydate.rtndate()
CDR = getCDR(mydate.rtndate())
sfields = CDR.rtnCDR()
eTitle = "Dumb Bill: $%3.2f" % sfields[2]
eContent = "CallCount: %d\nTotalLength: %d:%d\nBill: $%3.2f\n" % (sfields[0],(sfields[1]/60),(sfields[1]%60),sfields[2])
feedURL = "http://www.google.com/calendar/feeds/sometext here%40group.calendar.google.com/private/full"
print eTitle
print eContent
gcalsvc = googleCal(username here,password here)
gcalsvc.doentries(feedURL,mydate.rtndate(),eTitle,eContent)
print "New Entry: " + eTitle
print "Successfully Inserted\n"

Thursday, March 12, 2009

IronPython Studio and SD

Both product were great but both have bug (as of this writing) that makes wannabe pythonistas or pythoneers life difficult.

In my experience, IP Studio in the middle of coding and debugging will eventually reset the design form to just blank form. Double clicking on the blank form erases my codes.

On SharpDevelop, I could live without a design tab. But, not with this bug. It took me sometimes to figure out that there's nothing wrong with my codes by running it from the commandline.

With these bugs how other pythonistas does it? Me, I rely on IP Studio to generate all codes related to the form. Or just cut and paste the codes I found on the web to make thing more easier. Then run it from the commandline, let alone the ipy check for errors.


Followers

About Me

An underrated techie. With great interest in digital electronics, photography(minus photoshop and others stuff) and audio-video engineering. With more than 10 years of IT working experience ranging from systems and network administration, programming and Voice-Over-IP engineering.