Hello world XMPP echo component
This commit is contained in:
commit
9b4a09677a
2
.gitignore
vendored
Normal file
2
.gitignore
vendored
Normal file
|
@ -0,0 +1,2 @@
|
|||
config.yml
|
||||
telegabber
|
23
config.yml.example
Normal file
23
config.yml.example
Normal file
|
@ -0,0 +1,23 @@
|
|||
:telegram:
|
||||
:loglevel: :warn
|
||||
:content:
|
||||
:path: '/var/www/telegabber/content' # webserver workdir
|
||||
:link: 'http://tlgrm.localhost/content' # webserver public address
|
||||
:upload: 'https:///xmppfiles.localhost' # xmpp http upload address
|
||||
:tdlib_verbosity: 1
|
||||
:tdlib:
|
||||
:lib_path: 'lib/'
|
||||
:client:
|
||||
:api_id: '17349'
|
||||
:api_hash: '344583e45741c457fe1862106095a5eb'
|
||||
:device_model: 'telegabber'
|
||||
:application_version: '2.0'
|
||||
:use_chat_info_database: false
|
||||
|
||||
:xmpp:
|
||||
:loglevel: :warn
|
||||
:jid: 'tlgrm.localhost'
|
||||
:host: '127.0.0.1'
|
||||
:port: 8899
|
||||
:password: 'password'
|
||||
:db: 'sessions.dat'
|
64
config/config.go
Normal file
64
config/config.go
Normal file
|
@ -0,0 +1,64 @@
|
|||
package config
|
||||
|
||||
import (
|
||||
"io/ioutil"
|
||||
"log"
|
||||
|
||||
"gopkg.in/yaml.v2"
|
||||
)
|
||||
|
||||
type Config struct {
|
||||
Telegram TelegramConfig `yaml:":telegram"`
|
||||
Xmpp XmppConfig `yaml:":xmpp"`
|
||||
}
|
||||
|
||||
type XmppConfig struct {
|
||||
Loglevel string `yaml:":loglevel"`
|
||||
Jid string `yaml:":jid"`
|
||||
Host string `yaml:":host"`
|
||||
Port string `yaml:":port"`
|
||||
Password string `yaml:":password"`
|
||||
Db string `yaml:":db"`
|
||||
}
|
||||
|
||||
type TelegramConfig struct {
|
||||
Loglevel string `yaml:":loglevel"`
|
||||
Content TelegramContentConfig `yaml:":content"`
|
||||
Verbosity uint8 `yaml:":tdlib_verbosity"`
|
||||
Tdlib TelegramTdlibConfig `yaml:":tdlib"`
|
||||
}
|
||||
|
||||
type TelegramContentConfig struct {
|
||||
Path string `yaml:":path"`
|
||||
Link string `yaml:":link"`
|
||||
Upload string `yaml:":upload"`
|
||||
}
|
||||
|
||||
type TelegramTdlibConfig struct {
|
||||
Path string `yaml:":lib_path"`
|
||||
Client TelegramTdlibClientConfig `yaml:":client"`
|
||||
}
|
||||
|
||||
type TelegramTdlibClientConfig struct {
|
||||
ApiId string `yaml:":api_id"`
|
||||
ApiHash string `yaml:":api_hash"`
|
||||
DeviceModel string `yaml:":device_model"`
|
||||
ApplicationVersion string `yaml:":application_version"`
|
||||
UseChatInfoDatabase bool `yaml:":use_chat_info_database"`
|
||||
}
|
||||
|
||||
func ReadConfig(path string) Config {
|
||||
var config Config
|
||||
|
||||
file, err := ioutil.ReadFile(path)
|
||||
if err != nil {
|
||||
log.Fatalf("Can't open config file: %v", err)
|
||||
}
|
||||
|
||||
err = yaml.Unmarshal(file, &config)
|
||||
if err != nil {
|
||||
log.Fatalf("Error parsing config: %v", err)
|
||||
}
|
||||
|
||||
return config
|
||||
}
|
8
go.mod
Normal file
8
go.mod
Normal file
|
@ -0,0 +1,8 @@
|
|||
module dev.narayana.im/narayana/telegabber
|
||||
|
||||
go 1.13
|
||||
|
||||
require (
|
||||
gopkg.in/yaml.v2 v2.2.4
|
||||
gosrc.io/xmpp v0.1.3
|
||||
)
|
8
go.sum
Normal file
8
go.sum
Normal file
|
@ -0,0 +1,8 @@
|
|||
github.com/google/go-cmp v0.3.0/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU=
|
||||
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7 h1:9zdDQZ7Thm29KFXgAX/+yaf3eVbP7djjWp/dXAppNCc=
|
||||
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
|
||||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
||||
gopkg.in/yaml.v2 v2.2.4 h1:/eiJrUcujPVeJ3xlSWaiNi3uSVmDGBK1pDHUHAnao1I=
|
||||
gopkg.in/yaml.v2 v2.2.4/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
|
||||
gosrc.io/xmpp v0.1.3 h1:VYP1bA35irlQ1ZAJqNhJOz8NSsSTkzQRhREfmuG1H80=
|
||||
gosrc.io/xmpp v0.1.3/go.mod h1:fWixaMaFvx8cxXcJVJ5kU9csMeD/JN8on7ybassU8rY=
|
18
telegabber.go
Normal file
18
telegabber.go
Normal file
|
@ -0,0 +1,18 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"log"
|
||||
|
||||
"dev.narayana.im/narayana/telegabber/config"
|
||||
"dev.narayana.im/narayana/telegabber/xmpp"
|
||||
)
|
||||
|
||||
const CONFIG_PATH string = "config.yml"
|
||||
|
||||
func main() {
|
||||
config := config.ReadConfig(CONFIG_PATH)
|
||||
cm := xmpp.NewComponent(config.Xmpp)
|
||||
|
||||
// reconnect automatically
|
||||
log.Fatal(cm.Run())
|
||||
}
|
30
xmpp/component.go
Normal file
30
xmpp/component.go
Normal file
|
@ -0,0 +1,30 @@
|
|||
package xmpp
|
||||
|
||||
import (
|
||||
"log"
|
||||
|
||||
"dev.narayana.im/narayana/telegabber/config"
|
||||
|
||||
"gosrc.io/xmpp"
|
||||
)
|
||||
|
||||
func NewComponent(conf config.XmppConfig) *xmpp.StreamManager {
|
||||
options := xmpp.ComponentOptions{
|
||||
Address: conf.Host + ":" + conf.Port,
|
||||
Domain: conf.Jid,
|
||||
Secret: conf.Password,
|
||||
Name: "telegabber",
|
||||
}
|
||||
|
||||
router := xmpp.NewRouter()
|
||||
router.HandleFunc("message", HandleMessage)
|
||||
|
||||
component, err := xmpp.NewComponent(options, router)
|
||||
if err != nil {
|
||||
log.Fatalf("%+v", err)
|
||||
}
|
||||
|
||||
cm := xmpp.NewStreamManager(component, nil)
|
||||
|
||||
return cm
|
||||
}
|
21
xmpp/handlers.go
Normal file
21
xmpp/handlers.go
Normal file
|
@ -0,0 +1,21 @@
|
|||
package xmpp
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
|
||||
"gosrc.io/xmpp"
|
||||
"gosrc.io/xmpp/stanza"
|
||||
)
|
||||
|
||||
func HandleMessage(s xmpp.Sender, p stanza.Packet) {
|
||||
msg, ok := p.(stanza.Message)
|
||||
if !ok {
|
||||
_, _ = fmt.Fprintf(os.Stdout, "Ignoring packet: %T\n", p)
|
||||
return
|
||||
}
|
||||
|
||||
_, _ = fmt.Fprintf(os.Stdout, "Body = %s - from = %s\n", msg.Body, msg.From)
|
||||
reply := stanza.Message{Attrs: stanza.Attrs{To: msg.From}, Body: msg.Body}
|
||||
_ = s.Send(reply)
|
||||
}
|
Loading…
Reference in a new issue