parent
c0c21a35a4
commit
653b1bde94
@ -0,0 +1,33 @@
|
||||
package telegram
|
||||
|
||||
const notEnoughArguments string = "Not enough arguments"
|
||||
const telegramNotInitialized string = "Telegram connection is not initialized yet"
|
||||
|
||||
// ProcessTransportCommand executes commands sent directly to the component
|
||||
func (c *Client) ProcessTransportCommand(cmd string, args []string) string {
|
||||
switch cmd {
|
||||
case "login", "code", "password":
|
||||
if cmd == "login" && c.Session.Login != "" {
|
||||
return ""
|
||||
}
|
||||
|
||||
if len(args) < 1 {
|
||||
return notEnoughArguments
|
||||
}
|
||||
if c.authorizer == nil {
|
||||
return telegramNotInitialized
|
||||
}
|
||||
|
||||
switch cmd {
|
||||
case "login":
|
||||
c.authorizer.PhoneNumber <- args[0]
|
||||
c.Session.Login = args[0]
|
||||
case "code":
|
||||
c.authorizer.Code <- args[0]
|
||||
case "password":
|
||||
c.authorizer.Password <- args[0]
|
||||
}
|
||||
}
|
||||
|
||||
return ""
|
||||
}
|
@ -0,0 +1,53 @@
|
||||
package gateway
|
||||
|
||||
import (
|
||||
"encoding/xml"
|
||||
|
||||
log "github.com/sirupsen/logrus"
|
||||
"gosrc.io/xmpp"
|
||||
"gosrc.io/xmpp/stanza"
|
||||
)
|
||||
|
||||
// Jid stores the component's JID object
|
||||
var Jid *xmpp.Jid
|
||||
|
||||
// SendMessage creates and sends a message stanza
|
||||
func SendMessage(to string, from string, body string, component *xmpp.Component) {
|
||||
componentJid := Jid.Full()
|
||||
|
||||
var logFrom string
|
||||
var messageFrom string
|
||||
if from == "" {
|
||||
logFrom = componentJid
|
||||
messageFrom = componentJid
|
||||
} else {
|
||||
logFrom = from
|
||||
messageFrom = from + "@" + componentJid
|
||||
}
|
||||
|
||||
log.WithFields(log.Fields{
|
||||
"from": logFrom,
|
||||
"to": to,
|
||||
}).Warn("Got message")
|
||||
|
||||
message := stanza.Message{
|
||||
Attrs: stanza.Attrs{
|
||||
From: messageFrom,
|
||||
To: to,
|
||||
Type: "chat",
|
||||
},
|
||||
Body: body,
|
||||
}
|
||||
|
||||
// explicit check, as marshalling is expensive
|
||||
if log.GetLevel() == log.DebugLevel {
|
||||
xmlMessage, err := xml.Marshal(message)
|
||||
if err == nil {
|
||||
log.Debug(string(xmlMessage))
|
||||
} else {
|
||||
log.Debugf("%#v", message)
|
||||
}
|
||||
}
|
||||
|
||||
_ = component.Send(message)
|
||||
}
|
Loading…
Reference in new issue