Skip to content

Commit

Permalink
bot messsaging repl
Browse files Browse the repository at this point in the history
  • Loading branch information
tsyber1an committed Jun 8, 2021
1 parent f22b280 commit ff40ed1
Show file tree
Hide file tree
Showing 5 changed files with 129 additions and 0 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1 +1,5 @@
# telegram-cli

### the demo

![alt text](./demo.png "Telegram bot reply demo")
Binary file added demo.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
8 changes: 8 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
module github.com/Funfun/telegram-cli

go 1.16

require (
github.com/urfave/cli/v2 v2.3.0
gopkg.in/tucnak/telebot.v2 v2.3.5
)
24 changes: 24 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
github.com/cpuguy83/go-md2man/v2 v2.0.0-20190314233015-f79a8a8ca69d h1:U+s90UTSYgptZMwQh2aRr3LuazLJIa+Pg3Kc1ylSYVY=
github.com/cpuguy83/go-md2man/v2 v2.0.0-20190314233015-f79a8a8ca69d/go.mod h1:maD7wRr/U5Z6m/iR4s+kqSMx2CaBsrgA7czyZG/E6dU=
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/pkg/errors v0.8.1 h1:iURUrRGxPUNPdy5/HRSm+Yj6okJ6UtLINN0Q9M4+h3I=
github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/russross/blackfriday/v2 v2.0.1 h1:lPqVAte+HuHNfhJ/0LC98ESWRz8afy9tM/0RK8m9o+Q=
github.com/russross/blackfriday/v2 v2.0.1/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
github.com/shurcooL/sanitized_anchor_name v1.0.0 h1:PdmoCO6wvbs+7yrJyMORt4/BmY5IYyJwS/kOiWx8mHo=
github.com/shurcooL/sanitized_anchor_name v1.0.0/go.mod h1:1NzhyTcUVG4SuEtjjoZeVRXNmyL/1OwPU0+IJeTBvfc=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.5.1 h1:nOGnQDM7FYENwehXlg/kFVnos3rEvtKTjRvOWSzb6H4=
github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5cxcmMvtA=
github.com/urfave/cli/v2 v2.3.0 h1:qph92Y649prgesehzOrQjdWyxFOp/QVM+6imKHad91M=
github.com/urfave/cli/v2 v2.3.0/go.mod h1:LJmUH05zAU44vOAcrfzZQKsZbVcdbOG8rtL3/XcUArI=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/tucnak/telebot.v2 v2.3.5 h1:TdMJTlG8kvepsvZdy/gPeYEBdwKdwFFjH1AQTua9BOU=
gopkg.in/tucnak/telebot.v2 v2.3.5/go.mod h1:BgaIIx50PSRS9pG59JH+geT82cfvoJU/IaI5TJdN3v8=
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.2.3 h1:fvjTMHxHEw/mxHbtzPi3JCcKXQRAnQTBRo6YCJSVHKI=
gopkg.in/yaml.v2 v2.2.3/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
93 changes: 93 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
package main

import (
"errors"
"fmt"
"log"
"os"
"time"

"github.com/urfave/cli/v2"
tb "gopkg.in/tucnak/telebot.v2"
)

func NewBot(apiToken string) (*tb.Bot, error) {
if apiToken == "" {
return nil, errors.New("token can not be empty")
}

var bot *tb.Bot
tbSettings := tb.Settings{
Poller: &tb.LongPoller{Timeout: 10 * time.Second},
}
tbSettings.Token = apiToken
var err error
bot, err = tb.NewBot(tbSettings)
if err != nil {
return nil, fmt.Errorf("failed to init a bot, got the error: %s", err)
}

return bot, nil
}

func main() {

app := &cli.App{
Name: "Telegram Bot REPL",
Flags: []cli.Flag{
&cli.StringFlag{
Name: "api_token",
Value: "",
Usage: "api_token",
},
},
Usage: "Telegram Bot REPL",
Commands: []*cli.Command{
{
Name: "tell",
Aliases: []string{"install"},
Usage: "handle a message and reply",
Subcommands: []*cli.Command{
{
Name: "sendMessage",
Usage: "tell which command and which reply text should",
Action: func(c *cli.Context) error {
bot, err := NewBot(c.String("api_token"))
if err != nil {
return err
}

bot.Handle(os.Args[5], func(m *tb.Message) {
log.Printf("command %s received. Repling with %s", os.Args[5], os.Args[6])
bot.Send(m.Sender, os.Args[6])
log.Println("a message was send. Stopping a bot")
bot.Stop()
return
})

log.Println("Waiting a text messge")
bot.Start()

return nil
},
},
},
Action: func(c *cli.Context) error {
_, err := NewBot(c.String("api_token"))
if err != nil {
return err
}

log.Println("The Bot is on. Ready to receive commands")

return nil
},
},
},
}

err := app.Run(os.Args)
if err != nil {
log.Fatal(err)
}
}

0 comments on commit ff40ed1

Please sign in to comment.