40 lines
		
	
	
		
			1.0 KiB
		
	
	
	
		
			Lua
		
	
	
	
	
	
			
		
		
	
	
			40 lines
		
	
	
		
			1.0 KiB
		
	
	
	
		
			Lua
		
	
	
	
	
	
| local e621 = {}
 | |
| 
 | |
| function e621:init(config)
 | |
|   e621.triggers = utilities.triggers(self.info.username, config.cmd_pat):t('e621', true).table
 | |
|   e621.doc = [[*
 | |
| ]]..config.cmd_pat..[[e621* _<Tags>_: Sucht auf e621 mit diesen Tags]]
 | |
| end
 | |
| 
 | |
| e621.command = 'e621 <Tags>'
 | |
| 
 | |
| function e621:get_e621(tag)
 | |
|   local BASE_URL = 'https://e621.net'
 | |
|   local url = BASE_URL..'/post/index.json?tags='..tag
 | |
|   local res, code = https.request(url)
 | |
|   if code ~= 200 then return nil end
 | |
|   local data = json.decode(res)
 | |
|   if not data[1] then return nil end
 | |
|   
 | |
|   local i = math.random(#data)
 | |
|   local link_image = data[i].file_url
 | |
|   return link_image
 | |
| end
 | |
| 
 | |
| function e621:action(msg, config)
 | |
|   local input = utilities.input_from_msg(msg)
 | |
|   if not input then
 | |
|     utilities.send_reply(msg, e621.doc, true)
 | |
| 	return
 | |
|   end
 | |
|   local url = e621:get_e621(input)
 | |
|   if not url then
 | |
|     utilities.send_reply(msg, config.errors.results)
 | |
| 	return
 | |
|   end
 | |
|   utilities.send_typing(msg.chat.id, 'upload_photo')
 | |
|   local file = download_to_file(url)
 | |
|   utilities.send_photo(msg.chat.id, file, url, msg.message_id)
 | |
| end
 | |
| 
 | |
| return e621 | 
