Compare commits
9 Commits
drone-test
...
master
Author | SHA1 | Date | |
---|---|---|---|
e346743cae | |||
5e83b1ace4 | |||
8b138cadda | |||
53061f1fd2 | |||
692bcc57ea | |||
a7e57523fd | |||
e8184e34b8 | |||
35e3d54ea3 | |||
ec74632c02 |
@ -16,4 +16,6 @@ steps:
|
|||||||
when:
|
when:
|
||||||
status:
|
status:
|
||||||
- success
|
- success
|
||||||
|
branch:
|
||||||
|
- master
|
||||||
|
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
FROM golang:alpine
|
FROM golang:1.17-alpine
|
||||||
ADD . /app
|
COPY . /app
|
||||||
WORKDIR /app
|
WORKDIR /app
|
||||||
RUN go get ; CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build
|
RUN go get ; CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build
|
||||||
CMD ./Crisp_Telegram_bot
|
CMD ["./Crisp_Telegram_bot"]
|
||||||
|
|
||||||
|
@ -12,7 +12,7 @@ Will Support:
|
|||||||
|
|
||||||
## Getting Started
|
## Getting Started
|
||||||
|
|
||||||
1. Get your crisp API credentials from [Crisp API token generator](https://go.crisp.chat/account/token/)
|
1. Get your crisp API credentials from [Crisp API token generator](https://go.crisp.chat/account/token/) FIXME
|
||||||
1. Create a bot with [BotFather](https://t.me/botfather), save the token for later use.
|
1. Create a bot with [BotFather](https://t.me/botfather), save the token for later use.
|
||||||
1. Build & Run.
|
1. Build & Run.
|
||||||
|
|
||||||
|
@ -4,7 +4,3 @@ services:
|
|||||||
build: .
|
build: .
|
||||||
volumes:
|
volumes:
|
||||||
- /root/config.yml:/app/config.yml
|
- /root/config.yml:/app/config.yml
|
||||||
depends_on:
|
|
||||||
- db
|
|
||||||
db:
|
|
||||||
image: redis
|
|
||||||
|
116
main.go
116
main.go
@ -8,15 +8,14 @@ import (
|
|||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/crisp-im/go-crisp-api/crisp"
|
"github.com/crisp-im/go-crisp-api/crisp"
|
||||||
"github.com/go-redis/redis"
|
|
||||||
tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api"
|
tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api"
|
||||||
"github.com/spf13/viper"
|
|
||||||
"github.com/l0sted/Crisp_Telegram_bot/utils"
|
"github.com/l0sted/Crisp_Telegram_bot/utils"
|
||||||
|
"github.com/spf13/viper"
|
||||||
)
|
)
|
||||||
|
|
||||||
var bot *tgbotapi.BotAPI
|
var bot *tgbotapi.BotAPI
|
||||||
var client *crisp.Client
|
var client *crisp.Client
|
||||||
var redisClient *redis.Client
|
|
||||||
var config *viper.Viper
|
var config *viper.Viper
|
||||||
|
|
||||||
// CrispMessageInfo stores the original message
|
// CrispMessageInfo stores the original message
|
||||||
@ -35,89 +34,23 @@ func (s *CrispMessageInfo) UnmarshalBinary(data []byte) error {
|
|||||||
return json.Unmarshal(data, s)
|
return json.Unmarshal(data, s)
|
||||||
}
|
}
|
||||||
|
|
||||||
func contains(s []interface{}, e int64) bool {
|
|
||||||
for _, a := range s {
|
|
||||||
if int64(a.(int)) == e {
|
|
||||||
return true
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
//from tg to crisp
|
|
||||||
func replyToUser(update *tgbotapi.Update) {
|
|
||||||
if update.Message.ReplyToMessage == nil {
|
|
||||||
msg := tgbotapi.NewMessage(update.Message.Chat.ID, "请回复一个消息")
|
|
||||||
bot.Send(msg)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
res, err := redisClient.Get(strconv.Itoa(update.Message.ReplyToMessage.MessageID)).Result()
|
|
||||||
|
|
||||||
if err != nil {
|
|
||||||
msg := tgbotapi.NewMessage(update.Message.Chat.ID, "ERROR: "+err.Error())
|
|
||||||
bot.Send(msg)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
var msgInfo CrispMessageInfo
|
|
||||||
err = json.Unmarshal([]byte(res), &msgInfo)
|
|
||||||
|
|
||||||
if err := json.Unmarshal([]byte(res), &msgInfo); err != nil {
|
|
||||||
msg := tgbotapi.NewMessage(update.Message.Chat.ID, "ERROR: "+err.Error())
|
|
||||||
bot.Send(msg)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
if update.Message.Text != "" {
|
|
||||||
client.Website.SendTextMessageInConversation(msgInfo.WebsiteID, msgInfo.SessionID, crisp.ConversationTextMessageNew{
|
|
||||||
Type: "text",
|
|
||||||
From: "operator",
|
|
||||||
Origin: "chat",
|
|
||||||
Content: update.Message.Text,
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
msg := tgbotapi.NewMessage(update.Message.Chat.ID, "回复成功!")
|
|
||||||
bot.Send(msg)
|
|
||||||
}
|
|
||||||
//from crisp to tg
|
//from crisp to tg
|
||||||
func sendMsgToAdmins(text string, WebsiteID string, SessionID string) {
|
func sendMsgToAdmins(text string, WebsiteID string, SessionID string) {
|
||||||
for _, id := range config.Get("admins").([]interface{}) {
|
for _, id := range config.Get("admins").([]interface{}) {
|
||||||
msg := tgbotapi.NewMessage(int64(id.(int)), text)
|
msg := tgbotapi.NewMessage(int64(id.(int)), text)
|
||||||
msg.ParseMode = "Markdown"
|
msg.ParseMode = "Markdown"
|
||||||
sent, _ := bot.Send(msg)
|
sent, _ := bot.Send(msg)
|
||||||
|
log.Println(strconv.Itoa(sent.MessageID))
|
||||||
redisClient.Set(strconv.Itoa(sent.MessageID), &CrispMessageInfo{
|
|
||||||
WebsiteID,
|
|
||||||
SessionID,
|
|
||||||
}, 12*time.Hour)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func init() {
|
func main() {
|
||||||
config = utils.GetConfig()
|
config = utils.GetConfig()
|
||||||
|
|
||||||
var chat_prefix = config.GetString("prefix")
|
var chat_prefix = config.GetString("prefix")
|
||||||
|
|
||||||
log.Printf("Initializing Redis...")
|
|
||||||
|
|
||||||
redisClient = redis.NewClient(&redis.Options{
|
|
||||||
Addr: config.GetString("redis.host"),
|
|
||||||
Password: config.GetString("redis.password"),
|
|
||||||
DB: config.GetInt("redis.db"),
|
|
||||||
})
|
|
||||||
|
|
||||||
var err error
|
var err error
|
||||||
|
|
||||||
_, err = redisClient.Ping().Result()
|
|
||||||
if err != nil {
|
|
||||||
log.Panic(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
log.Printf("Initializing Bot...")
|
log.Printf("Initializing Bot...")
|
||||||
|
|
||||||
bot, err = tgbotapi.NewBotAPI(config.GetString("telegram.key"))
|
bot, err = tgbotapi.NewBotAPI(config.GetString("telegram.key"))
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Panic(err)
|
log.Panic(err)
|
||||||
}
|
}
|
||||||
@ -130,7 +63,8 @@ func init() {
|
|||||||
log.Printf("Initializing Crisp Listner")
|
log.Printf("Initializing Crisp Listner")
|
||||||
client = crisp.New()
|
client = crisp.New()
|
||||||
// Set authentication parameters
|
// Set authentication parameters
|
||||||
client.Authenticate(config.GetString("crisp.identifier"), config.GetString("crisp.key"))
|
// client.Authenticate(config.GetString("crisp.identifier"), config.GetString("crisp.key"))
|
||||||
|
client.AuthenticateTier("plugin", config.GetString("crisp.identifier"), config.GetString("crisp.key"))
|
||||||
|
|
||||||
// Connect to realtime events backend and listen (only to 'message:send' namespace)
|
// Connect to realtime events backend and listen (only to 'message:send' namespace)
|
||||||
client.Events.Listen(
|
client.Events.Listen(
|
||||||
@ -146,19 +80,19 @@ func init() {
|
|||||||
|
|
||||||
// Register handler on 'message:send/text' namespace
|
// Register handler on 'message:send/text' namespace
|
||||||
reg.On("message:send/text", func(evt crisp.EventsReceiveTextMessage) {
|
reg.On("message:send/text", func(evt crisp.EventsReceiveTextMessage) {
|
||||||
text := fmt.Sprintf(`(%s) *%s(%s): *%s`, chat_prefix, *evt.User.Nickname, *evt.User.UserID, *evt.Content)
|
text := fmt.Sprintf(`%s *%s: *%s`, chat_prefix, *evt.User.Nickname, *evt.Content)
|
||||||
sendMsgToAdmins(text, *evt.WebsiteID, *evt.SessionID)
|
sendMsgToAdmins(text, *evt.WebsiteID, *evt.SessionID)
|
||||||
})
|
})
|
||||||
|
|
||||||
// Register handler on 'message:send/file' namespace
|
// Register handler on 'message:send/file' namespace
|
||||||
reg.On("message:send/file", func(evt crisp.EventsReceiveFileMessage) {
|
reg.On("message:send/file", func(evt crisp.EventsReceiveFileMessage) {
|
||||||
text := fmt.Sprintf(`(%s) *%s(%s): *[File](%s)`, chat_prefix, *evt.User.Nickname, *evt.User.UserID, evt.Content.URL)
|
text := fmt.Sprintf(`%s *%s: *[File](%s)`, chat_prefix, *evt.User.Nickname, evt.Content.URL)
|
||||||
sendMsgToAdmins(text, *evt.WebsiteID, *evt.SessionID)
|
sendMsgToAdmins(text, *evt.WebsiteID, *evt.SessionID)
|
||||||
})
|
})
|
||||||
|
|
||||||
// Register handler on 'message:send/animation' namespace
|
// Register handler on 'message:send/animation' namespace
|
||||||
reg.On("message:send/animation", func(evt crisp.EventsReceiveAnimationMessage) {
|
reg.On("message:send/animation", func(evt crisp.EventsReceiveAnimationMessage) {
|
||||||
text := fmt.Sprintf(`(%s) *%s(%s): *[Animation](%s)`, chat_prefix, *evt.User.Nickname, *evt.User.UserID, evt.Content.URL)
|
text := fmt.Sprintf(`%s *%s: *[Animation](%s)`, chat_prefix, *evt.User.Nickname, evt.Content.URL)
|
||||||
sendMsgToAdmins(text, *evt.WebsiteID, *evt.SessionID)
|
sendMsgToAdmins(text, *evt.WebsiteID, *evt.SessionID)
|
||||||
})
|
})
|
||||||
},
|
},
|
||||||
@ -171,33 +105,7 @@ func init() {
|
|||||||
log.Fatal("Crisp listener error, check your API key or internet connection?")
|
log.Fatal("Crisp listener error, check your API key or internet connection?")
|
||||||
},
|
},
|
||||||
)
|
)
|
||||||
}
|
for {
|
||||||
|
time.Sleep(1 * time.Second)
|
||||||
func main() {
|
|
||||||
var updates tgbotapi.UpdatesChannel
|
|
||||||
|
|
||||||
log.Print("Start pooling")
|
|
||||||
u := tgbotapi.NewUpdate(0)
|
|
||||||
u.Timeout = 60
|
|
||||||
|
|
||||||
updates, _ = bot.GetUpdatesChan(u)
|
|
||||||
|
|
||||||
for update := range updates {
|
|
||||||
if update.Message == nil {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
|
|
||||||
log.Printf("%s %s: %s", update.Message.From.FirstName, update.Message.From.LastName, update.Message.Text)
|
|
||||||
|
|
||||||
switch update.Message.Command() {
|
|
||||||
case "start":
|
|
||||||
msg := tgbotapi.NewMessage(update.Message.Chat.ID, "Blinkload Telegram 客服助手")
|
|
||||||
msg.ParseMode = "Markdown"
|
|
||||||
bot.Send(msg)
|
|
||||||
}
|
|
||||||
|
|
||||||
if contains(config.Get("admins").([]interface{}), int64(update.Message.From.ID)) {
|
|
||||||
// replyToUser(&update)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
1
test.html
Normal file
1
test.html
Normal file
@ -0,0 +1 @@
|
|||||||
|
<script type="text/javascript">window.$crisp=[];window.CRISP_WEBSITE_ID="8635a202-94bb-49cc-a983-d69a45ca4be1";(function(){d=document;s=d.createElement("script");s.src="https://client.crisp.chat/l.js";s.async=1;d.getElementsByTagName("head")[0].appendChild(s);})();</script>
|
@ -18,10 +18,6 @@ func GetConfig() *viper.Viper {
|
|||||||
c.SetDefault("debug", true)
|
c.SetDefault("debug", true)
|
||||||
c.SetDefault("admins", []interface{}{})
|
c.SetDefault("admins", []interface{}{})
|
||||||
|
|
||||||
c.SetDefault("redis.host", "localhost:6379")
|
|
||||||
c.SetDefault("redis.db", 0)
|
|
||||||
c.SetDefault("redis.password", "")
|
|
||||||
|
|
||||||
c.SetDefault("crisp.identifier", "")
|
c.SetDefault("crisp.identifier", "")
|
||||||
c.SetDefault("crisp.key", "")
|
c.SetDefault("crisp.key", "")
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user