Example #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)
		}
	}
}
Example #2
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))
}