示例#1
0
// *whirrrrrrrrrrr*
func probe(db *bolt.DB, api *tbotapi.TelegramBotAPI, closed chan struct{}, wg *sync.WaitGroup) {
	dbBucketName := "alphazero"
	dbBucketStats := fmt.Sprintf("%s.stats", dbBucketName)
	for {
		select {
		case <-closed:
			wg.Done()
			return
		case val := <-api.Updates:
			typ := val.Message.Type()
			if typ != model.TextType {
				fmt.Fprintf(os.Stderr, "Not handling type: %s\n", typ)
				continue
			}

			response, md := process(db, val)
			db.Update(func(tx *bolt.Tx) error {
				b := tx.Bucket([]byte(dbBucketStats))
				id := []byte("messages.count.received")
				var val []byte
				if val = b.Get(id); val == nil {
					val = []byte("0")
				}
				c, _ := strconv.Atoi(string(val))
				c++
				return b.Put(id, []byte(strconv.Itoa(c)))
			})

			// unsafeval := blackfriday.MarkdownCommon([]byte(*val.Message.Text))
			// unsafe := blackfriday.MarkdownCommon([]byte(response))
			// html := bluemonday.UGCPolicy().SanitizeBytes(unsafe)

			sender := val.Message.Chat
			recipient := model.NewRecipientFromChat(val.Message.Chat)

			om := model.NewOutgoingMessage(
				model.NewChatRecipient(*recipient.ChatID),
				response).SetMarkdown(md)

			var msg *model.MessageResponse
			var err error

			msg, err = api.SendMessageExtended(om)

			if err != nil {
				msg, err = api.SendMessageExtended(om.SetMarkdown(false))
				if err != nil {
					fmt.Fprintf(os.Stderr, "Err: %s\n", err)
					continue
				}
			}

			fmt.Printf(
				"MessageID: %04d, ChatID: %s, Text: %s, IsGroupChat:%t\n",
				msg.Message.ID, sender.String(), response, msg.Message.Chat.IsGroupChat())
		case val := <-api.Errors:
			fmt.Printf("Err: %s\n", val)
		}
	}
}
示例#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
// SendMessage sends a text message to the chatID specified, with the given text.
// For more options, use the SendMessageExtended function.
// On success, the sent message is returned as a MessageResponse.
func (api *TelegramBotAPI) SendMessage(chatID int, text string) (*model.MessageResponse, error) {
	return api.SendMessageExtended(model.NewOutgoingMessage(model.NewChatRecipient(chatID), text))
}