summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAlexander Kiryukhin <a.kiryukhin@mail.ru>2019-04-09 18:14:00 +0300
committerAlexander Kiryukhin <a.kiryukhin@mail.ru>2019-04-09 18:14:00 +0300
commitf42a0559562be0fa571db8f34743afce1a9602c6 (patch)
treed06c0e4f516ce9aa10442943153ddc3705e113a4
first commit
-rw-r--r--bot.go124
-rw-r--r--go.mod10
-rw-r--r--go.sum8
3 files changed, 142 insertions, 0 deletions
diff --git a/bot.go b/bot.go
new file mode 100644
index 0000000..f84cb44
--- /dev/null
+++ b/bot.go
@@ -0,0 +1,124 @@
+package main
+
+import (
+ "context"
+ "fmt"
+ "log"
+ "os"
+
+ "github.com/go-telegram-bot-api/telegram-bot-api"
+ "github.com/neonxp/rutina"
+ "github.com/neonxp/tamtam"
+)
+
+type message struct {
+ From string
+ To int64
+ Text string
+}
+
+type routing struct {
+ TgID int64
+ TTID int64
+}
+
+func main() {
+ tamtamToken := os.Getenv("TAMTAM")
+ telegramToken := os.Getenv("TELEGRAM")
+ r := rutina.New()
+
+ routes := []routing{
+ {TgID: -1001046507545, TTID: -69062921115159},
+ }
+
+ tt2tg := make(chan message, 1)
+ tg2tt := make(chan message, 1)
+
+ // region TamTam
+ tamtamApi := tamtam.New(tamtamToken)
+ ttch := make(chan interface{}, 1)
+ r.Go(func(ctx context.Context) error {
+ for {
+ select {
+ case <-ctx.Done():
+ return nil
+ case upd := <-ttch:
+ log.Printf("[TT] Received: %#v", upd)
+ switch upd := upd.(type) {
+ case tamtam.UpdateMessageCreated:
+ for _, r := range routes {
+ if r.TTID == upd.Message.Recipient.ChatId {
+ from := upd.Message.Sender.Name
+ if upd.Message.Sender.Username != "" {
+ from = fmt.Sprintf("%s (%s)", upd.Message.Sender.Name, upd.Message.Sender.Username)
+ }
+ tt2tg <- message{
+ To: r.TgID,
+ From: from,
+ Text: upd.Message.Body.Text,
+ }
+ }
+ }
+
+ default:
+ log.Printf("Unknown type: %#v", upd)
+ }
+ case msg := <-tg2tt:
+ res, err := tamtamApi.SendMessage(msg.To, msg.To, &tamtam.NewMessageBody{
+ Text: fmt.Sprintf("[TG] %s: %s", msg.From, msg.Text),
+ })
+ log.Printf("[TT] Answer: %#v %#v", res, err)
+ }
+ }
+ }, rutina.ShutdownIfDone, rutina.RestartIfFail)
+ r.Go(func(ctx context.Context) error {
+ return tamtamApi.GetUpdatesLoop(ctx, ttch)
+ }, rutina.ShutdownIfDone, rutina.RestartIfFail)
+ // endregion
+
+ // region Telegram
+ tgApi, err := tgbotapi.NewBotAPI(telegramToken)
+ if err != nil {
+ log.Panic(err)
+ }
+
+ tgApi.Debug = true
+ u := tgbotapi.NewUpdate(0)
+ u.Timeout = 60
+ updates, err := tgApi.GetUpdatesChan(u)
+ r.Go(func(ctx context.Context) error {
+ for {
+ select {
+ case <-ctx.Done():
+ return nil
+ case upd := <-updates:
+ if upd.Message == nil { // ignore any non-Message Updates
+ continue
+ }
+ for _, r := range routes {
+ if r.TgID == upd.Message.Chat.ID {
+ from := fmt.Sprintf("%s %s", upd.Message.From.FirstName, upd.Message.From.LastName)
+ if upd.Message.From.UserName != "" {
+ from = fmt.Sprintf("%s %s (%s)", upd.Message.From.FirstName, upd.Message.From.LastName, upd.Message.From.UserName)
+ }
+ tg2tt <- message{
+ To: r.TTID,
+ From: from,
+ Text: upd.Message.Text,
+ }
+ }
+ }
+ case msg := <-tt2tg:
+ res, err := tgApi.Send(tgbotapi.NewMessage(msg.To, fmt.Sprintf("[TT] %s: %s", msg.From, msg.Text)))
+ log.Printf("[TG] Answer: %#v %#v", res, err)
+ }
+ }
+ }, rutina.ShutdownIfDone, rutina.RestartIfFail)
+
+ // endregion
+
+ r.ListenOsSignals()
+ if err := r.Wait(); err != nil {
+ log.Fatal(err)
+ }
+}
diff --git a/go.mod b/go.mod
new file mode 100644
index 0000000..4b87825
--- /dev/null
+++ b/go.mod
@@ -0,0 +1,10 @@
+module transport
+
+go 1.12
+
+require (
+ github.com/go-telegram-bot-api/telegram-bot-api v4.6.4+incompatible
+ github.com/neonxp/rutina v0.4.3
+ github.com/neonxp/tamtam v0.1.0
+ github.com/technoweenie/multipartstreamer v1.0.1 // indirect
+)
diff --git a/go.sum b/go.sum
new file mode 100644
index 0000000..4c428aa
--- /dev/null
+++ b/go.sum
@@ -0,0 +1,8 @@
+github.com/go-telegram-bot-api/telegram-bot-api v4.6.4+incompatible h1:2cauKuaELYAEARXRkq2LrJ0yDDv1rW7+wrTEdVL3uaU=
+github.com/go-telegram-bot-api/telegram-bot-api v4.6.4+incompatible/go.mod h1:qf9acutJ8cwBUhm1bqgz6Bei9/C/c93FPDljKWwsOgM=
+github.com/neonxp/rutina v0.4.3 h1:It7wu2L1FlPMC7UFGS7cTdMPWqd2hwL6+xP8UP72xZc=
+github.com/neonxp/rutina v0.4.3/go.mod h1:QJOHIcMI4Lh4Nyyi0v119KZllW1S5KxJyy/zg5KQXno=
+github.com/neonxp/tamtam v0.1.0 h1:L7u59d0Yj0TBSXSV6YKnlQrSoaei004eD+DWxMbEkaY=
+github.com/neonxp/tamtam v0.1.0/go.mod h1:8e/f0WPbHVY6VS4ngTdXCJ11afv5+DjakMQAiI23b9Q=
+github.com/technoweenie/multipartstreamer v1.0.1 h1:XRztA5MXiR1TIRHxH2uNxXxaIkKQDeX7m2XsSOlQEnM=
+github.com/technoweenie/multipartstreamer v1.0.1/go.mod h1:jNVxdtShOxzAsukZwTSw6MDx5eUJoiEBsSvzDU9uzog=