Portiere Time-Plugin
This commit is contained in:
		| @@ -1,4 +1,4 @@ | |||||||
| # Brawlbot, auf Basis von otouto | # otouto | ||||||
| The plugin-wielding, multipurpose Telegram bot. | The plugin-wielding, multipurpose Telegram bot. | ||||||
|  |  | ||||||
| [Public Bot](http://telegram.me/mokubot) | [Official Channel](http://telegram.me/otouto) | [Development Group](http://telegram.me/BotDevelopment) | [Public Bot](http://telegram.me/mokubot) | [Official Channel](http://telegram.me/otouto) | [Development Group](http://telegram.me/BotDevelopment) | ||||||
|   | |||||||
| @@ -4,27 +4,58 @@ local HTTPS = require('ssl.https') | |||||||
| local JSON = require('dkjson') | local JSON = require('dkjson') | ||||||
| local utilities = require('otouto.utilities') | local utilities = require('otouto.utilities') | ||||||
|  |  | ||||||
| time.command = 'time <location>' | time.command = 'time <Ort>' | ||||||
|  |  | ||||||
| function time:init(config) | function time:init(config) | ||||||
| 	time.triggers = utilities.triggers(self.info.username, config.cmd_pat):t('time', true).table | 	time.triggers = utilities.triggers(self.info.username, config.cmd_pat):t('time', true).table | ||||||
| 	time.doc = [[``` | 	time.doc = [[* | ||||||
| ]]..config.cmd_pat..[[time <location> | ]]..config.cmd_pat..[[time*: Aktuelle Zeit in Deutschland | ||||||
| Returns the time, date, and timezone for the given location. | *]]..config.cmd_pat..[[time* _<Ort>_: Gibt Zeit an diesem Ort aus]] | ||||||
| ```]] | end | ||||||
|  |  | ||||||
|  | function time:localize(output) | ||||||
|  |   -- Days | ||||||
|  |   local output = string.gsub(output, "Monday", "Montag") | ||||||
|  |   local output = string.gsub(output, "Tuesday", "Dienstag") | ||||||
|  |   local output = string.gsub(output, "Wednesday", "Mittwoch") | ||||||
|  |   local output = string.gsub(output, "Thursday", "Donnerstag") | ||||||
|  |   local output = string.gsub(output, "Friday", "Freitag") | ||||||
|  |   local output = string.gsub(output, "Saturday", "Samstag") | ||||||
|  |   local output = string.gsub(output, "Sunday", "Sonntag") | ||||||
|  | 	 | ||||||
|  | 	-- Months | ||||||
|  |   local output = string.gsub(output, "January", "Januar") | ||||||
|  |   local output = string.gsub(output, "February", "Februar") | ||||||
|  |   local output = string.gsub(output, "March", "März") | ||||||
|  |   local output = string.gsub(output, "April", "April") | ||||||
|  |   local output = string.gsub(output, "May", "Mai") | ||||||
|  |   local output = string.gsub(output, "June", "Juni") | ||||||
|  |   local output = string.gsub(output, "July", "Juli") | ||||||
|  |   local output = string.gsub(output, "August", "August") | ||||||
|  |   local output = string.gsub(output, "September", "September") | ||||||
|  |   local output = string.gsub(output, "October", "Oktober") | ||||||
|  |   local output = string.gsub(output, "November", "November") | ||||||
|  |   local output = string.gsub(output, "December", "Dezember") | ||||||
|  | 	 | ||||||
|  | 	-- Timezones | ||||||
|  |   local output = string.gsub(output, "Africa", "Afrika") | ||||||
|  |   local output = string.gsub(output, "America", "Amerika") | ||||||
|  |   local output = string.gsub(output, "Asia", "Asien") | ||||||
|  |   local output = string.gsub(output, "Australia", "Australien") | ||||||
|  |   local output = string.gsub(output, "Europe", "Europa") | ||||||
|  |   local output = string.gsub(output, "Indian", "Indien") | ||||||
|  |   local output = string.gsub(output, "Pacific", "Pazifik") | ||||||
|  |    | ||||||
|  |   return output | ||||||
| end | end | ||||||
|  |  | ||||||
| function time:action(msg, config) | function time:action(msg, config) | ||||||
|  |  | ||||||
|   local input = utilities.input(msg.text) |   local input = utilities.input(msg.text) | ||||||
|   if not input then |   if not input then | ||||||
| 		if msg.reply_to_message and msg.reply_to_message.text then |     local output = os.date("%A, %d. %B %Y, *%H:%M:%S Uhr*") | ||||||
| 			input = msg.reply_to_message.text | 	utilities.send_reply(self, msg, time:localize(output), true) | ||||||
| 		else |  | ||||||
| 			utilities.send_message(self, msg.chat.id, time.doc, true, msg.message_id, true) |  | ||||||
| 	return | 	return | ||||||
|   end |   end | ||||||
| 	end |  | ||||||
|  |  | ||||||
|   local coords = utilities.get_coords(input, config) |   local coords = utilities.get_coords(input, config) | ||||||
|   if type(coords) == 'string' then |   if type(coords) == 'string' then | ||||||
| @@ -35,8 +66,7 @@ function time:action(msg, config) | |||||||
|   local now = os.time() |   local now = os.time() | ||||||
|   local utc = os.time(os.date("!*t", now)) |   local utc = os.time(os.date("!*t", now)) | ||||||
|  |  | ||||||
| 	local url = 'https://maps.googleapis.com/maps/api/timezone/json?location=' .. coords.lat ..','.. coords.lon .. '×tamp='..utc |   local url = 'https://maps.googleapis.com/maps/api/timezone/json?location=' .. coords.lat ..','.. coords.lon .. '×tamp='..utc..'&language=de' | ||||||
|  |  | ||||||
|   local jstr, res = HTTPS.request(url) |   local jstr, res = HTTPS.request(url) | ||||||
|   if res ~= 200 then |   if res ~= 200 then | ||||||
|     utilities.send_reply(self, msg, config.errors.connection) |     utilities.send_reply(self, msg, config.errors.connection) | ||||||
| @@ -44,7 +74,7 @@ function time:action(msg, config) | |||||||
|   end |   end | ||||||
|    |    | ||||||
|   local jdat = JSON.decode(jstr) |   local jdat = JSON.decode(jstr) | ||||||
|  |   local timezoneid = '*'..string.gsub(jdat.timeZoneId, '_', ' ' )..'*' | ||||||
|   local timestamp = now + jdat.rawOffset + jdat.dstOffset |   local timestamp = now + jdat.rawOffset + jdat.dstOffset | ||||||
|   local utcoff = (jdat.rawOffset + jdat.dstOffset) / 3600 |   local utcoff = (jdat.rawOffset + jdat.dstOffset) / 3600 | ||||||
|   if utcoff == math.abs(utcoff) then |   if utcoff == math.abs(utcoff) then | ||||||
| @@ -52,11 +82,13 @@ function time:action(msg, config) | |||||||
|   else |   else | ||||||
|     utcoff = utilities.pretty_float(utcoff) |     utcoff = utilities.pretty_float(utcoff) | ||||||
|   end |   end | ||||||
| 	local output = os.date('!%I:%M %p\n', timestamp) .. os.date('!%A, %B %d, %Y\n', timestamp) .. jdat.timeZoneName .. ' (UTC' .. utcoff .. ')' |   -- "%A, %d. %B %Y, %H:%M:%S Uhr" | ||||||
| 	output = '```\n' .. output .. '\n```' |   local output = timezoneid..':\n'..os.date('!%A, %d. %B %Y, %H:%M:%S Uhr',timestamp) | ||||||
|  |   local output = time:localize(output) | ||||||
|  |    | ||||||
|  |   local output = output..'\n_'..jdat.timeZoneName .. ' (UTC' .. utcoff .. ')_' | ||||||
| 	 | 	 | ||||||
|   utilities.send_reply(self, msg, output, true) |   utilities.send_reply(self, msg, output, true) | ||||||
|  |  | ||||||
| end | end | ||||||
|  |  | ||||||
| return time | return time | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user
	 Andreas Bielawski
					Andreas Bielawski