本文整理汇总了Golang中github.com/tucnak/telebot.Chat.IsGroupChat方法的典型用法代码示例。如果您正苦于以下问题:Golang Chat.IsGroupChat方法的具体用法?Golang Chat.IsGroupChat怎么用?Golang Chat.IsGroupChat使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类github.com/tucnak/telebot.Chat
的用法示例。
在下文中一共展示了Chat.IsGroupChat方法的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
}
示例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
}
示例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
}
示例4: GetName
func GetName(sender telebot.Chat) string {
name := sender.FirstName + " " + sender.LastName
if sender.IsGroupChat() {
name = sender.Title
}
return name
}
示例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)
}
}
}
示例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
}