当前位置: 首页>>代码示例>>Golang>>正文


Golang telebot.Chat类代码示例

本文整理汇总了Golang中github.com/tucnak/telebot.Chat的典型用法代码示例。如果您正苦于以下问题:Golang Chat类的具体用法?Golang Chat怎么用?Golang Chat使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。


在下文中一共展示了Chat类的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。

示例1: getAllUsernamesForChat

// getAllUsernamesForChat returns all usernames for a chat, sans the sender's.
func (j *JarvisBot) getAllUsernamesForChat(chat *telebot.Chat, sender *telebot.User) (string, error) {
	uArray, groupID, senderID := []string{}, strconv.Itoa(int(chat.ID)), strconv.Itoa(sender.ID)

	if !chat.IsGroupChat() {
		return "", fmt.Errorf("chat %s is not a group chat!", groupID)
	}

	err := j.db.View(func(tx *bolt.Tx) error {
		b := tx.Bucket(group_usernames_bucket_name)
		gb := b.Bucket([]byte(groupID))
		if gb == nil {
			return fmt.Errorf("error retrieving bucket for group ID %s", groupID)
		}

		gb.ForEach(func(k, v []byte) error {
			key, curr := string(k), string(v)
			if key != senderID && key != timestampKey && key != countKey {
				uArray = append(uArray, curr)
			}
			return nil
		})
		return nil
	})

	if err != nil {
		return "", err
	}

	res := ""
	for _, v := range uArray {
		res = res + "@" + v + " "
	}
	return res, nil
}
开发者ID:rahulg,项目名称:jarvisbot,代码行数:35,代码来源:ping.go

示例2: usernameExistsForChat

// usernameExistsForChat checks if a username exists for a given chat.
// This returns false if the userID exists but the username has changed.
func (j *JarvisBot) usernameExistsForChat(chat *telebot.Chat, sender *telebot.User) bool {
	res := false
	// Guard against possibility that chat isn't a group chat.
	// We're lazy here, returning true implies that we don't want to run a save.
	if !chat.IsGroupChat() {
		return true
	}

	groupID, senderID, username := strconv.Itoa(int(chat.ID)), strconv.Itoa(sender.ID), sender.Username

	j.db.View(func(tx *bolt.Tx) error {
		b := tx.Bucket(group_usernames_bucket_name)
		gb := b.Bucket([]byte(groupID))
		if gb == nil {
			return nil
		}

		v := gb.Get([]byte(senderID))
		if v != nil && string(v) == username {
			res = true
			return nil
		}

		return nil
	})

	return res
}
开发者ID:rahulg,项目名称:jarvisbot,代码行数:30,代码来源:ping.go

示例3: GetDefinition

// GetDefinition get definition
func GetDefinition(msg string, sender telebot.Chat) string {
	var definition string
	if sender.IsGroupChat() {
		if strings.HasPrefix(msg, BOT_NAME) {
			msg = msg[len(BOT_NAME):]
		} else if strings.HasSuffix(msg, BOT_NAME) {
			msg = msg[:len(msg)-len(BOT_NAME)]
		} else {
			return ""
		}
	}

	switch {
	case strings.HasPrefix(msg, "/start"):
		definition = fmt.Sprintf(GREETING_MSG, GetName(sender), GetName(sender))
	case strings.HasPrefix(msg, "/") || msg == "":
		definition = HELP_MSG
	case strings.HasPrefix(msg, "/stop"):
		definition = BYE_MSG
	default:
		definition = GetDefinitionFromDb(msg, sender)
	}

	return definition
}
开发者ID:aladine,项目名称:envndictbot,代码行数:26,代码来源:main.go

示例4: GetName

func GetName(sender telebot.Chat) string {
	name := sender.FirstName + " " + sender.LastName
	if sender.IsGroupChat() {
		name = sender.Title
	}
	return name
}
开发者ID:aladine,项目名称:envndictbot,代码行数:7,代码来源:main.go

示例5: saveUsernameSafely

// saveUsernameSafely checks if a user exists in a given group chat, and saves if he/she
// doesn't currently exist.
// We try as much as possible to avoid opening a Read-Write transaction, because
// Bolt can only have one Read-Write transaction at any time.
func (j *JarvisBot) saveUsernameSafely(chat *telebot.Chat, sender *telebot.User) {
	if sender.Username == "" || !chat.IsGroupChat() {
		return
	}

	if !j.usernameExistsForChat(chat, sender) {
		err := j.saveUserToDB(chat, sender)
		if err != nil {
			j.log.Printf("[%s] error saving user %s to group %s: %s", time.Now().Format(time.RFC3339), sender.Username, chat.Title, err)
		}
	}
}
开发者ID:rahulg,项目名称:jarvisbot,代码行数:16,代码来源:ping.go

示例6: updateLastSentTime

// updateLastSentTime updates the last sent time for a given chat.
func (j *JarvisBot) updateLastSentTime(chat *telebot.Chat) error {
	if !chat.IsGroupChat() {
		return fmt.Errorf("chat is not a group chat!")
	}

	groupID := strconv.Itoa(int(chat.ID))
	err := j.db.Update(func(tx *bolt.Tx) error {
		b := tx.Bucket(group_usernames_bucket_name)
		gb := b.Bucket([]byte(groupID))
		if gb == nil {
			return fmt.Errorf("invariant error - group bucket does not exist, yet attempt to update timestamp")
		}

		timeString := gb.Get([]byte(timestampKey))
		if timeString == nil {
			return resetTime(gb)
		} else {
			lastTime, err := time.Parse(time.RFC3339, string(timeString))
			if err != nil {
				return err
			}
			if time.Since(lastTime) > time.Hour {
				return resetTime(gb)
			} else {
				countBytes := gb.Get([]byte(countKey))
				if countBytes == nil {
					return fmt.Errorf("invariant error - timestamp was saved but no count exists")
				}
				lastCount, err := strconv.Atoi(string(countBytes))
				if err != nil {
					return err
				}
				newCount := strconv.Itoa(lastCount + 1)
				err = gb.Put([]byte(countKey), []byte(newCount))
				if err != nil {
					return err
				}
			}
		}

		return nil
	})

	return err
}
开发者ID:rahulg,项目名称:jarvisbot,代码行数:46,代码来源:ping.go


注:本文中的github.com/tucnak/telebot.Chat类示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。