Skip to content

Commit

Permalink
feat(telegram): implement echo bot
Browse files Browse the repository at this point in the history
  • Loading branch information
ernado committed Nov 28, 2020
1 parent 72a78eb commit 7664b5f
Show file tree
Hide file tree
Showing 695 changed files with 214 additions and 30 deletions.
File renamed without changes.
53 changes: 52 additions & 1 deletion telegram/cmd/gotdcli/main.go → cmd/gotdecho/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package main

import (
"context"
"crypto/rand"
"fmt"
"os"
"strconv"
Expand All @@ -10,7 +11,9 @@ import (
"go.uber.org/zap/zapcore"
"golang.org/x/xerrors"

"github.com/ernado/td/crypto"
"github.com/ernado/td/telegram"
"github.com/ernado/td/tg"
)

func run(ctx context.Context) error {
Expand Down Expand Up @@ -50,6 +53,53 @@ func run(ctx context.Context) error {
return xerrors.Errorf("failed to init connection: %w", err)
}

client.SetUpdateHandler(func(updates *tg.Updates) error {
// This wll be required to send message back.
users := map[int]*tg.User{}
for _, u := range updates.Users {
user, ok := u.(*tg.User)
if !ok {
continue
}
users[user.ID] = user
}

for _, update := range updates.Updates {
switch u := update.(type) {
case *tg.UpdateNewMessage:
switch m := u.Message.(type) {
case *tg.Message:
switch peer := m.PeerID.(type) {
case *tg.PeerUser:
user := users[peer.UserID]
logger.With(
zap.String("text", m.Message),
zap.Int("user_id", user.ID),
zap.String("user_first_name", user.FirstName),
zap.String("username", user.Username),
).Info("Got message")

randomID, err := crypto.RandInt64(rand.Reader)
if err != nil {
return err
}
return client.SendMessage(ctx, &tg.MessagesSendMessageRequest{
RandomID: randomID,
Message: m.Message,
Peer: &tg.InputPeerUser{
UserID: user.ID,
AccessHash: user.AccessHash,
},
})
}
}
default:
logger.With(zap.String("update_type", fmt.Sprintf("%T", u))).Info("Ignoring update")
}
}
return nil
})

// Trying to log in as bot.
if err := client.BotLogin(ctx, telegram.BotLogin{
ID: appID,
Expand All @@ -59,7 +109,8 @@ func run(ctx context.Context) error {
return xerrors.Errorf("failed to perform bot login: %w", err)
}

return nil
// Just reading updates.
select {}
}

func main() {
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
2 changes: 1 addition & 1 deletion gen/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,4 @@ package gen
// Templates should be first.
//go:generate go run github.com/go-bindata/go-bindata/go-bindata -pkg=internal -o=internal/bindata.go -mode=420 -modtime=1 ./_template/...

//go:generate go run github.com/ernado/td/gen/cmd/gotdgen --clean --package td --target example --schema _testdata/example.tl
//go:generate go run github.com/ernado/td/cmd/gotdgen --clean --package td --target example --schema _testdata/example.tl
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,6 @@ require (
github.com/sergi/go-diff v1.1.0 // indirect
github.com/stretchr/testify v1.6.1
go.uber.org/zap v1.16.0
golang.org/x/sys v0.0.0-20200223170610-d5e6a3e2c0ae // indirect
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1
)
4 changes: 3 additions & 1 deletion go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,12 @@ github.com/go-bindata/go-bindata v3.1.2+incompatible h1:5vjJMVhowQdPzjE1LdxyFF7Y
github.com/go-bindata/go-bindata v3.1.2+incompatible/go.mod h1:xK8Dsgwmeed+BBsSy2XTopBn/8uK2HWuGSnA11C3Joo=
github.com/go-openapi/inflect v0.19.0 h1:9jCH9scKIbHeV9m12SmPilScz6krDxKRasNNSNPXu/4=
github.com/go-openapi/inflect v0.19.0/go.mod h1:lHpZVlpIQqLyKwJ4N+YSc9hchQy/i12fJykb83CRBH4=
github.com/go-playground/assert/v2 v2.0.1 h1:MsBgLAaY856+nPRTKrp3/OZK38U/wa0CcBYNjji3q3A=
github.com/go-playground/assert/v2 v2.0.1/go.mod h1:VDjEfimB/XKnb+ZQfWdccd7VUvScMdVu0Titje2rxJ4=
github.com/go-playground/locales v0.13.0 h1:HyWk6mgj5qFqCT5fjGBuRArbVDfE4hi8+e8ceBS/t7Q=
github.com/go-playground/locales v0.13.0/go.mod h1:taPMhCMXrRLJO55olJkUXHZBHCxTMfnGwq/HNwmWNS8=
github.com/go-playground/universal-translator v0.17.0 h1:icxd5fm+REJzpZx7ZfpaD876Lmtgy7VtROAbHHXk8no=
github.com/go-playground/universal-translator v0.17.0/go.mod h1:UkSxE5sNxxRwHyU+Scu5vgOQjsIJAF8j9muTVoKLVtA=
github.com/go-playground/validator v9.31.0+incompatible h1:UA72EPEogEnq76ehGdEDp4Mit+3FDh548oRqwVgNsHA=
github.com/go-playground/validator/v10 v10.4.1 h1:pH2c5ADXtd66mxoE0Zm9SUhxE20r7aM3F26W0hOn+GE=
github.com/go-playground/validator/v10 v10.4.1/go.mod h1:nlOn6nFhuKACm19sB/8EGNn9GlaMV7XkbRSipzJ0Ii4=
github.com/google/renameio v0.1.0/go.mod h1:KWCgfxg9yswjAJkECMjeO8J8rahYeXnNhOm40UhjYkI=
Expand Down Expand Up @@ -68,6 +68,8 @@ golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJ
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190412213103-97732733099d h1:+R4KGOnez64A81RvjARKc4UT5/tI9ujCIVX+P5KiHuI=
golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20200223170610-d5e6a3e2c0ae h1:/WDfKMnPU+m5M4xB+6x4kaepxRw6jWvR5iDRdvjHgy8=
golang.org/x/sys v0.0.0-20200223170610-d5e6a3e2c0ae/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.2/go.mod h1:bEr9sfX3Q8Zfm5fL9x+3itogRgK3+ptLWKqgva+5dAk=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
Expand Down
4 changes: 1 addition & 3 deletions internal/generate.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
package internal

//go:generate go run github.com/ernado/td/gen/cmd/gotdgen --clean --package mt --target mt --schema _schema/schema.tl

//go:generate go run github.com/ernado/td/gen/cmd/gotdgen --clean --package tg --target tg --schema _schema/telegram.tl
//go:generate go run github.com/ernado/td/cmd/gotdgen --clean --package mt --target mt --schema _schema/schema.tl
2 changes: 1 addition & 1 deletion internal/proto/container.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package proto

import (
"github.com/ernado/td/bin"
"github.com/ernado/td/internal/crypto"
"github.com/ernado/td/crypto"

"golang.org/x/xerrors"
)
Expand Down
2 changes: 1 addition & 1 deletion internal/proto/encrypted_message_data.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package proto

import (
"github.com/ernado/td/internal/crypto"
"github.com/ernado/td/crypto"

"github.com/ernado/td/bin"
)
Expand Down
2 changes: 1 addition & 1 deletion internal/proto/encrypted_message_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"math/big"
"testing"

"github.com/ernado/td/internal/crypto"
"github.com/ernado/td/crypto"

"github.com/ernado/td/bin"
)
Expand Down
2 changes: 1 addition & 1 deletion internal/proto/rpc_result.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package proto

import (
"github.com/ernado/td/bin"
"github.com/ernado/td/internal/crypto"
"github.com/ernado/td/crypto"
)

const ResultTypeID = 0xf35c6d01
Expand Down
2 changes: 1 addition & 1 deletion internal/proto/unencrypted_message.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"fmt"

"github.com/ernado/td/bin"
"github.com/ernado/td/internal/crypto"
"github.com/ernado/td/crypto"
)

// UnencryptedMessage is plaintext message.
Expand Down
2 changes: 2 additions & 0 deletions td.go
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
// Package td implements MTProto encoding and decoding.
package td

//go:generate go run github.com/ernado/td/cmd/gotdgen --clean --package tg --target tg --schema _schema/telegram.tl
2 changes: 1 addition & 1 deletion telegram/bot_login.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"golang.org/x/xerrors"

"github.com/ernado/td/bin"
"github.com/ernado/td/internal/tg"
"github.com/ernado/td/tg"
)

// BotLogin wraps credentials that are required to login as bot.
Expand Down
5 changes: 4 additions & 1 deletion telegram/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,9 @@ import (
"golang.org/x/xerrors"

"github.com/ernado/td/bin"
"github.com/ernado/td/internal/crypto"
"github.com/ernado/td/crypto"
"github.com/ernado/td/internal/proto"
"github.com/ernado/td/tg"
)

// Client represents a MTProto client to Telegram.
Expand All @@ -29,6 +30,8 @@ type Client struct {
seq int
log *zap.Logger

updateHandler func(u *tg.Updates) error

// callbacks for rpc requests
rpcMux sync.Mutex
rpc map[crypto.MessageID]func(b *bin.Buffer, rpcErr error)
Expand Down
2 changes: 1 addition & 1 deletion telegram/create_auth_key.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import (
"golang.org/x/xerrors"

"github.com/ernado/td/bin"
"github.com/ernado/td/internal/crypto"
"github.com/ernado/td/crypto"
"github.com/ernado/td/internal/mt"
"github.com/ernado/td/internal/proto"
)
Expand Down
2 changes: 1 addition & 1 deletion telegram/decrypt.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
"golang.org/x/xerrors"

"github.com/ernado/td/bin"
"github.com/ernado/td/internal/crypto"
"github.com/ernado/td/crypto"
"github.com/ernado/td/internal/proto"
)

Expand Down
2 changes: 1 addition & 1 deletion telegram/encrypt.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (

"github.com/ernado/ige"

"github.com/ernado/td/internal/crypto"
"github.com/ernado/td/crypto"
"github.com/ernado/td/internal/proto"
)

Expand Down
2 changes: 1 addition & 1 deletion telegram/encrypt_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
"go.uber.org/zap"

"github.com/ernado/td/bin"
"github.com/ernado/td/internal/crypto"
"github.com/ernado/td/crypto"
)

