示例#1
0
func main() {
	api, err := tbotapi.New(os.Getenv("TELEGRAM_API_KEY"))
	if err != nil {
		fmt.Fprintf(os.Stderr, "API key is missing.\nBye\n\n")
		log.Fatal(err)
	}

	dbName := "alphazerobot.db"
	dbBucketName := "alphazero"
	dbBucketLogs := fmt.Sprintf("%s.logs", dbBucketName)
	dbBucketStats := fmt.Sprintf("%s.stats", dbBucketName)

	workers := 3
	closed := make(chan struct{})
	db, err := bolt.Open(dbName, 0600, &bolt.Options{Timeout: 1 * time.Second})
	if err != nil {
		log.Fatal(err)
	}

	defer func() {
		// api.Close() // waiting 1 minute... too long
		logStop(db)
		db.Close()
		fmt.Fprintf(os.Stdout, "Bye\n")
	}()

	createBucket(db, dbBucketLogs)
	createBucket(db, dbBucketStats)
	logStart(db)

	printDBDebug(db)

	wg := &sync.WaitGroup{}
	sigs := make(chan os.Signal, 1)
	signal.Notify(sigs, os.Interrupt)
	go func() {
		for sig := range sigs {
			fmt.Fprintf(os.Stdout, "Closing with Ctrl-C (%s)\n", sig.String())
			for i := 0; i < workers; i++ {
				closed <- struct{}{}
			}
			close(closed)
			break
		}
	}()

	fmt.Fprintf(os.Stdout, "User ID: \t%d\n", api.ID)
	fmt.Fprintf(os.Stdout, "Bot Name: \t%s\n", api.Name)
	fmt.Fprintf(os.Stdout, "Bot Username: \t%s\n", api.Username)

	for i := 0; i < workers; i++ {
		wg.Add(1)
		go probe(db, api, closed, wg)
	}
	wg.Wait()
}
示例#2
0
func sendMsg(msg string) error {
	api, err := tbotapi.New("196770228:AAGx4xbx6iBk7Y4bt2_bTrSVc0OyorEis18")
	if err != nil {
		return err
	}

	fmt.Printf("User ID: %d\n", api.ID)
	fmt.Printf("Bot Name: %s\n", api.Name)
	fmt.Printf("Bot Username: %s\n", api.Username)

	recipient := model.NewChannelRecipient("@englishforreading")
	message := model.NewOutgoingMessage(recipient, msg).SetMarkdown(true)

	_, err = api.SendMessageExtended(message)
	if err != nil {
		return err
	}

	api.Close()
	return nil
}
示例#3
0
func main() {
	api, err := tbotapi.New("YOUR_API_KEY")
	if err != nil {
		log.Fatal(err)
	}

	// just to show its working
	fmt.Printf("User ID: %d\n", api.ID)
	fmt.Printf("Bot Name: %s\n", api.Name)
	fmt.Printf("Bot Username: %s\n", api.Username)

	closed := make(chan struct{})
	wg := &sync.WaitGroup{}

	wg.Add(1)
	go func() {
		for {
			select {
			case <-closed:
				wg.Done()
				return
			case val := <-api.Updates:
				typ := val.Message.Type()
				if typ != model.TextType {
					//ignore non-text messages for now
					continue
				}

				// -> simple echo bot
				msg, err := api.SendMessage(model.NewRecipientFromChat(val.Message.Chat), *val.Message.Text)

				//or
				//msg, err := api.SendMessage(model.NewChatRecipient(val.Message.Chat.Id), *val.Message.Text)

				// -> simple echo bot with disabled web page preview
				//msg, err := api.SendMessageExtended(model.NewOutgoingMessage(model.NewChatRecipient(val.Message.Chat.Id), val.Message.Text).SetDisableWebPagePreview(true))

				// or:
				//msg, err := api.SendMessageExtended(model.NewOutgoingMessage(model.NewRecipientFromChat(val.Message.Chat), val.Message.Text).SetDisableWebPagePreview(true))

				// -> simple echo bot via forwarding
				//msg, err = api.ForwardMessage(model.NewOutgoingForward(model.NewRecipientFromChat(val.Message.Chat), val.Message.Chat, val.Message.Id))

				// -> bot that always sends an image as response
				//msg, err := api.SendPhoto(model.NewOutgoingPhoto(model.NewChatRecipient(val.Message.Chat.Id)), "/path/to/your/image.jpg")

				if err != nil {
					fmt.Printf("Err: %s\n", err)
					continue
				}
				fmt.Printf("MessageID: %d, Text: %s, IsGroupChat:%t\n", msg.Message.ID, *msg.Message.Text, msg.Message.Chat.IsGroupChat())
			case val := <-api.Errors:
				fmt.Printf("Err: %s\n", val)
			}
		}
	}()

	// let it run for five minutes
	time.Sleep(time.Duration(5) * time.Minute)

	fmt.Println("Closing...")

	api.Close()
	close(closed)
	wg.Wait()
}