2022-04-26 00:02:51 +08:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"context"
|
|
|
|
"fmt"
|
2022-04-29 17:21:42 +08:00
|
|
|
"os"
|
|
|
|
"os/signal"
|
|
|
|
|
2022-04-26 00:02:51 +08:00
|
|
|
"github.com/go-telegram/bot"
|
|
|
|
"github.com/go-telegram/bot/models"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Send any text message to the bot after the bot has been started
|
|
|
|
|
|
|
|
func main() {
|
2022-05-03 00:39:04 +08:00
|
|
|
ctx, cancel := signal.NotifyContext(context.Background(), os.Interrupt)
|
|
|
|
defer cancel()
|
|
|
|
|
2022-04-26 00:02:51 +08:00
|
|
|
opts := []bot.Option{
|
|
|
|
bot.WithDefaultHandler(handler),
|
|
|
|
}
|
|
|
|
|
2023-04-26 17:00:17 +08:00
|
|
|
b, err := bot.New(os.Getenv("EXAMPLE_TELEGRAM_BOT_TOKEN"), opts...)
|
|
|
|
if nil != err {
|
|
|
|
// panics for the sake of simplicity.
|
|
|
|
// you should handle this error properly in your code.
|
|
|
|
panic(err)
|
|
|
|
}
|
2022-04-26 00:02:51 +08:00
|
|
|
|
2022-05-06 17:47:43 +08:00
|
|
|
b.Start(ctx)
|
2022-04-26 00:02:51 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func handler(ctx context.Context, b *bot.Bot, update *models.Update) {
|
|
|
|
fileData, errReadFile := os.ReadFile("./examples/send_photo_upload/facebook.png")
|
|
|
|
if errReadFile != nil {
|
|
|
|
fmt.Printf("error read file, %v\n", errReadFile)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-05-06 17:47:43 +08:00
|
|
|
params := &bot.SendPhotoParams{
|
2022-04-29 17:21:42 +08:00
|
|
|
ChatID: update.Message.Chat.ID,
|
2022-04-26 00:02:51 +08:00
|
|
|
Photo: &models.InputFileUpload{Filename: "facebook.png", Data: bytes.NewReader(fileData)},
|
|
|
|
Caption: "New uploaded Facebook logo",
|
|
|
|
}
|
|
|
|
|
2022-05-06 17:47:43 +08:00
|
|
|
b.SendPhoto(ctx, params)
|
2022-04-26 00:02:51 +08:00
|
|
|
}
|