debuging locallySlack in Java` in Kotlin`Such.
macOS Mojave 10.14.5IntelliJ IDEA ultimateDocker installed (required for local development)https://github.com/yu-suke-dev/fanza-bot


[sample]
   ├── HelloWorldFunction
   │   ├── build.gradle
   │   ├── gradle
   │   │   └── wrapper
   │   │       ├── gradle-wrapper.jar
   │   │       └── gradle-wrapper.properties
   │   ├── gradlew
   │   ├── gradlew.bat
   │   └── src
   │       ├── main
   │       │   └── java
   │       │       └── helloworld
   │       │           ├── App.java
   │       │           └── GatewayResponse.java
   │       └── test
   │           └── java
   │               └── helloworld
   │                   └── AppTest.java
   ├── README.md
   ├── sample.iml
   └── template.yaml
build.gradle
plugins {
    id 'org.jetbrains.kotlin.jvm' version '1.3.40'
}
apply plugin: 'kotlin'
repositories {
    mavenCentral()
}
sourceCompatibility = '1.8'
targetCompatibility = '1.8'
jar {
    manifest {
        attributes('Main-Class': 'fanza.App')
    }
    from { configurations.compile.collect { it.isDirectory() ? it : zipTree(it) } }
}
dependencies {
    compile "com.amazonaws:aws-lambda-java-core:1.2.0"
    compile "org.jetbrains.kotlin:kotlin-stdlib-jdk8:1.3.40"
    compile "com.squareup.okhttp3:okhttp:3.9.0"
    compile "org.json:json:20180813"
    testCompile "junit:junit:4.12"
}
[src]
  ├── main
  │   └── kotlin
  │       └── fanza
  │           └── App.java
  └── test
  │   └── java
  │       └── fanza
  │           └── AppTest.java
App.kt
package fanza
import com.amazonaws.services.lambda.runtime.Context
import com.amazonaws.services.lambda.runtime.RequestHandler
import okhttp3.MediaType
import okhttp3.OkHttpClient
import okhttp3.Request
import okhttp3.RequestBody
import org.json.JSONObject
import java.io.IOException
/**
 * Handler for requests to Lambda function.
 */
class App : RequestHandler<Any, Any> {
    /**
     * called method
     */
    override fun handleRequest(input: Any, context: Context): Any {
        try {
            //ID required for fanza API
            var fanzaApiId = System.getenv("FANZA_API_ID")
            var affiliateId = System.getenv("FANZA_AFFILIATE_ID")
            //Create the URL you want to search
            val fanzaUrl: String = "https://api.dmm.com/affiliate/v3/ItemList"
                    .plus("?api_id=$fanzaApiId")
                    .plus("&affiliate_id=$affiliateId")
                    .plus("&site=FANZA&service=digital&floor=videoa")
                    .plus("&hits=20&sort=date&keyword=amateur&output=json")
            //GET to FANZA
            val fanzaInfo = get(fanzaUrl).replace("\\", "")
            //Regular expression of the part you want to extract
            val regex = Regex("https://www.dmm.co.jp/litevideo/-/part/=/cid=[a-z]{3,5}[0-9]{3,5}/size=720_480/")
            //Randomly extract one from the extracted results
            val result: String = regex.findAll(fanzaInfo)
                    .map { it.value }
                    .toList()
                    .random()
            //Slack webhook URL
            var slackUrl: String = System.getenv("SLACK_URL")
            //What to send to Slack
            var slackJson = JSONObject()
            slackJson.put("channel", "#fanza")
            slackJson.put("username", "fanza-bot")
            slackJson.put("text", result)
            slackJson.put("icon_emoji", ":womens:")
            //post send
            post(slackUrl, slackJson.toString())
            return "Success"
        } catch (e: IOException) {
            return RuntimeException(e)
        }
    }
    /**
     * get
     */
    @Throws(IOException::class)
    operator fun get(url: String): String {
        val request = Request.Builder()
                .url(url)
                .header("Content-Type", "application/json")
                .addHeader("X-Custom-Header", "application/json")
                .build()
        val client = OkHttpClient.Builder().build()
        val response = client.newCall(request).execute()
        return response.body()!!.string()
    }
    /**
     * post
     */
    @Throws(IOException::class)
    fun post(url: String, json: String): String {
        val body = RequestBody
                .create(MediaType.parse("application/json; charset=utf-8"), json)
        val request = Request.Builder()
                .url(url)
                .put(body)
                .build()
        val client = OkHttpClient()
        return client.newCall(request).execute().body()!!.string()
    }
}
 
 

gradle jar

Register the product: Omitted. Register the product by referring to the following articles. https://qiita.com/shogo_m/items/9aa7b400a031e8503e7f
Create Project: Select ʻIoT 1-Click` from the AWS console screen

Specifying the project name

Specify the created Lambda function here.

Press the * button. (Pochi)

Recommended Posts