Heroku

Preview reading
It is recommended to visit our pages about UpdatesFilters and Webhooks to have more clear understanding about what is happening in this examples page

  • Heroku apps by default accessible via https://<app name>.herokuapp.com/
  • Heroku provide one port to be proxied for the link above. You can retrieve number of this port by calling System.getenv("PORT").toInt()
  • Currently (Sat Aug 15 5:04:21 +00 2020) there is only one official server engine for ktor which is correctly working with Heroku: Tomcat server engine

Configuration example

Server configuration alternatives
Here will be presented full variant of configuration of webhooks and starting server. You always able to set webhook manualy, create your own ktor server and include webhooks handling in it or create and start server with only webhooks handling. More info you can get on page Webhooks

val bot = telegramBot(TOKEN)
val scope = CoroutineScope(Dispatchers.Default)

val filter = flowsUpdatesFilter {
  messageFlow.onEach {
    println(it) // will be printed 
  }.launchIn(scope)
}

val subroute = UUID.randomUUID().toString() // It will be used as subpath for security target as recommended by https://core.telegram.org/bots/api#setwebhook

val server = bot.setWebhookInfoAndStartListenWebhooks(
  System.getenv("PORT"),
  Tomcat, // will be available after implementing of "io.ktor:ktor-server-tomcat:$ktor_version" dependency
  SetWebhook("https://<app name>.herokuapp.com/$subroute", allowedUpdates = filter.allowedUpdates),
  {
    it.printStackTrace()
  },
  "0.0.0.0",
  subroute,
  scope = scope,
  block = filter.asUpdateReceiver
)

server.environment.connectors.forEach {
  println(it)
}
server.start(false)