Skype - World Time

Skype - World Time

Skype is a software application that allows registered users to communicate with each other through video calls and instant messaging. While making international calls, the caller will always prefer to keep the time difference in mind. This sample demonstrates how Xtend IVR can be used to get the world time via Skype. Utilise this scripting language to screen scrape information from a website.

Download the evaluation version of Xtend IVR and install the telephony application in your system. Run the sample script from the Script Editor. Click here to refer the code.

The automated attendant will work as given below:

  • Initially, IVR loads the mapped country code
  • IVR waits for the chat message
  • On receiving the chat, IVR displays the chat message and removes the unwanted leading and trailing white spaces
  • IVR prompts the user to enter the country name with at least 3 characters
  • If there is an exact match, then IVR gets the time from the website and append it with the response string
  • If there is no exact match, then IVR tries to match the closest word and based on that returns all matching results
Download the source file zip download for the Skype - WorldTime


LoadFile("CountryMap.txt","$CountryMap")


MAIN:
	Display "Waiting for chat . . ."
		
	
	while !Skype.GetChatMessage()
		SleepEvent 
	endwhile
		
	
	Display  "Received message from " $SkypeChat.FROM_HANDLE " -> " $SkypeChat.
	
	
	$Country = $SkypeChat.BODY
	$Country = alltrim($Country)
	
	
	if len($Country) < 3
		Skype.SendChatMessage($SkypeChat.CHATNAME, "Please send me the name of 
			the country for which you require the current time.")
		goto MAIN
	endif
	
	
	$Response = ScanCountryMap($Country)
	if $Response == ""
		
		$Response = ScanCountryMap($Country, true)
	endif
	
	if $Response == ""
		$Response = Format("I do not know the current time for country %s. 
		It is possible that this country has many time zones. 
		Please try a specific state or city in %s", $Country, $Country)
	endif
	Skype.SendChatMessage($SkypeChat.CHATNAME, $Response)
	goto MAIN


Function ScanCountryMap($Country, $MatchAnywhere)	
	
	$LineNumber = 1
	$ResponseStr = ""
	while $LineNumber <= $CountryMap.Filelines
		
		$CurrLine = Format("$CountryMap.Line%d",$LineNumber)
		if Find(Upper($$CurrLine),Upper($Country)) >= 0
			
			$pos = Find($$CurrLine," ")
			if $pos >= 0
				
				$CountryCode = Left($$CurrLine,$pos)
				$CountryName = Mid($$CurrLine,$pos+1)
				
				if !$MatchAnywhere 
					if upper(alltrim($CountryName)) <> 
							upper(alltrim($Country))
						$LineNumber += 1
						continue
					endif
				endif
				
				Display Format("(%s) Looking up time for %s",
							$Country, $CountryName)
				
				
				$CurrTime = GetTimeFromWorldTimeServer($CountryCode)
				if $CurrTime == ""
					$LineNumber += 1
					continue
				endif
				
				
				$Str = Format("The current time in %s is %s", 
							$CountryName, $CurrTime)
				if $ResponseStr = ""
					$ResponseStr = $Str
				else
					$ResponseStr = Format("%s%c%s",$ResponseStr,
								chr(10),$Str)
				endif
			endif
		endif
		$LineNumber += 1
	endwhile
return $ResponseStr


Function GetTimeFromWorldTimeServer($CountryCode)
	
	if http(Format("http://www.worldtimeserver.com/time.asp?
					locationid=%s",$CountryCode)) <> TRUE
		return ""
	endif
	
	
	
	$key = '<span class="font7">'
	$endkey = '</span>'
	
	
	$pos = Find($http.Page, $key)
	if $pos < 0
		return ""
	endif
	
	$pos += len($key)
	$CurrTime = Mid($http.Page, $pos)
	
	$pos = Find($CurrTime,$endkey)
	if $pos < 0
		return ""
	endif
	
	
	$CurrTime = Mid($CurrTime,0,$pos)
	$pos = find($CurrTime,":")
	if $pos < 0
		return ""
	endif
	
	$hh = mid($CurrTime,0,$pos)
	$pos += 1
	$mm = mid($CurrTime,$pos)
	$pos = find($mm,' ')
	if $pos < 0
		return ""
	endif
	$mm = mid($mm,0,$pos)
	
	$pos = reversefind($CurrTime,' ')
	if $pos < 0
		return ""
	endif
	$ampm = upper(mid($CurrTime, $pos+1, 2))
	
	$Str = Format("%d:%02d %s", $hh,$mm,$ampm)
return $Str



ONHANGUP:
	hangup
	goto MAIN



ONSYSTEMERROR:
	log $error
	display $error
	if $debug 
		msgbox $error
	endif
	hangup
	goto MAIN