-
Notifications
You must be signed in to change notification settings - Fork 0
/
AiicyBot.go
127 lines (103 loc) · 2.95 KB
/
AiicyBot.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
//
// Copyright (c) 2016-2018 The Aiicy Team
// Licensed under The MIT License (MIT)
// See: LICENSE
//
// +build !windows
package main
import (
"fmt"
"io/ioutil"
"os"
"time"
cusSet "github.com/Aiicy/AiicyBot/pkg/setting"
"github.com/kardianos/osext"
tb "gopkg.in/tucnak/telebot.v2"
)
func GenRandomPicFromPath(path string) string {
pwd, err := osext.ExecutableFolder()
if err != nil {
log.Error(err)
}
log.Debugf("current path is %s", pwd)
log.Debugf("Pic folder is %s", path)
path = pwd + "/" + path
f, err := os.Stat(path)
if err != nil {
log.Fatalln(err)
}
if f.IsDir() != true {
log.Fatalln(path, "is not a folder")
}
dir_list, err := ioutil.ReadDir(path)
if err != nil {
log.Fatalln(err)
}
NumPic := len(dir_list)
if NumPic == 0 {
log.Fatalf("the pic folder [%s] is empty!", path)
}
PicName := dir_list[RandInt(0, NumPic)].Name()
PicName = path + "/" + PicName
log.Infof("PicName is %s", PicName)
return PicName
}
func StartBot() {
//BotName := Config.BotName
setting := tb.Settings{
Token: cusSet.TgConf.Secure.BotToken,
Poller: &tb.LongPoller{Timeout: 10 * time.Second},
}
bot, err := tb.NewBot(setting)
if err != nil {
log.Fatalln(err)
return
}
bot.Handle(tb.OnText, func(m *tb.Message) {
/*
text := fmt.Sprintf("机器人名字 @%s\n", conf.BotName)
text = text + "Git 服务器地址: [https://git.aiicy.com/](https://git.aiicy.com/)\n"
text = text + "请点击连接添加我们的机器人: [https://t.me/AiicyBot](https://t.me/AiicyBot)\n"
text = text + "获取使用方法,请输入 /help\n"
*/
ProcessMessage(bot, m)
})
bot.Handle("/hi", func(m *tb.Message) {
bot.Send(m.Chat, Tr("hello", m.Sender.FirstName))
})
bot.Handle("/ping", func(m *tb.Message) {
bot.Send(m.Chat, "pong")
})
bot.Handle("/pic", func(m *tb.Message) {
pic := GenRandomPicFromPath(cusSet.TgConf.PicFolder)
p := &tb.Photo{File: tb.FromDisk(pic)}
bot.Notify(m.Chat, tb.UploadingPhoto)
bot.Send(m.Chat, p)
})
bot.Handle("/time", func(m *tb.Message) {
PicUrl := GetBiJinPhoto()
p := &tb.Photo{File: tb.FromURL(PicUrl)}
bot.Notify(m.Chat, tb.UploadingPhoto)
bot.Send(m.Chat, p)
})
bot.Handle("/setlang", func(m *tb.Message) {
if "zh_CN" == m.Payload || "简体中文" == m.Payload {
cusSet.CurLang = "zh-CN"
} else {
cusSet.CurLang = "en-US"
}
bot.Send(m.Chat, Tr("change_lang", cusSet.TgConf.BotName))
})
bot.Handle("/help", func(m *tb.Message) {
text := fmt.Sprintf(Tr("help.title", cusSet.TgConf.BotName) + "\n")
text = text + "/hi -- " + Tr("help.hi") + " \n"
text = text + "/pic -- " + Tr("help.pic") + " \n"
text = text + fmt.Sprintf("/time -- "+Tr("help.time", cusSet.TgConf.BijinTZ)+"\n")
text = text + "/setlang -- " + Tr("help.setlang", cusSet.TgConf.BotName) + " \n"
text = text + "/ping -- " + Tr("help.ping", cusSet.TgConf.BotName) + " \n"
text = text + "/help -- " + Tr("help.help") + " \n"
bot.Notify(m.Chat, tb.Typing)
bot.Send(m.Chat, text)
})
bot.Start()
}