This commit is contained in:
shoopea 2019-05-03 16:00:30 +08:00
parent 488eada53b
commit b55fbeac64

121
main.go
View File

@ -1,39 +1,23 @@
package main package main
import ( import (
"crypto/md5"
"encoding/base32"
"encoding/json"
"fmt" "fmt"
"math/rand"
"regexp" "regexp"
"strconv" "strconv"
"strings" "strings"
"sync" "sync"
"time" "time"
"crypto/md5"
"encoding/base32"
"encoding/json"
"math/rand"
"github.com/Arman92/go-tdlib" "github.com/Arman92/go-tdlib"
"github.com/streadway/amqp"
// "github.com/go-web/tokenizer" // "github.com/go-web/tokenizer"
// "database/sql" // "database/sql"
// _ "github.com/go-sql-driver/mysql" // _ "github.com/go-sql-driver/mysql"
) )
type MsgDirection int
const (
Incoming MsgDirection = 1
Outgoing MsgDirection = 2
)
type ChatWarsMessage struct {
UserID64 int64 `json:"user_id"`
Direction MsgDirection `json:"direction"`
MsgText string `json:"msg"`
MsgID64 int64 `json:"msg_id"`
}
type ChatWarsWithdraw struct { type ChatWarsWithdraw struct {
Item string `json:"item"` Item string `json:"item"`
Quantity int64 `json:"quantity"` Quantity int64 `json:"quantity"`
@ -65,7 +49,6 @@ var hmacKey []byte
var withdrawals = map[string]ChatWarsWithdraw{} var withdrawals = map[string]ChatWarsWithdraw{}
var TgEncoding *base32.Encoding var TgEncoding *base32.Encoding
func main() { func main() {
// msgMutex = &sync.Mutex{} // msgMutex = &sync.Mutex{}
tdlib.SetLogVerbosityLevel(1) tdlib.SetLogVerbosityLevel(1)
@ -73,7 +56,6 @@ func main() {
TgEncoding = base32.StdEncoding.WithPadding(base32.NoPadding) TgEncoding = base32.StdEncoding.WithPadding(base32.NoPadding)
// Create new instance of client // Create new instance of client
client := tdlib.NewClient(tdlib.Config{ client := tdlib.NewClient(tdlib.Config{
APIID: "187786", APIID: "187786",
@ -150,7 +132,7 @@ func PillageKnight(c *tdlib.Client) {
// fmt.Println("UpdateNewMessage : chatID : ", chatID) // fmt.Println("UpdateNewMessage : chatID : ", chatID)
// fmt.Println("UpdateNewMessage : senderUserID : ", senderUserID) // fmt.Println("UpdateNewMessage : senderUserID : ", senderUserID)
// fmt.Println("UpdateNewMessage : viaBotUserID : ", viaBotUserID) // fmt.Println("UpdateNewMessage : viaBotUserID : ", viaBotUserID)
if (senderUserID == user_chtwrsbot && chatID == user_chtwrsbot) { if senderUserID == user_chtwrsbot && chatID == user_chtwrsbot {
return true return true
} }
return false return false
@ -241,27 +223,12 @@ func ListenCW(c *tdlib.Client) {
updateMsg := (*msg).(*tdlib.UpdateNewMessage) updateMsg := (*msg).(*tdlib.UpdateNewMessage)
chatID := updateMsg.Message.ChatID chatID := updateMsg.Message.ChatID
senderUserID := updateMsg.Message.SenderUserID senderUserID := updateMsg.Message.SenderUserID
if (senderUserID == user_chtwrsbot && chatID == user_chtwrsbot) { if senderUserID == user_chtwrsbot && chatID == user_chtwrsbot {
return true return true
} }
return false return false
} }
conn, _ := amqp.Dial("amqp://shoopea:UmDd5g4WRa2MzqOHsG2T@localhost:5672/chatwars")
defer conn.Close()
ch, _ := conn.Channel()
defer ch.Close()
q, _ := ch.QueueDeclare(
"msg", // name
false, // durable
false, // delete when unused
false, // exclusive
false, // no-wait
nil, // arguments
)
receiver := c.AddEventReceiver(&tdlib.UpdateNewMessage{}, eventFilter, 100) receiver := c.AddEventReceiver(&tdlib.UpdateNewMessage{}, eventFilter, 100)
for newMsg := range receiver.Chan { for newMsg := range receiver.Chan {
updateMsg := (newMsg).(*tdlib.UpdateNewMessage) updateMsg := (newMsg).(*tdlib.UpdateNewMessage)
@ -278,36 +245,42 @@ func ListenCW(c *tdlib.Client) {
fmt.Println(txt, "\n") fmt.Println(txt, "\n")
fmt.Println("===============================================================================================================") fmt.Println("===============================================================================================================")
m := ChatWarsMessage{
UserID64 : ownUserID64,
MsgText : string(txt),
Direction : Incoming,
MsgID64 : updateMsg.Message.ID,
}
b, err := json.Marshal(m)
_ = ch.Publish(
"", // exchange
q.Name, // routing key
false, // mandatory
false, // immediate
amqp.Publishing{
ContentType: "application/json",
Body: []byte(b),
})
fmt.Println("recv msgID64 : ", msgID64) fmt.Println("recv msgID64 : ", msgID64)
if msgID64 != int64(0) { if msgID64 != int64(0) {
switch msgText { switch msgText {
case "/g_stock_res": vault.Res = string(txt) ; msgDisplay = false; msgDelete = true case "/g_stock_res":
case "/g_stock_alch": vault.Alch = string(txt) ; msgDisplay = false; msgDelete = true vault.Res = string(txt)
case "/g_stock_misc": vault.Misc = string(txt) ; msgDisplay = false; msgDelete = true msgDisplay = false
case "/g_stock_rec": vault.Rec = string(txt) ; msgDisplay = false; msgDelete = true msgDelete = true
case "/g_stock_parts": vault.Parts = string(txt) ; msgDisplay = false; msgDelete = true case "/g_stock_alch":
case "/g_stock_other": vault.Other = string(txt) ; msgDisplay = false; msgDelete = true vault.Alch = string(txt)
case "/g_withdraw": time.Sleep(1 * time.Second) ; _ = ForwardMsg(c, updateMsg.Message.ID, updateMsg.Message.ChatID, msgUserID64) ; msgDisplay = false; msgDelete = false msgDisplay = false
default: fmt.Println("No specific task for ", msgText) ; msgDisplay = true; msgDelete = false msgDelete = true
case "/g_stock_misc":
vault.Misc = string(txt)
msgDisplay = false
msgDelete = true
case "/g_stock_rec":
vault.Rec = string(txt)
msgDisplay = false
msgDelete = true
case "/g_stock_parts":
vault.Parts = string(txt)
msgDisplay = false
msgDelete = true
case "/g_stock_other":
vault.Other = string(txt)
msgDisplay = false
msgDelete = true
case "/g_withdraw":
time.Sleep(1 * time.Second)
_ = ForwardMsg(c, updateMsg.Message.ID, updateMsg.Message.ChatID, msgUserID64)
msgDisplay = false
msgDelete = false
default:
fmt.Println("No specific task for ", msgText)
msgDisplay = true
msgDelete = false
} }
time.Sleep(1 * time.Second) time.Sleep(1 * time.Second)
if msgDelete { if msgDelete {
@ -366,7 +339,7 @@ func ListenDarkwing(c *tdlib.Client) {
updateMsg := (*msg).(*tdlib.UpdateNewMessage) updateMsg := (*msg).(*tdlib.UpdateNewMessage)
chatID := updateMsg.Message.ChatID chatID := updateMsg.Message.ChatID
senderUserID := updateMsg.Message.SenderUserID senderUserID := updateMsg.Message.SenderUserID
if (senderUserID == user_botnestbot && chatID == chat_darkwing) { if senderUserID == user_botnestbot && chatID == chat_darkwing {
return true return true
} }
return false return false
@ -408,7 +381,7 @@ func ListenGuild(c *tdlib.Client) {
updateMsg := (*msg).(*tdlib.UpdateNewMessage) updateMsg := (*msg).(*tdlib.UpdateNewMessage)
chatID := updateMsg.Message.ChatID chatID := updateMsg.Message.ChatID
// senderUserID := updateMsg.Message.SenderUserID // senderUserID := updateMsg.Message.SenderUserID
if (chatID == chat_guild) { if chatID == chat_guild {
return true return true
} }
return false return false
@ -679,7 +652,7 @@ func ListenGuild(c *tdlib.Client) {
md5String := TgEncoding.EncodeToString(md5Bytes[:]) md5String := TgEncoding.EncodeToString(md5Bytes[:])
withdrawals[md5String] = m; withdrawals[md5String] = m
msgText = fmt.Sprintf("To validate @%s withdrawal of %d x %s\nClick /withdraw_%s\n", user.Username, size, items[t.ReplaceAllString(line, "${Code}")].Name, md5String) msgText = fmt.Sprintf("To validate @%s withdrawal of %d x %s\nClick /withdraw_%s\n", user.Username, size, items[t.ReplaceAllString(line, "${Code}")].Name, md5String)
inputMsgTxt := tdlib.NewInputMessageText(tdlib.NewFormattedText(msgText, nil), true, true) inputMsgTxt := tdlib.NewInputMessageText(tdlib.NewFormattedText(msgText, nil), true, true)
@ -744,6 +717,7 @@ func ListenGuild(c *tdlib.Client) {
} }
} }
} }
/* /*
func ForwardOwnMsg(c *tdlib.Client) { func ForwardOwnMsg(c *tdlib.Client) {
eventFilter := func(msg *tdlib.TdMessage) bool { eventFilter := func(msg *tdlib.TdMessage) bool {
@ -773,7 +747,7 @@ func Listen(c *tdlib.Client) {
updateMsg := (*msg).(*tdlib.UpdateNewMessage) updateMsg := (*msg).(*tdlib.UpdateNewMessage)
chatID := updateMsg.Message.ChatID chatID := updateMsg.Message.ChatID
// senderUserID := updateMsg.Message.SenderUserID // senderUserID := updateMsg.Message.SenderUserID
if (chatID == user_chtwrsbot || chatID == user_botnestbot || chatID == user_chtwrscastlebot || chatID == chat_darkwing || chatID == chat_guild) { if chatID == user_chtwrsbot || chatID == user_botnestbot || chatID == user_chtwrscastlebot || chatID == chat_darkwing || chatID == chat_guild {
return false return false
} }
return true return true
@ -818,7 +792,7 @@ func Listen(c *tdlib.Client) {
} }
} }
func ForwardMsg(c *tdlib.Client, msgID int64, fromChatID int64, toChatID int64) (int64) { func ForwardMsg(c *tdlib.Client, msgID int64, fromChatID int64, toChatID int64) int64 {
msgIDs := make([]int64, 1) msgIDs := make([]int64, 1)
msgIDs[0] = msgID msgIDs[0] = msgID
msgs, _ := c.ForwardMessages(toChatID, fromChatID, msgIDs, false, false, false) msgs, _ := c.ForwardMessages(toChatID, fromChatID, msgIDs, false, false, false)
@ -836,8 +810,7 @@ func DeleteMsg(c *tdlib.Client, chatID int64, msgID int64) {
fmt.Println("Deleting message ", msgID) fmt.Println("Deleting message ", msgID)
} }
func OwnUserID(c *tdlib.Client) (int32) { func OwnUserID(c *tdlib.Client) int32 {
user, _ := c.GetMe() user, _ := c.GetMe()
return user.ID; return user.ID
} }