Esempio n. 1
0
func main() {
	log.SetFlags(0)
	log.SetPrefix("echobot: ")
	flag.Usage = usage
	flag.Parse()

	if *webhook == "" {
		log.Printf("missing webhook parameter\n\n")
		flag.Usage()
	}
	if *token == "" {
		log.Printf("missing token parameter\n\n")
		flag.Usage()
	}

	b := tlbot.New(*token)
	err := b.SetWebhook(*webhook)
	if err != nil {
		log.Fatal(err)
	}

	messages := b.Listen(net.JoinHostPort(*host, *port))
	for msg := range messages {
		go func() {
			// echo the message as *bold*
			txt := "*" + msg.Text + "*"
			err := b.SendMessage(msg.Chat.ID, txt, tlbot.ModeMarkdown, false, nil)
			if err != nil {
				log.Printf("Error while sending message. Err: %v\n", err)
			}
		}()
	}
}
Esempio n. 2
0
func main() {
	log.SetPrefix("ilber: ")
	log.SetFlags(log.LstdFlags | log.Lshortfile)
	flag.Usage = usage
	flag.Parse()

	config, err := readConfig(*flagConfig)
	if err != nil {
		fmt.Fprintf(os.Stderr, "configuration error: %v\n", err)
		os.Exit(1)
	}

	b := tlbot.New(config.Token)
	if err := b.SetWebhook(config.Webhook); err != nil {
		log.Fatalf("error while setting webhook: %v", err)
	}
	log.Printf("Webhook set to %v\n", config.Webhook)

	if config.Profile {
		go func() {
			log.Println("Exposing profile information on http://:6969")
			log.Printf("profile error: %v", http.ListenAndServe(":6969", nil))
		}()
	}

	ctx := newCtxWithValues(config)

	messages := b.Listen(net.JoinHostPort(config.Host, config.Port))
	for msg := range messages {
		log.Printf("%v\n", msg)

		// react only to user sent messages
		if msg.IsService() {
			continue
		}
		// is message a bot command?
		cmdname := msg.Command()
		if cmdname == "" {
			continue
		}

		// is the command even registered?
		cmd := command.Lookup(cmdname)
		if cmd == nil {
			continue
		}

		// it is. cool, run it!
		go cmd.Run(ctx, &b, &msg)
	}
}