type Zero struct{}
Expand Down
2 changes: 1 addition & 1 deletion telegram/handle_bad_msg.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package telegram
import (
"fmt"

"github.com/ernado/td/internal/crypto"
"github.com/ernado/td/crypto"

"golang.org/x/xerrors"

Expand Down
24 changes: 24 additions & 0 deletions telegram/handle_gzip.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package telegram

import (
"golang.org/x/xerrors"

"github.com/ernado/td/bin"
"github.com/ernado/td/internal/proto"
)

func (c *Client) gzip(b *bin.Buffer) (*bin.Buffer, error) {
var content proto.GZIP
if err := content.Decode(b); err != nil {
return nil, xerrors.Errorf("failed to decode: %w", err)
}
return &bin.Buffer{Buf: content.Data}, nil
}

func (c *Client) handleGZIP(b *bin.Buffer) error {
content, err := c.gzip(b)
if err != nil {
return xerrors.Errorf("failed to unzip: %w", err)
}
return c.handleMessage(content)
}
9 changes: 5 additions & 4 deletions telegram/handle_result.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,13 @@ func (c *Client) handleResult(b *bin.Buffer) error {
return err
}
if id == proto.GZIPTypeID {
var content proto.GZIP
if err := content.Decode(b); err != nil {
return xerrors.Errorf("failed to decode: %w", err)
content, err := c.gzip(b)
if err != nil {
return xerrors.Errorf("failed to decompres: %w", err)
}

// Replacing buffer so callback will deal with uncompressed data.
b = &bin.Buffer{Buf: content.Data}
b = content

// Replacing id with inner id if error is compressed for any reason.
if id, err = b.PeekID(); err != nil {
Expand Down
49 changes: 49 additions & 0 deletions telegram/handle_updates.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package telegram

import (
"fmt"

"go.uber.org/zap"
"golang.org/x/xerrors"

"github.com/ernado/td/bin"
"github.com/ernado/td/tg"
)

func (c *Client) processUpdates(updates tg.UpdatesClass) error {
if c.updateHandler == nil {
// Ignoring. Probably we should ACK.
return nil
}
switch u := updates.(type) {
case *tg.Updates:
go func() {
if c.updateHandler == nil {
return
}
// We should send ACK here.
if err := c.updateHandler(u); err != nil {
c.log.With(zap.Error(err)).Error("Update handler returning error")
}
}()
return nil
default:
c.log.With(zap.String("update_type", fmt.Sprintf("%T", u))).Debug("Ignoring update")
}
return nil
}

func (c *Client) handleUpdates(b *bin.Buffer) error {
updates, err := tg.DecodeUpdates(b)
if err != nil {
return xerrors.Errorf("failed to decode updates: %w", err)
}
return c.processUpdates(updates)
}

// SetUpdateHandler sets handler as default update handler.
//
// Provided handler will be called on received update.
func (c *Client) SetUpdateHandler(handler func(u *tg.Updates) error) {
c.updateHandler = handler
}
2 changes: 1 addition & 1 deletion telegram/init_connection.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package telegram
import (
"context"

"github.com/ernado/td/internal/tg"
"github.com/ernado/td/tg"

"go.uber.org/zap"

Expand Down
2 changes: 1 addition & 1 deletion telegram/message_id.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package telegram

import "github.com/ernado/td/internal/crypto"
import "github.com/ernado/td/crypto"

func (c *Client) newMessageID() crypto.MessageID {
return crypto.NewMessageID(c.clock(), crypto.MessageFromClient)
Expand Down
2 changes: 1 addition & 1 deletion telegram/new_encrypted_msg.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"go.uber.org/zap"

"github.com/ernado/td/bin"
"github.com/ernado/td/internal/crypto"
"github.com/ernado/td/crypto"
"github.com/ernado/td/internal/proto"
)

Expand Down
2 changes: 1 addition & 1 deletion telegram/ping.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"golang.org/x/xerrors"

"github.com/ernado/td/bin"
"github.com/ernado/td/internal/crypto"
"github.com/ernado/td/crypto"
)

type pingMessage struct {
Expand Down
Loading

0 comments on commit 7664b5f

Please sign in to comment.