166 lines
5.2 KiB
Go
166 lines
5.2 KiB
Go
/*
|
|
Copyright © 2023 Jonas Möller <mail@jonasmoeller.de>
|
|
|
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
of this software and associated documentation files (the "Software"), to deal
|
|
in the Software without restriction, including without limitation the rights
|
|
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
copies of the Software, and to permit persons to whom the Software is
|
|
furnished to do so, subject to the following conditions:
|
|
|
|
The above copyright notice and this permission notice shall be included in
|
|
all copies or substantial portions of the Software.
|
|
|
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
THE SOFTWARE.
|
|
*/
|
|
package cmd
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api/v5"
|
|
"github.com/spf13/cobra"
|
|
"github.com/spf13/viper"
|
|
"github.com/zmb3/spotify/v2"
|
|
spotifyauth "github.com/zmb3/spotify/v2/auth"
|
|
"golang.org/x/oauth2"
|
|
"log"
|
|
"strings"
|
|
)
|
|
|
|
// telegramBotCmd represents the telegramBot command
|
|
var telegramBotCmd = &cobra.Command{
|
|
Use: "telegramBot",
|
|
Short: "Start the Telegram bot",
|
|
Long: `Start the Telegram bot which listens for new messages in the configured group chat.
|
|
|
|
Currently spotigram only uses a single group ID to verify the origin of the message.
|
|
It is not planned to extend this in the future.`,
|
|
Run: func(cmd *cobra.Command, args []string) {
|
|
var (
|
|
cachedToken = new(oauth2.Token)
|
|
auth = spotifyauth.New()
|
|
ctx = context.Background()
|
|
)
|
|
viper.SetDefault("telegram_debug", false)
|
|
viper.SetDefault("telegram_update_timeout", 60)
|
|
|
|
// Telegram Bot
|
|
bot, err := tgbotapi.NewBotAPI(viper.GetString("telegram_api_token"))
|
|
if err != nil {
|
|
cobra.CheckErr(err)
|
|
}
|
|
|
|
bot.Debug = viper.GetBool("telegram_debug")
|
|
|
|
log.Printf("Authorized account: %s", bot.Self.UserName)
|
|
|
|
u := tgbotapi.NewUpdate(0)
|
|
u.Timeout = viper.GetInt("telegram_update_timeout")
|
|
|
|
updates := bot.GetUpdatesChan(u)
|
|
|
|
for update := range updates {
|
|
if update.Message.Chat.IsGroup() && update.Message.Chat.ID == viper.GetInt64("telegram_group_id") {
|
|
|
|
if update.Message == nil {
|
|
continue
|
|
}
|
|
|
|
if !update.Message.IsCommand() {
|
|
continue
|
|
}
|
|
|
|
msg := tgbotapi.NewMessage(update.Message.Chat.ID, "")
|
|
msg.ReplyToMessageID = update.Message.MessageID
|
|
|
|
switch update.Message.Command() {
|
|
case "spotigram":
|
|
// Spotify client initialization
|
|
cachedToken.AccessToken = viper.GetString("spotify_access_token")
|
|
cachedToken.RefreshToken = viper.GetString("spotify_refresh_token")
|
|
cachedToken.TokenType = viper.GetString("spotify_token_type")
|
|
cachedToken.Expiry = viper.GetTime("spotify_token_expiry")
|
|
client := spotify.New(auth.Client(ctx, cachedToken))
|
|
newToken, err := client.Token()
|
|
if err != nil {
|
|
cobra.CheckErr(err)
|
|
}
|
|
if newToken != cachedToken {
|
|
viper.Set("spotify_access_token", newToken.AccessToken)
|
|
viper.Set("spotify_refresh_token", newToken.RefreshToken)
|
|
viper.Set("spotify_token_type", newToken.TokenType)
|
|
viper.Set("spotify_token_expiry", newToken.Expiry)
|
|
if err = viper.WriteConfig(); err != nil {
|
|
cobra.CheckErr(err)
|
|
}
|
|
}
|
|
|
|
results, err := client.Search(ctx, strings.TrimPrefix(update.Message.Text, "/spotigram "), spotify.SearchTypeTrack)
|
|
if err != nil {
|
|
cobra.CheckErr(err)
|
|
}
|
|
if results != nil {
|
|
track := results.Tracks.Tracks[0]
|
|
playlist, err := client.GetPlaylist(ctx, spotify.ID(viper.GetString("spotify_playlist_id")))
|
|
if err != nil {
|
|
cobra.CheckErr(err)
|
|
}
|
|
|
|
addTrackResponse, err := client.AddTracksToPlaylist(ctx,
|
|
playlist.ID,
|
|
results.Tracks.Tracks[0].ID)
|
|
if err != nil {
|
|
cobra.CheckErr(err)
|
|
}
|
|
|
|
msg.Text = fmt.Sprintf("Ich habe den Titel <pre>%s</pre> von <pre>%s</pre> zur Playlist <pre>%s</pre> hinzugefügt.",
|
|
track.Name,
|
|
track.Artists[0].Name,
|
|
playlist.Name,
|
|
)
|
|
msg.ParseMode = "HTML"
|
|
if _, err := bot.Send(msg); err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
|
|
fmt.Printf("New track added to playlist, snapshot: %s\n", addTrackResponse)
|
|
|
|
}
|
|
case "rofl":
|
|
msg.Text = "🤣"
|
|
if _, err := bot.Send(msg); err != nil {
|
|
cobra.CheckErr(err)
|
|
}
|
|
default:
|
|
msg.Text = "🖕"
|
|
if _, err := bot.Send(msg); err != nil {
|
|
cobra.CheckErr(err)
|
|
}
|
|
}
|
|
|
|
}
|
|
}
|
|
},
|
|
}
|
|
|
|
func init() {
|
|
rootCmd.AddCommand(telegramBotCmd)
|
|
|
|
// Here you will define your flags and configuration settings.
|
|
|
|
// Cobra supports Persistent Flags which will work for this command
|
|
// and all subcommands, e.g.:
|
|
// telegramBotCmd.PersistentFlags().String("foo", "", "A help for foo")
|
|
|
|
// Cobra supports local flags which will only run when this command
|
|
// is called directly, e.g.:
|
|
// telegramBotCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
|
|
}
|