DEV Community

Jiayu Yi
Jiayu Yi

Posted on • Originally published at blog.jiayu.co

Terraform Provider for Telegram

Originally published at https://blog.jiayu.co/2020/01/terraform-provider-for-telegram/ on 2020-01-14.

Over the weekend, I wrote a custom Terraform provider for Telegram:

GitHub logo yi-jiayu / terraform-provider-telegram

Terraform provider for Telegram

Terraform Provider for Telegram

Manage Telegram resources with Terraform

Example Usage

If we have a bot implemented as a Google Cloud Function, we can set the bot webhook in the same Terraform project:

resource "google_cloudfunctions_function" "my_bot" {
  name         = "my-bot"
  trigger_http = true
  # other arguments
}
resource "telegram_bot_webhook" "my_bot" {
  url = google_cloudfunctions_function.my_bot.https_trigger_url
}

You can also manage bot commands using Terraform:

resource "telegram_bot_commands" "example" {
  commands = [
    {
      command = "start"
      description = "View welcome message"
    },
    {
      command = "help",
      description = "Show help"
    }
  ]
}

Installation

Download the latest release for your OS and extract it to the user plugins directory, which is located at %APPDATA%\terraform.d\plugins on Windows and ~/.terraform.d/plugins on Linux and macOS.

See also…

Terraform is an infrastructure as code tool which allows you to declaratively specify your infrastructure and reproducibly set it up.

My main goal was to have first-class Terraform support for setting a bot webhook. Right now, you can automate all the infrastructure for a Telegram bot except for setting the webhook, which has to be done manually or with a workaround like using the local-exec provisioner.

With the Terraform provider for Telegram, a bot webhook can be declared as a Terraform resource:

resource "telegram_bot_webhook" "example" {
  url = "https://www.example.com/webhook"
}

Creating, updating and deleting a bot webhook works as expected.

Besides setting a bot webhook, there is also a Telegram bot data source for getting details about the currently-authenticated Telegram bot:

data "telegram_bot" "example" {}

output "bot_link" {
  value = "https://t.me/${data.telegram_bot.example.username}"
}

Getting started

Since the Terraform provider for Telegram is a third-party plugin, you need to manually install it by dropping its executable into your user plugins directory. You can find precompiled binaries for your OS on the releases page, or build it from source with a Go toolchain (Go 1.13 or newer).

The user plugins directory is located at %APPDATA%\terraform.d\plugins on Windows, and ~/.terraform.d/plugins on all other systems. See the Terraform documentation on third-party plugins for more information.

Once you've installed the plugin and added some Telegram resources, you'll need to run terraform init again for Terraform to pick up the new provider before you can run terraform plan or terraform apply.

Support

If you're already using Terraform in your Telegram bot projects, give the Terraform provider for Telegram a try! If you aren't already practising infrastructure as code, it's a good opportunity to get started! Contributions on GitHub are welcome.

I've also applied to have the Terraform provider for Telegram listed on the Terraform community providers page.

Top comments (0)