本文整理汇总了Golang中github.com/tucnak/telebot.Bot类的典型用法代码示例。如果您正苦于以下问题:Golang Bot类的具体用法?Golang Bot怎么用?Golang Bot使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了Bot类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: onCommand
func onCommand(bot *telebot.Bot, command string, args []string) {
if command == "msg" && len(args) > 1 {
user := config.GetUser(args[0])
if user.UID == config.NilUser.UID {
log.Errorf("[Syscmds] Couldn't get an user with the name or UID %s", args[0])
}
msg := connect(args[1:])
bot.SendMessage(user, "*[Sysadmin]* "+msg, util.Markdown)
log.Infof("[Syscmds] Sent message %[1]s to %[2]s", msg, user.Name)
} else if command == "broadcast" && len(args) > 0 {
msg := connect(args)
for _, user := range config.GetAllUsers() {
bot.SendMessage(user, "*[Sysadmin Broadcast]* "+msg, util.Markdown)
}
log.Infof("[Syscmds] Broadcasted message %[1]s", msg)
} else if command == "config" && len(args) > 0 {
if strings.EqualFold(args[0], "save") {
if !config.IndentConfig && len(args) > 1 && strings.EqualFold(args[0], "pretty") {
config.IndentConfig = true
config.Save()
config.IndentConfig = false
} else {
config.Save()
}
} else if strings.EqualFold(args[0], "load") {
config.Load()
}
} else if command == "stop" {
Shutdown("Sysadmin")
}
}
示例2: Loop
// Loop is an infinite loop that checks for new Ranssi posts
func Loop(bot *telebot.Bot, noNotifAtInit bool) {
for {
readNow := config.GetConfig().LastReadPost + 1
node := getPost(readNow)
if node != nil {
topic := strings.TrimSpace(node.FirstChild.FirstChild.Data)
log.Infof("[Posts] New post detected: %s (ID %d)", topic, readNow)
if !noNotifAtInit {
config.GetUsersWithSettingAndRun(func(u config.User) {
bot.SendMessage(u, lang.Translatef(u, "posts.new", topic, readNow), util.Markdown)
}, subSetting)
}
config.GetConfig().LastReadPost = readNow
config.ASave()
updateNews()
time.Sleep(5 * time.Second)
continue
}
noNotifAtInit = false
time.Sleep(1 * time.Minute)
}
}
示例3: HandleCommand
// HandleCommand handles a /language command
func HandleCommand(bot *telebot.Bot, message telebot.Message, args []string) {
sender := config.GetUserWithUID(message.Sender.ID)
if len(args) > 0 {
lang := GetLanguage(args[0])
if lang == nil {
bot.SendMessage(message.Chat, Translatef(sender, "lang.notfound", lang.Name), util.Markdown)
return
}
if len(args) > 1 {
user := config.GetUser(args[1])
if user.UID != config.NilUser.UID && !sender.HasPermission(langChangeOtherPerm) {
bot.SendMessage(message.Chat, Translatef(sender, "error.noperms", langChangeOtherPerm), util.Markdown)
return
}
user.SetSetting("language", lang.Name)
bot.SendMessage(message.Chat, Translatef(sender, "lang.changed.other", lang.Name, user.Name), util.Markdown)
} else {
sender.SetSetting("language", lang.Name)
bot.SendMessage(message.Chat, Translatef(sender, "lang.changed", lang.Name), util.Markdown)
}
} else {
bot.SendMessage(message.Chat, Translatef(sender, "lang.usage"), util.Markdown)
}
}
示例4: sendSecondYear
func sendSecondYear(day int, bot *telebot.Bot, message telebot.Message) {
sender := config.GetUserWithUID(message.Sender.ID)
bot.SendMessage(message.Chat,
lang.Translatef(sender, "timetable.generic",
secondyear[day][0].Subject, secondyear[day][1].Subject, secondyear[day][2].Subject, secondyear[day][3].Subject,
util.DateToString(secondyear[day][0].Date))+"\n"+lang.Translatef(sender, "timetable.other", other[day].Subject),
util.Markdown)
}
示例5: Run
func (m *NorrisPlugin) Run(bot *telebot.Bot, config util.Config, message telebot.Message) {
if message.Text == config.CommandPrefix+"norris" {
res, _ := getJoke("http://api.icndb.com/jokes/random")
bot.SendMessage(message.Chat, res.Value.Joke, nil)
}
}
示例6: sendTo
// sends a fixer message to a single ID.
func sendTo(ID int, bot *telebot.Bot, fa *fixrdb.FixrDB) error {
var err error
rates, err := fa.GetRates(ID)
base, err := fa.GetSetting(ID, "base")
FixerData, err := fixerio.GetFixerData(base, rates)
if err != nil {
return err
}
bot.SendMessage(telebot.User{ID: ID}, FixerData.String(), nil)
return nil
}
示例7: SendPhoto
func SendPhoto(url string, message telebot.Message, bot *telebot.Bot) error {
imagefile, err := SaveImage(url)
if err != nil {
log.Println("Error fetching ")
log.Println(err)
bot.SendMessage(message.Chat, url, nil)
return err
}
defer os.Remove(imagefile)
var photo = telebot.Photo{}
photo.Thumbnail.File, err = telebot.NewFile(imagefile)
if err != nil {
log.Println("Error creating the new file ")
log.Println(err)
bot.SendMessage(message.Chat, url, nil)
return err
}
//photo.filename=imagefile
err = bot.SendPhoto(message.Chat, &photo, nil)
if err != nil {
log.Println("Error sending photo")
log.Println(err)
bot.SendMessage(message.Chat, url, nil)
return err
}
return err
}
示例8: Run
func (m *GooglePlugin) Run(bot *telebot.Bot, config util.Config, message telebot.Message) {
if strings.Contains(message.Text, config.CommandPrefix+"imgsearch") {
imgsearch := message.Text
log.Println("Searching " + imgsearch)
imgsearch = strings.Replace(imgsearch, config.CommandPrefix+"imgsearch ", "", -1)
imgsearch = strings.Replace(imgsearch, " ", "%20", -1)
util.DecodeJson("https://ajax.googleapis.com/ajax/services/search/images?v=1.0&q="+imgsearch, func(body io.ReadCloser) bool {
var data Response
err := json.NewDecoder(body).Decode(&data)
for _, i := range data.Response.Results {
util.SendPhoto(i.Url, message, bot)
log.Println("Found " + i.Title)
}
if err != nil {
return false
} else {
return true
}
})
}
if strings.Contains(message.Text, config.CommandPrefix+"search") {
websearch := message.Text
log.Println("Searching " + websearch)
websearch = strings.Replace(websearch, config.CommandPrefix+"search ", "", -1)
websearch = strings.Replace(websearch, " ", "%20", -1)
util.DecodeJson("https://ajax.googleapis.com/ajax/services/search/web?v=1.0&q="+websearch, func(body io.ReadCloser) bool {
var data Response
err := json.NewDecoder(body).Decode(&data)
for _, i := range data.Response.Results {
bot.SendMessage(message.Chat, i.Title+" - "+i.Url, nil)
log.Println("Found " + i.Title)
}
if err != nil {
return false
} else {
return true
}
})
}
}
示例9: handler_loc
// loc - search building number by name
func handler_loc(bot *telebot.Bot, msg telebot.Message, args []string) {
if len(args) == 0 {
bot.SendMessage(msg.Chat, msgfmt("loc_noarg"), nil)
return
}
data := get_data("loc")
arr := strings.Split(data, ",")
result := ""
for _, v := range arr {
if strings.Contains(v, args[0]) {
result += v + "\n"
}
}
bot.SendMessage(msg.Chat, msgfmt("loc", result), nil)
}
示例10: processHook
func processHook(ctx webhooks.Context) {
h := ctx.Hook()
branch := strings.TrimPrefix(h.Ref, `refs/heads/`)
for _, r := range cfg.Repos {
go func(r repo) {
if r.Name != h.Repo.Name ||
(r.Branch != `*` && r.Branch != branch) {
return
}
go r.Tasks.Run() //execute tasks
if r.Notify.Telegram.ChatID != 0 &&
r.Notify.Telegram.Token != `` {
var (
buf bytes.Buffer
bot *telebot.Bot
err error
)
err = tmpl.Execute(&buf, map[string]interface{}{
`hook`: h,
`branch`: branch,
})
if err != nil {
log.Println(`Template ERR:`, err)
return
}
if bot, err = telebot.NewBot(r.Notify.Telegram.Token); err != nil {
log.Println(`Telegram ERR:`, err)
return
}
err = bot.SendMessage(telebot.User{ID: r.Notify.Telegram.ChatID}, buf.String(), &sendOpts)
if err != nil {
log.Println(`Telegram ERR:`, err)
return
}
log.Println(`Message Sent`)
}
}(r)
}
}
示例11: inlineHandler
func inlineHandler(bot *telebot.Bot) {
for query := range bot.Queries {
log.Printf("Helped @%s with printer facts!", query.From.Username)
fact, err := GetPrinterFact()
if err != nil {
panic(err)
}
results := []telebot.Result{
fact,
}
// And finally respond to the query:
if err := bot.Respond(query, results); err != nil {
log.Println("ouch:", err)
}
}
}
示例12: Run
func (m *HelpPlugin) Run(bot *telebot.Bot, config util.Config, message telebot.Message) {
if message.Text == config.CommandPrefix+"help" || message.Text == config.CommandPrefix+"start" {
mk := make([]string, len(plugin_registry.Commands))
i := 0
for k, _ := range plugin_registry.Commands {
mk[i] = k
i++
}
sort.Strings(mk)
var buffer bytes.Buffer
for _, v := range mk {
buffer.WriteString(config.CommandPrefix + v + " - " + plugin_registry.Commands[v] + "\n")
}
bot.SendMessage(message.Chat,
util.RandomFromArray(quips)+", "+message.Sender.FirstName+"\n Those are my commands: \n"+buffer.String(), nil)
}
}
示例13: Send
func (img *Image) Send(bot *telebot.Bot, msg telebot.Message) (err error) {
if img == nil {
warning := "You are trying to call a method of inexistent object :-)"
bot.SendMessage(msg.Chat, warning, nil)
return errors.New(warning)
}
img.Filename = fmt.Sprint("assets/", msg.ID, img.Ext)
if img.Filename == "" {
bot.SendMessage(msg.Chat, "There's any filename associated to this query.", nil)
return errors.New("There's any filename associated to this query.")
}
i, err := telebot.NewFile(img.Filename)
if err != nil {
return err
}
caption := img.Caption[:int(math.Min(float64(len(img.Caption)), MaxCaption))]
photo := telebot.Photo{Thumbnail: telebot.Thumbnail{File: i, Width: img.Width, Height: img.Height}, Caption: caption}
err = bot.SendPhoto(msg.Chat, &photo, &telebot.SendOptions{ReplyTo: msg})
if err != nil {
return err
}
return nil
}
示例14: Run
func (m *IMDBPlugin) Run(bot *telebot.Bot, config util.Config, message telebot.Message) {
if strings.Contains(message.Text, config.CommandPrefix+"imdb") {
imdbsearch := message.Text
log.Println("Searching " + imdbsearch)
imdbsearch = strings.Replace(imdbsearch, config.CommandPrefix+"imdb ", "", -1)
imdbsearch = strings.Replace(imdbsearch, " ", "%20", -1)
util.DecodeJson("http://www.imdbapi.com/?t="+imdbsearch, func(body io.ReadCloser) bool {
var imdb Movie
err := json.NewDecoder(body).Decode(&imdb)
bot.SendMessage(message.Chat, imdb.Title+" - "+imdb.Year+"\n"+imdb.Genre+" - "+imdb.Duration+"\n"+imdb.Image+"\n"+"http://imdb.com/title/"+imdb.Id, nil)
if err != nil {
return false
} else {
return true
}
})
}
}
示例15: Run
func (m *XkcdPlugin) Run(bot *telebot.Bot, config util.Config, message telebot.Message) {
if strings.Contains(message.Text, config.CommandPrefix+"xkcd") {
xkcd := message.Text
log.Println("Searching " + xkcd)
xkcd = strings.Replace(xkcd, config.CommandPrefix+"xkcd ", "", -1)
util.DecodeJson("https://xkcd.com/"+xkcd+"/info.0.json", func(body io.ReadCloser) bool {
var data Result
err := json.NewDecoder(body).Decode(&data)
bot.SendMessage(message.Chat, strconv.Itoa(data.Id)+" - "+data.Img+" - "+data.Title, nil)
log.Println("Found " + data.Title)
if err != nil {
return false
} else {
return true
}
})
}
if message.Text == config.CommandPrefix+"xkcd" {
util.DecodeJson("https://xkcd.com/info.0.json", func(body io.ReadCloser) bool {
var data Result
err := json.NewDecoder(body).Decode(&data)
bot.SendMessage(message.Chat, strconv.Itoa(data.Id)+" - "+data.Img+" - "+data.Title, nil)
log.Println("Found " + data.Title)
if err != nil {
return false
} else {
return true
}
})
}
}