mirror of
https://github.com/klmp200/kaamelott-soundboard-telegram-bot
synced 2024-11-21 15:53:20 +00:00
Maintenant il parle avec telegram
This commit is contained in:
parent
b20f1b708a
commit
930386892d
1
.gitignore
vendored
1
.gitignore
vendored
@ -38,4 +38,5 @@ Network Trash Folder
|
|||||||
Temporary Items
|
Temporary Items
|
||||||
.apdisk
|
.apdisk
|
||||||
|
|
||||||
|
settings.json
|
||||||
kaamelott-soundboard-telegram-bot
|
kaamelott-soundboard-telegram-bot
|
||||||
|
141
bot.go
141
bot.go
@ -5,62 +5,137 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"log"
|
"log"
|
||||||
|
"net/http"
|
||||||
"os"
|
"os"
|
||||||
|
"strconv"
|
||||||
|
"time"
|
||||||
|
|
||||||
"github.com/blevesearch/bleve"
|
tb "gopkg.in/tucnak/telebot.v2"
|
||||||
)
|
)
|
||||||
|
|
||||||
// Sound description d'un son
|
type settings struct {
|
||||||
type Sound struct {
|
ListeningAddress string `json:"listening_address"`
|
||||||
Character string `json:"character"`
|
Domain string `json:"domain"`
|
||||||
Episode string `json:"episode"`
|
TelegramKey string `json:"telegram_key"`
|
||||||
File string `json:"file"`
|
|
||||||
Title string `json:"title"`
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func main() {
|
const (
|
||||||
|
soundDatabase = "./sounds/sounds.json"
|
||||||
|
soundFolderPrefix = "./sounds"
|
||||||
|
defaultlisteningAddress = "0.0.0.0:8080"
|
||||||
|
defaultDomain = "http://localhost:8080"
|
||||||
|
defaultTelegramKey = "nope"
|
||||||
|
)
|
||||||
|
|
||||||
var sounds []Sound
|
var index *SoundIndex
|
||||||
|
var cfg *settings
|
||||||
|
|
||||||
jsonFile, err := os.Open("sounds.json")
|
func loadSettings(path string) (*settings, error) {
|
||||||
|
loaded := settings{
|
||||||
|
ListeningAddress: defaultlisteningAddress,
|
||||||
|
Domain: defaultDomain,
|
||||||
|
TelegramKey: defaultTelegramKey,
|
||||||
|
}
|
||||||
|
|
||||||
|
jsonFile, err := os.Open(path)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatal(err)
|
log.Printf("non trouvé mais c'est pas grave on fait sans")
|
||||||
return
|
return &loaded, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
byteData, err := ioutil.ReadAll(jsonFile)
|
byteData, err := ioutil.ReadAll(jsonFile)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatal(err)
|
return nil, fmt.Errorf("Je peux pas le lire ton fichier %s > %w", path, err)
|
||||||
return
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if json.Unmarshal(byteData, &sounds) != nil {
|
if json.Unmarshal(byteData, &loaded) != nil {
|
||||||
log.Fatal(err)
|
return nil, fmt.Errorf("Ton json %s il est tout pété > %w", path, err)
|
||||||
return
|
|
||||||
}
|
}
|
||||||
|
|
||||||
mapping := bleve.NewIndexMapping()
|
return &loaded, nil
|
||||||
index, err := bleve.NewMemOnly(mapping)
|
}
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
|
||||||
|
cfg, err := loadSettings("settings.json")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatal(err)
|
log.Fatal(err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, sound := range sounds {
|
index, err := loadAndIndexSounds(soundDatabase)
|
||||||
if index.Index(sound.File, sound) != nil {
|
if err != nil {
|
||||||
log.Printf("Error d'indexion de %v", sound)
|
log.Fatal(err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
b, err := tb.NewBot(tb.Settings{
|
||||||
|
Token: cfg.TelegramKey,
|
||||||
|
Poller: &tb.LongPoller{Timeout: 10 * time.Second},
|
||||||
|
})
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
log.Fatal(err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
fs := http.FileServer(http.Dir(soundFolderPrefix))
|
||||||
|
http.Handle("/", fs)
|
||||||
|
|
||||||
|
log.Printf("Écoute sur %s...", cfg.ListeningAddress)
|
||||||
|
go http.ListenAndServe(cfg.ListeningAddress, nil)
|
||||||
|
|
||||||
|
b.Handle("/cite", func(m *tb.Message) {
|
||||||
|
sounds, err := index.Search(m.Text)
|
||||||
|
if err != nil {
|
||||||
|
b.Send(m.Chat, "Pas de fichier audio trouvé")
|
||||||
|
return
|
||||||
}
|
}
|
||||||
}
|
path := fmt.Sprintf("%s/%s", soundFolderPrefix, sounds[0].File)
|
||||||
|
_, err = b.Send(
|
||||||
|
m.Chat,
|
||||||
|
&tb.Audio{
|
||||||
|
File: tb.FromDisk(path),
|
||||||
|
Caption: sounds[0].Title,
|
||||||
|
Title: sounds[0].Episode,
|
||||||
|
Performer: sounds[0].Character,
|
||||||
|
},
|
||||||
|
)
|
||||||
|
if err != nil {
|
||||||
|
log.Panicf("Impossible d'envoyer le fichier %s: %s", path, err)
|
||||||
|
b.Send(m.Chat, "Erreur d'envoi du fichier")
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
query := bleve.NewMatchQuery("cul")
|
b.Handle(tb.OnQuery, func(q *tb.Query) {
|
||||||
search := bleve.NewSearchRequest(query)
|
sounds, err := index.Search(q.Text)
|
||||||
searchResults, err := index.Search(search)
|
if err != nil {
|
||||||
if err != nil {
|
log.Println("Pas de fichier audio trouvé")
|
||||||
log.Fatal(err)
|
return
|
||||||
return
|
}
|
||||||
}
|
results := make(tb.Results, len(sounds))
|
||||||
for _, file := range searchResults.Hits {
|
for i, sound := range sounds {
|
||||||
fmt.Println(file.ID)
|
url := fmt.Sprintf("%s/%s", cfg.Domain, sound.File)
|
||||||
}
|
log.Println(url)
|
||||||
|
results[i] = &tb.AudioResult{
|
||||||
|
URL: url,
|
||||||
|
Caption: sound.Episode,
|
||||||
|
Title: sound.Title,
|
||||||
|
Performer: sound.Character,
|
||||||
|
}
|
||||||
|
results[i].SetResultID(strconv.Itoa(i))
|
||||||
|
}
|
||||||
|
err = b.Answer(q, &tb.QueryResponse{
|
||||||
|
Results: results,
|
||||||
|
SwitchPMParameter: "Ajoute moi",
|
||||||
|
CacheTime: 60, // une minute
|
||||||
|
})
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
log.Println(err)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
b.Start()
|
||||||
|
|
||||||
}
|
}
|
||||||
|
5
go.mod
5
go.mod
@ -2,4 +2,7 @@ module github.com/klmp200/kaamelott-soundboard-telegram-bot
|
|||||||
|
|
||||||
go 1.14
|
go 1.14
|
||||||
|
|
||||||
require github.com/blevesearch/bleve v1.0.7
|
require (
|
||||||
|
github.com/blevesearch/bleve v1.0.7
|
||||||
|
gopkg.in/tucnak/telebot.v2 v2.0.0-20200426184946-59629fe0483e
|
||||||
|
)
|
||||||
|
5
go.sum
5
go.sum
@ -55,6 +55,8 @@ github.com/onsi/gomega v1.4.3/go.mod h1:ex+gbHU/CVuBBDIJjb2X0qEXbFg53c61hWP/1Cpa
|
|||||||
github.com/pelletier/go-toml v1.2.0/go.mod h1:5z9KED0ma1S8pY6P1sdut58dfprrGBbd/94hg7ilaic=
|
github.com/pelletier/go-toml v1.2.0/go.mod h1:5z9KED0ma1S8pY6P1sdut58dfprrGBbd/94hg7ilaic=
|
||||||
github.com/philhofer/fwd v1.0.0 h1:UbZqGr5Y38ApvM/V/jEljVxwocdweyH+vmYvRPBnbqQ=
|
github.com/philhofer/fwd v1.0.0 h1:UbZqGr5Y38ApvM/V/jEljVxwocdweyH+vmYvRPBnbqQ=
|
||||||
github.com/philhofer/fwd v1.0.0/go.mod h1:gk3iGcWd9+svBvR0sR+KPcfE+RNWozjowpeBVG3ZVNU=
|
github.com/philhofer/fwd v1.0.0/go.mod h1:gk3iGcWd9+svBvR0sR+KPcfE+RNWozjowpeBVG3ZVNU=
|
||||||
|
github.com/pkg/errors v0.8.1 h1:iURUrRGxPUNPdy5/HRSm+Yj6okJ6UtLINN0Q9M4+h3I=
|
||||||
|
github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
|
||||||
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
|
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
|
||||||
github.com/rcrowley/go-metrics v0.0.0-20190826022208-cac0b30c2563/go.mod h1:bCqnVzQkZxMG4s8nGwiZ5l3QUCyqpo9Y+/ZMZ9VjZe4=
|
github.com/rcrowley/go-metrics v0.0.0-20190826022208-cac0b30c2563/go.mod h1:bCqnVzQkZxMG4s8nGwiZ5l3QUCyqpo9Y+/ZMZ9VjZe4=
|
||||||
github.com/russross/blackfriday v1.5.2/go.mod h1:JO/DiYxRf+HjHt06OyowR9PTA263kcR/rfWxYHBV53g=
|
github.com/russross/blackfriday v1.5.2/go.mod h1:JO/DiYxRf+HjHt06OyowR9PTA263kcR/rfWxYHBV53g=
|
||||||
@ -69,6 +71,7 @@ github.com/steveyen/gtreap v0.1.0/go.mod h1:kl/5J7XbrOmlIbYIXdRHDDE5QxHqpk0cmkT7
|
|||||||
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
|
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
|
||||||
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
|
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
|
||||||
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
|
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
|
||||||
|
github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5cxcmMvtA=
|
||||||
github.com/syndtr/goleveldb v1.0.0/go.mod h1:ZVVdQEZoIme9iO1Ch2Jdy24qqXrMMOU6lpPAyBWyWuQ=
|
github.com/syndtr/goleveldb v1.0.0/go.mod h1:ZVVdQEZoIme9iO1Ch2Jdy24qqXrMMOU6lpPAyBWyWuQ=
|
||||||
github.com/tinylib/msgp v1.1.0 h1:9fQd+ICuRIu/ue4vxJZu6/LzxN0HwMds2nq/0cFvxHU=
|
github.com/tinylib/msgp v1.1.0 h1:9fQd+ICuRIu/ue4vxJZu6/LzxN0HwMds2nq/0cFvxHU=
|
||||||
github.com/tinylib/msgp v1.1.0/go.mod h1:+d+yLhGm8mzTaHzB+wgMYrodPfmZrzkirds8fDWklFE=
|
github.com/tinylib/msgp v1.1.0/go.mod h1:+d+yLhGm8mzTaHzB+wgMYrodPfmZrzkirds8fDWklFE=
|
||||||
@ -91,5 +94,7 @@ golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
|
|||||||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
||||||
gopkg.in/fsnotify.v1 v1.4.7/go.mod h1:Tz8NjZHkW78fSQdbUxIjBTcgA1z1m8ZHf0WmKUhAMys=
|
gopkg.in/fsnotify.v1 v1.4.7/go.mod h1:Tz8NjZHkW78fSQdbUxIjBTcgA1z1m8ZHf0WmKUhAMys=
|
||||||
gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7/go.mod h1:dt/ZhP58zS4L8KSrWDmTeBkI65Dw0HsyUHuEVlX15mw=
|
gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7/go.mod h1:dt/ZhP58zS4L8KSrWDmTeBkI65Dw0HsyUHuEVlX15mw=
|
||||||
|
gopkg.in/tucnak/telebot.v2 v2.0.0-20200426184946-59629fe0483e h1:b9xwpngyOzAgWsFzw+kknHd/XZAnEsohHcGfu+z/LZA=
|
||||||
|
gopkg.in/tucnak/telebot.v2 v2.0.0-20200426184946-59629fe0483e/go.mod h1:+2HaHCMjzfvC3MVOSmgRKeAPruYl4PEcSxywvP8GipU=
|
||||||
gopkg.in/yaml.v2 v2.2.1/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
|
gopkg.in/yaml.v2 v2.2.1/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
|
||||||
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
|
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
|
||||||
|
88
index.go
Normal file
88
index.go
Normal file
@ -0,0 +1,88 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"encoding/json"
|
||||||
|
"fmt"
|
||||||
|
"io/ioutil"
|
||||||
|
"log"
|
||||||
|
"os"
|
||||||
|
|
||||||
|
"github.com/blevesearch/bleve"
|
||||||
|
)
|
||||||
|
|
||||||
|
// Sound description d'un son de la soundbox
|
||||||
|
type Sound struct {
|
||||||
|
Character string `json:"character"`
|
||||||
|
Episode string `json:"episode"`
|
||||||
|
File string `json:"file"`
|
||||||
|
Title string `json:"title"`
|
||||||
|
}
|
||||||
|
|
||||||
|
// SoundIndex Aide à la recherche de son
|
||||||
|
type SoundIndex struct {
|
||||||
|
index bleve.Index
|
||||||
|
Sounds []Sound
|
||||||
|
soundsByFile map[string]Sound // soundsMap[fileName]
|
||||||
|
}
|
||||||
|
|
||||||
|
// Search recherche un son dans l'index
|
||||||
|
func (si *SoundIndex) Search(str string) ([]Sound, error) {
|
||||||
|
var resp []Sound
|
||||||
|
|
||||||
|
query := bleve.NewMatchQuery(str)
|
||||||
|
req := bleve.NewSearchRequest(query)
|
||||||
|
searchResults, err := si.index.Search(req)
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("erreur dans la requête > %w", err)
|
||||||
|
}
|
||||||
|
for _, hit := range searchResults.Hits {
|
||||||
|
if sound, ok := si.soundsByFile[hit.ID]; ok {
|
||||||
|
resp = append(resp, sound)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if len(resp) == 0 {
|
||||||
|
return nil, fmt.Errorf("pas de fichier trouvé")
|
||||||
|
}
|
||||||
|
|
||||||
|
return resp, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func loadAndIndexSounds(path string) (*SoundIndex, error) {
|
||||||
|
|
||||||
|
var sounds []Sound
|
||||||
|
soundMap := make(map[string]Sound)
|
||||||
|
|
||||||
|
jsonFile, err := os.Open(path)
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("ouverture de la base de sons > %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
byteData, err := ioutil.ReadAll(jsonFile)
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("lecture de la base de sons > %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if json.Unmarshal(byteData, &sounds) != nil {
|
||||||
|
return nil, fmt.Errorf("lecture de la base des sons > %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
mapping := bleve.NewIndexMapping()
|
||||||
|
index, err := bleve.NewMemOnly(mapping)
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("création de l'indexeur > %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, sound := range sounds {
|
||||||
|
if index.Index(sound.File, sound) != nil {
|
||||||
|
log.Printf("Error d'indexion de %v", sound)
|
||||||
|
}
|
||||||
|
soundMap[sound.File] = sound
|
||||||
|
}
|
||||||
|
|
||||||
|
return &SoundIndex{
|
||||||
|
index,
|
||||||
|
sounds,
|
||||||
|
soundMap,
|
||||||
|
}, nil
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user