feat: Initial commit

This commit is contained in:
ptrcnull 2022-04-03 16:51:06 +02:00
commit fc8de33537
Signed by: ptrcnull
GPG key ID: 411F7B30801DD9CA
3 changed files with 101 additions and 0 deletions

13
go.mod Normal file
View file

@ -0,0 +1,13 @@
module mqttg
go 1.18
require (
git.ddd.rip/ptrcnull/telegram v1.1.0
github.com/eclipse/paho.mqtt.golang v1.3.5
)
require (
github.com/gorilla/websocket v1.4.2 // indirect
golang.org/x/net v0.0.0-20200425230154-ff2c4b7c35a0 // indirect
)

12
go.sum Normal file
View file

@ -0,0 +1,12 @@
git.ddd.rip/ptrcnull/telegram v1.1.0 h1:nLyQE9uR6CBK4kra9uN+v7rmf2uZ2HOpGZh4J+V0XqI=
git.ddd.rip/ptrcnull/telegram v1.1.0/go.mod h1:SSSKvfhw7mDx/8UPoLdtP9J74z2/pXccHnKzdi16nLA=
github.com/eclipse/paho.mqtt.golang v1.3.5 h1:sWtmgNxYM9P2sP+xEItMozsR3w0cqZFlqnNN1bdl41Y=
github.com/eclipse/paho.mqtt.golang v1.3.5/go.mod h1:eTzb4gxwwyWpqBUHGQZ4ABAV7+Jgm1PklsYT/eo8Hcc=
github.com/gorilla/websocket v1.4.2 h1:+/TMaTYc4QFitKJxsQ7Yye35DkWvkdLcvGKqM+x0Ufc=
github.com/gorilla/websocket v1.4.2/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/net v0.0.0-20200425230154-ff2c4b7c35a0 h1:Jcxah/M+oLZ/R4/z5RzfPzGbPXnVDPkEDtf2JnuxN+U=
golang.org/x/net v0.0.0-20200425230154-ff2c4b7c35a0/go.mod h1:qpuaurCH72eLCgpAm/N6yyVIVM9cpaDIP3A8BGJEC5A=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20200323222414-85ca7c5b95cd/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=

76
main.go Normal file
View file

@ -0,0 +1,76 @@
package main
import (
"flag"
"git.ddd.rip/ptrcnull/telegram"
mqtt "github.com/eclipse/paho.mqtt.golang"
"log"
"net/url"
"os"
)
func main() {
token := flag.String("token", "", "Telegram bot token")
chat := flag.String("chat", "", "Telegram chat ID")
broker := flag.String("broker", "", "MQTT broker address")
topic := flag.String("topic", "", "MQTT topic")
flag.Parse()
if *token == "" || *chat == "" || *broker == "" || *topic == "" {
flag.Usage()
os.Exit(1)
}
tg := telegram.Client{
Token: *token,
PollTimeout: 120,
}
brokerUrl, err := url.Parse(*broker)
if err != nil {
log.Fatalf("broker url: %s", err)
}
opts := mqtt.NewClientOptions()
opts.AddBroker(*broker)
if brokerUrl.User != nil {
opts.SetUsername(brokerUrl.User.Username())
if password, ok := brokerUrl.User.Password(); ok {
opts.SetUsername(password)
}
}
opts.SetDefaultPublishHandler(func(client mqtt.Client, message mqtt.Message) {
_, err := tg.SendMessage(*chat, string(message.Payload()))
if err != nil {
log.Fatalf("send message: %s", err)
}
})
opts.OnConnect = func(client mqtt.Client) {
log.Println("connected!")
}
opts.OnConnectionLost = func(client mqtt.Client, err error) {
log.Printf("connection lost: %s\n", err)
}
opts.SetClientID("mqttg")
client := mqtt.NewClient(opts)
if token := client.Connect(); token.Wait() && token.Error() != nil {
log.Fatalf("mqtt connect: %s", token.Error())
}
client.Subscribe(*topic + "/inbox", 1, nil)
for {
res, err := tg.GetUpdates()
if err != nil {
log.Fatalf("telegram updates: %s", err)
}
if !res.Ok {
log.Fatalf("telegram updates: %s", res.Raw)
}
for _, update := range res.Result {
if update.Message != nil {
client.Publish(*topic + "/outbox", 0, false, update.Message.Text)
}
}
}
}