go-telegram / bot

Telegram Bot API Go framework
MIT License
507 stars 46 forks source link

is it possible to use for serverless case? #23

Closed plandem closed 1 year ago

plandem commented 1 year ago

e.g. lambda functions or anything else

negasus commented 1 year ago

Telegram Bot can work with https://core.telegram.org/bots/api#setwebhook If your labmda can wake up by webhook, you can use this approach, i think

negasus commented 1 year ago

I make function bot.ProcessUpdate public. You can use this func for manually process updates.

Example lambda for Yandex.Cloud Functions

package main

import (
    "context"
    "encoding/json"
    "net/http"
    "os"

    "github.com/go-telegram/bot"
    "github.com/go-telegram/bot/models"
)

func Handler(rw http.ResponseWriter, req *http.Request) {
    b, err := bot.New(os.Getenv("EXAMPLE_TELEGRAM_BOT_TOKEN"), bot.WithDefaultHandler(defaultHandler))
    if err != nil {
        rw.WriteHeader(http.StatusBadRequest)
        return
    }

    update := models.Update{}

    errDecode := json.NewDecoder(req.Body).Decode(&update)
    if errDecode != nil {
        rw.WriteHeader(http.StatusBadRequest)
        return
    }

    b.ProcessUpdate(context.Background(), &update)
}

func defaultHandler(ctx context.Context, b *bot.Bot, update *models.Update) {
    b.SendMessage(ctx, &bot.SendMessageParams{
        ChatID:          update.Message.Chat.ID,
        Text:            "You wrote: " + update.Message.Text,
    })
}
plandem commented 1 year ago

cool! will try soon. Worried a bit about all these signal and other OS stuff in serverless environment