Skip to content

Instantly share code, notes, and snippets.

@lonelyelk
Last active February 17, 2023 01:30
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lonelyelk/604bef0437c2eb7dda2d395e9c05ce84 to your computer and use it in GitHub Desktop.
Save lonelyelk/604bef0437c2eb7dda2d395e9c05ce84 to your computer and use it in GitHub Desktop.
Telegram Webhook Provider for Terraform
#!/bin/zsh
go build -o terraform-provider-telegram && cp terraform-provider-telegram ~/.terraform.d/plugins/lonelyelk.ru/aws/telegram/1.0.0/darwin_amd64
package main
import (
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/v2/plugin"
)
func main() {
plugin.Serve(&plugin.ServeOpts{
ProviderFunc: func() *schema.Provider {
return webhookProvider()
},
})
}
package main
import (
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)
func webhookProvider() *schema.Provider {
return &schema.Provider{
ResourcesMap: map[string]*schema.Resource{
"telegram_webhook": resourceWebhook(),
},
}
}
package main
import (
"context"
"crypto/sha1"
"fmt"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
tele "gopkg.in/telebot.v3"
)
func resourceWebhook() *schema.Resource {
return &schema.Resource{
CreateContext: resourceWebhookCreate,
ReadContext: resourceWebhookRead,
UpdateContext: resourceWebhookUpdate,
DeleteContext: resourceWebhookDelete,
Schema: map[string]*schema.Schema{
"token": {
Type: schema.TypeString,
Required: true,
},
"uri": {
Type: schema.TypeString,
Required: true,
},
},
}
}
func getId(token string, uri string) string {
hash := sha1.New()
hash.Write([]byte(token))
hash.Write([]byte(uri))
return fmt.Sprintf("%x", hash.Sum(nil))
}
func resourceWebhookCreate(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
return resourceWebhookUpdate(ctx, d, m)
}
func resourceWebhookRead(ctx context.Context, d *schema.ResourceData, m interface{}) (diags diag.Diagnostics) {
token := d.Get("token").(string)
settings := tele.Settings{
Token: token,
Synchronous: true,
}
bot, err := tele.NewBot(settings)
if err != nil {
return diag.FromErr(err)
}
wh, err := bot.Webhook()
if err != nil {
return diag.FromErr(err)
}
if err = d.Set("uri", wh.Listen); err != nil {
return diag.FromErr(err)
}
d.SetId(getId(token, wh.Listen))
return
}
func resourceWebhookUpdate(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
token := d.Get("token").(string)
uri := d.Get("uri").(string)
settings := tele.Settings{
Token: token,
Synchronous: true,
}
bot, err := tele.NewBot(settings)
if err != nil {
return diag.FromErr(err)
}
err = bot.SetWebhook(&tele.Webhook{
Endpoint: &tele.WebhookEndpoint{
PublicURL: uri,
},
})
if err != nil {
return diag.FromErr(err)
}
return resourceWebhookRead(ctx, d, m)
}
func resourceWebhookDelete(ctx context.Context, d *schema.ResourceData, m interface{}) (diags diag.Diagnostics) {
d.SetId("")
return
}
terraform {
required_providers {
telegram = {
version = "~> 1.0.0"
source = "lonelyelk.ru/aws/telegram"
}
}
}
# ....
resource "telegram_webhook" "gw_hook" {
token = var.token
uri = "${aws_apigatewayv2_stage.lambda_bot.invoke_url}/${...}"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment