- Portiere Golem und MyAnimeList
- Fixe Help-Plugin - Fixe Kommandos für Forecast & Wetter
This commit is contained in:
		| @@ -44,6 +44,7 @@ function bot:init(config) -- The function run when the bot is started or reloade | ||||
| 	    self.plugins[k].name = v | ||||
| 		if p.init then p.init(self, config) end | ||||
| 	end | ||||
| 	 | ||||
| 	print('Bot started successfully as:\n@' .. self.info.username .. ', AKA ' .. self.info.first_name ..' ('..self.info.id..')') | ||||
|  | ||||
| 	self.last_update = self.last_update or 0 -- Set loop variables: Update offset, | ||||
|   | ||||
| @@ -3,6 +3,8 @@ local cats = {} | ||||
| local HTTP = require('socket.http') | ||||
| local utilities = require('otouto.utilities') | ||||
|  | ||||
| cats.command = 'cat [gif]' | ||||
|  | ||||
| function cats:init(config) | ||||
| 	if not cred_data.cat_apikey then | ||||
| 		print('Missing config value: cat_apikey.') | ||||
| @@ -19,7 +21,7 @@ function cats:init(config) | ||||
| *]]..config.cmd_pat..[[cat* _gif_: Postet eine zufällige, animierte Katze]] | ||||
| end | ||||
|  | ||||
| cats.command = 'cat [gif]' | ||||
|  | ||||
| local apikey = cred_data.cat_apikey or "" -- apply for one here: http://thecatapi.com/api-key-registration.html | ||||
|  | ||||
| function cats:action(msg, config) | ||||
|   | ||||
| @@ -36,7 +36,7 @@ function forecast:init(config) | ||||
| ]] | ||||
| end | ||||
|  | ||||
| forecast.command = 'forecast' | ||||
| forecast.command = 'f [Ort]' | ||||
|  | ||||
| local BASE_URL = "https://api.forecast.io/forecast" | ||||
| local apikey = cred_data.forecastio_apikey | ||||
|   | ||||
							
								
								
									
										59
									
								
								otouto/plugins/golem.lua
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										59
									
								
								otouto/plugins/golem.lua
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,59 @@ | ||||
| local golem = {} | ||||
|  | ||||
| local http = require('socket.http') | ||||
| local json = require('dkjson') | ||||
| local utilities = require('otouto.utilities') | ||||
| local bindings = require('otouto.bindings') | ||||
|  | ||||
| function golem:init(config) | ||||
| 	if not cred_data.golem_apikey then | ||||
| 		print('Missing config value: golem_apikey.') | ||||
| 		print('golem.lua will not be enabled.') | ||||
| 		return | ||||
| 	end | ||||
|  | ||||
|   golem.triggers = { | ||||
| 	"golem.de/news/([A-Za-z0-9-_-]+)-(%d+).html" | ||||
|   } | ||||
| end | ||||
|  | ||||
| local BASE_URL = 'http://api.golem.de/api' | ||||
|  | ||||
| function golem:get_golem_data (article_identifier) | ||||
|   local apikey = cred_data.golem_apikey | ||||
|   local url = BASE_URL..'/article/meta/'..article_identifier..'/?key='..apikey..'&format=json' | ||||
|   local res,code  = http.request(url) | ||||
|   if code ~= 200 then return "HTTP-FEHLER" end | ||||
|   local data = json.decode(res).data | ||||
|   return data | ||||
| end | ||||
|  | ||||
| function golem:send_golem_data(data) | ||||
|   local headline = '*'..data.headline..'*' | ||||
|   if data.subheadline ~= "" then | ||||
|     subheadline = '\n_'..data.subheadline..'_' | ||||
|   else | ||||
|     subheadline = "" | ||||
|   end | ||||
|   local subheadline = data.subheadline | ||||
|   local abstracttext = data.abstracttext | ||||
|   local text = headline..subheadline..'\n'..abstracttext | ||||
|   local image_url = data.leadimg.url | ||||
|   return text, image_url | ||||
| end | ||||
|  | ||||
| function golem:action(msg, config, matches) | ||||
|   local article_identifier = matches[2] | ||||
|   local data = golem:get_golem_data(article_identifier) | ||||
|   if not data then utilities.send_reply(self, msg, config.errors.connection) return end | ||||
|   local text, image_url = golem:send_golem_data(data) | ||||
|    | ||||
|   if image_url then | ||||
|     utilities.send_typing(self, msg.chat.id, 'upload_photo') | ||||
|     local file = download_to_file(image_url) | ||||
|     utilities.send_photo(self, msg.chat.id, file, nil, msg.message_id) | ||||
|   end | ||||
|   utilities.send_reply(self, msg, text, true) | ||||
| end | ||||
|  | ||||
| return golem | ||||
| @@ -8,12 +8,17 @@ local utilities = require('otouto.utilities') | ||||
| local help_text | ||||
|  | ||||
| function help:init(config) | ||||
|   help.triggers = utilities.triggers(self.info.username, config.cmd_pat):t('hilfe', true):t('help', true).table | ||||
| end | ||||
|  | ||||
| function help:action(msg, config) | ||||
|  | ||||
| 	local commandlist = {} | ||||
| 	help_text = '*Verfügbare Befehle:*\n• '..config.cmd_pat | ||||
|  | ||||
| 	for _,plugin in ipairs(self.plugins) do | ||||
| 		if plugin.command then | ||||
| 		     | ||||
| 			table.insert(commandlist, plugin.command) | ||||
| 			--help_text = help_text .. '\n• '..config.cmd_pat .. plugin.command:gsub('%[', '\\[') | ||||
| 		end | ||||
| @@ -21,17 +26,9 @@ function help:init(config) | ||||
|  | ||||
| 	table.insert(commandlist, 'hilfe [Plugin]') | ||||
| 	table.sort(commandlist) | ||||
|  | ||||
| 	help_text = help_text .. table.concat(commandlist, '\n• '..config.cmd_pat) .. '\nParameter: <benötigt> [optional]' | ||||
|  | ||||
| 	help_text = help_text:gsub('%[', '\\[') | ||||
|  | ||||
| 	help.triggers = utilities.triggers(self.info.username, config.cmd_pat):t('hilfe', true):t('help', true).table | ||||
|  | ||||
| end | ||||
|  | ||||
| function help:action(msg) | ||||
|  | ||||
| 	local input = utilities.input(msg.text_lower) | ||||
|  | ||||
| 	-- Attempts to send the help message via PM. | ||||
|   | ||||
							
								
								
									
										230
									
								
								otouto/plugins/myanimelist.lua
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										230
									
								
								otouto/plugins/myanimelist.lua
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,230 @@ | ||||
| local mal = {} | ||||
|  | ||||
| local http = require('socket.http') | ||||
| local URL = require('socket.url') | ||||
| local xml = require("xml")  | ||||
| local utilities = require('otouto.utilities') | ||||
| local bindings = require('otouto.bindings') | ||||
|  | ||||
| mal.command = 'anime <Anime>, /manga <Manga>' | ||||
|  | ||||
| function mal:init(config) | ||||
| 	if not cred_data.mal_user then | ||||
| 		print('Missing config value: mal_user.') | ||||
| 		print('myanimelist.lua will not be enabled.') | ||||
| 		return | ||||
| 	elseif not cred_data.mal_pw then | ||||
| 		print('Missing config value: mal_pw.') | ||||
| 		print('myanimelist.lua will not be enabled.') | ||||
| 		return | ||||
| 	end | ||||
|  | ||||
|   mal.triggers = { | ||||
| 	"^/(anime) (.+)$", | ||||
| 	"myanimelist.net/(anime)/[0-9]+/(.*)$", | ||||
| 	"^/(manga) (.+)$", | ||||
| 	"myanimelist.net/(manga)/[0-9]+/(.*)$" | ||||
| 	} | ||||
|   mal.doc = [[* | ||||
| ]]..config.cmd_pat..[[anime*_ <Anime>_: Sendet Infos zum Anime | ||||
| *]]..config.cmd_pat..[[manga*_ <Manga>_: Sendet Infos zum Manga | ||||
| ]] | ||||
| end | ||||
|  | ||||
| local user = cred_data.mal_user | ||||
| local password = cred_data.mal_pw | ||||
|  | ||||
| local BASE_URL = 'http://'..user..':'..password..'@myanimelist.net/api' | ||||
|  | ||||
| function mal:delete_tags(str) | ||||
|   str = string.gsub( str, '<br />', '') | ||||
|   str = string.gsub( str, '%[i%]', '') | ||||
|   str = string.gsub( str, '%[/i%]', '') | ||||
|   str = string.gsub( str, '—', ' — ') | ||||
|   return str | ||||
| end | ||||
|  | ||||
| local makeOurDate = function(dateString) | ||||
|   local pattern = "(%d+)%-(%d+)%-(%d+)" | ||||
|   local year, month, day = dateString:match(pattern) | ||||
|   return day..'.'..month..'.'..year | ||||
| end | ||||
|  | ||||
| function mal:get_mal_info(query, typ) | ||||
|   if typ == 'anime' then | ||||
|     url = BASE_URL..'/anime/search.xml?q='..query | ||||
|   elseif typ == 'manga' then | ||||
|     url = BASE_URL..'/manga/search.xml?q='..query | ||||
|   end | ||||
|   local res,code  = http.request(url) | ||||
|   if code ~= 200 then return "HTTP-Fehler" end | ||||
|   local result = xml.load(res) | ||||
|   return result | ||||
| end | ||||
|  | ||||
| function mal:send_anime_data(result, receiver) | ||||
|   local title = xml.find(result, 'title')[1] | ||||
|   local id = xml.find(result, 'id')[1] | ||||
|   local mal_url = 'http://myanimelist.net/anime/'..id | ||||
|    | ||||
|   if xml.find(result, 'synonyms')[1] then | ||||
|     alt_name = '\noder: '..unescape(mal:delete_tags(xml.find(result, 'synonyms')[1])) | ||||
|   else | ||||
|     alt_name = '' | ||||
|   end | ||||
|    | ||||
|   if xml.find(result, 'synopsis')[1] then | ||||
|     desc = '\n'..unescape(mal:delete_tags(string.sub(xml.find(result, 'synopsis')[1], 1, 200))) .. '...' | ||||
|   else | ||||
|     desc = '' | ||||
|   end | ||||
|  | ||||
|   if xml.find(result, 'episodes')[1] then | ||||
|     episodes = '\nEpisoden: '..xml.find(result, 'episodes')[1] | ||||
|   else | ||||
|     episodes = '' | ||||
|   end | ||||
|    | ||||
|   if xml.find(result, 'status')[1] then | ||||
|     status = ' ('..xml.find(result, 'status')[1]..')' | ||||
|   else | ||||
|     status = '' | ||||
|   end | ||||
|    | ||||
|   if xml.find(result, 'score')[1] ~= "0.00" then | ||||
|     score = '\nScore: '..string.gsub(xml.find(result, 'score')[1], "%.", ",") | ||||
|   else | ||||
|     score = '' | ||||
|   end | ||||
|    | ||||
|   if xml.find(result, 'type')[1] then | ||||
|     typ = '\nTyp: '..xml.find(result, 'type')[1] | ||||
|   else | ||||
|     typ = '' | ||||
|   end | ||||
|   | ||||
|   if xml.find(result, 'start_date')[1] ~= "0000-00-00" then | ||||
|     startdate = '\nVeröffentlichungszeitraum: '..makeOurDate(xml.find(result, 'start_date')[1]) | ||||
|   else | ||||
|     startdate = '' | ||||
|   end | ||||
|   | ||||
|   if xml.find(result, 'end_date')[1] ~= "0000-00-00" then | ||||
|     enddate = ' - '..makeOurDate(xml.find(result, 'end_date')[1]) | ||||
|   else | ||||
|     enddate = '' | ||||
|   end | ||||
|    | ||||
|   local text = '*'..title..'*'..alt_name..typ..episodes..status..score..startdate..enddate..'_'..desc..'_\n[Auf MyAnimeList ansehen]('..mal_url..')' | ||||
|   if xml.find(result, 'image') then | ||||
|     local image_url = xml.find(result, 'image')[1] | ||||
|     return text, image_url | ||||
|   else | ||||
|     return text | ||||
|   end  | ||||
| end | ||||
|  | ||||
| function mal:send_manga_data(result) | ||||
|   local title = xml.find(result, 'title')[1] | ||||
|   local id = xml.find(result, 'id')[1] | ||||
|   local mal_url = 'http://myanimelist.net/manga/'..id | ||||
|    | ||||
|   if xml.find(result, 'type')[1] then | ||||
|     typ = ' ('..xml.find(result, 'type')[1]..')' | ||||
|   else | ||||
|     typ = '' | ||||
|   end | ||||
|    | ||||
|   if xml.find(result, 'synonyms')[1] then | ||||
|     alt_name = '\noder: '..unescape(mal:delete_tags(xml.find(result, 'synonyms')[1])) | ||||
|   else | ||||
|     alt_name = '' | ||||
|   end | ||||
|  | ||||
|   if xml.find(result, 'chapters')[1] then | ||||
|     chapters = '\nKapitel: '..xml.find(result, 'chapters')[1] | ||||
|   else | ||||
|     chapters = '' | ||||
|   end | ||||
|    | ||||
|   if xml.find(result, 'status')[1] then | ||||
|     status = ' ('..xml.find(result, 'status')[1]..')' | ||||
|   else | ||||
|     status = '' | ||||
|   end | ||||
|  | ||||
|   if xml.find(result, 'volumes')[1] then | ||||
|     volumes = '\nBände '..xml.find(result, 'volumes')[1] | ||||
|   else | ||||
|     volumes = '' | ||||
|   end | ||||
|    | ||||
|   if xml.find(result, 'score')[1] ~= "0.00" then | ||||
|     score = '\nScore: '..xml.find(result, 'score')[1] | ||||
|   else | ||||
|     score = '' | ||||
|   end | ||||
|   | ||||
|   if xml.find(result, 'start_date')[1] ~= "0000-00-00" then | ||||
|     startdate = '\nVeröffentlichungszeitraum: '..makeOurDate(xml.find(result, 'start_date')[1]) | ||||
|   else | ||||
|     startdate = '' | ||||
|   end | ||||
|   | ||||
|   if xml.find(result, 'end_date')[1] ~= "0000-00-00" then | ||||
|     enddate = ' - '..makeOurDate(xml.find(result, 'end_date')[1]) | ||||
|   else | ||||
|     enddate = '' | ||||
|   end | ||||
|    | ||||
|   if xml.find(result, 'synopsis')[1] then | ||||
|     desc = '\n'..unescape(mal:delete_tags(string.sub(xml.find(result, 'synopsis')[1], 1, 200))) .. '...' | ||||
|   else | ||||
|     desc = '' | ||||
|   end | ||||
|   | ||||
|   local text = '*'..title..'*'..alt_name..typ..chapters..status..volumes..score..startdate..enddate..'_'..desc..'_\n[Auf MyAnimeList ansehen]('..mal_url..')' | ||||
|   if xml.find(result, 'image') then | ||||
|     local image_url = xml.find(result, 'image')[1] | ||||
|     return text, image_url | ||||
|   else | ||||
|     return text | ||||
|   end  | ||||
| end | ||||
|  | ||||
| function mal:action(msg, config) | ||||
|   local query = URL.escape(matches[2]) | ||||
|   if matches[1] == 'anime' then | ||||
|     local anime_info = mal:get_mal_info(query, 'anime') | ||||
|     if anime_info == "HTTP-Fehler" then | ||||
|       utilities.send_reply(self, msg, 'Anime nicht gefunden!') | ||||
| 	  return | ||||
|     else | ||||
|       local text, image_url = mal:send_anime_data(anime_info) | ||||
| 	  if image_url then | ||||
| 	    utilities.send_typing(self, msg.chat.id, 'upload_photo') | ||||
| 	    local file = download_to_file(image_url) | ||||
| 		utilities.send_photo(self, msg.chat.id, file, nil, msg.message_id) | ||||
| 	  end | ||||
| 	  utilities.send_reply(self, msg, text, true)   | ||||
| 	  return | ||||
|     end | ||||
|   elseif matches[1] == 'manga' then | ||||
|     local manga_info = mal:get_mal_info(query, 'manga') | ||||
|     if manga_info == "HTTP-Fehler" then | ||||
|       utilities.send_reply(self, msg, 'Manga nicht gefunden!') | ||||
| 	  return | ||||
|     else | ||||
|       local text, image_url = mal:send_manga_data(manga_info) | ||||
| 	  if image_url then | ||||
| 	    utilities.send_typing(self, msg.chat.id, 'upload_photo') | ||||
| 	    local file = download_to_file(image_url) | ||||
| 		utilities.send_photo(self, msg.chat.id, file, nil, msg.message_id) | ||||
| 	  end | ||||
| 	  utilities.send_reply(self, msg, text, true) | ||||
| 	  return | ||||
|     end | ||||
|   end | ||||
| end | ||||
|  | ||||
| return mal | ||||
| @@ -30,7 +30,7 @@ function weather:init(config) | ||||
| ]] | ||||
| end | ||||
|  | ||||
| weather.command = 'wetter' | ||||
| weather.command = 'w [Ort]' | ||||
|  | ||||
| local BASE_URL = "https://api.forecast.io/forecast" | ||||
| local apikey = cred_data.forecastio_apikey | ||||
|   | ||||
		Reference in New Issue
	
	Block a user
	 Andreas Bielawski
					Andreas Bielawski