实现quotely,userbot

This commit is contained in:
purofle 2024-05-12 10:24:02 +08:00
parent 42a0f1ab34
commit adcead6448
13 changed files with 1343 additions and 8 deletions

8
.idea/.gitignore generated vendored Normal file
View File

@ -0,0 +1,8 @@
# 默认忽略的文件
/shelf/
/workspace.xml
# 基于编辑器的 HTTP 客户端请求
/httpRequests/
# Datasource local storage ignored files
/dataSources/
/dataSources.local.xml

8
.idea/modules.xml generated Normal file
View File

@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectModuleManager">
<modules>
<module fileurl="file://$PROJECT_DIR$/.idea/remake_bot.iml" filepath="$PROJECT_DIR$/.idea/remake_bot.iml" />
</modules>
</component>
</project>

9
.idea/remake_bot.iml generated Normal file
View File

@ -0,0 +1,9 @@
<?xml version="1.0" encoding="UTF-8"?>
<module type="WEB_MODULE" version="4">
<component name="Go" enabled="true" />
<component name="NewModuleRootManager">
<content url="file://$MODULE_DIR$" />
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
</module>

6
.idea/vcs.xml generated Normal file
View File

@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="VcsDirectoryMappings">
<mapping directory="" vcs="Git" />
</component>
</project>

8
go.mod
View File

@ -1,5 +1,9 @@
module github.com/purofle/remake_bot module github.com/purofle/remake_bot
go 1.20 go 1.21
require gopkg.in/telebot.v3 v3.1.3 toolchain go1.22.1
require gopkg.in/telebot.v3 v3.2.1
require github.com/stretchr/testify v1.9.0 // indirect

7
go.sum
View File

@ -352,8 +352,9 @@ github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.7.5/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU= github.com/stretchr/testify v1.7.5/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
github.com/stretchr/testify v1.8.0 h1:pSgiaMZlXftHpm5L7V1+rVB+AZJydKsMxsQBIJw4PKk=
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU= github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg=
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
github.com/subosito/gotenv v1.4.1/go.mod h1:ayKnFf/c6rvx/2iiLrJUk1e6plDbT3edrFNGqEflhK0= github.com/subosito/gotenv v1.4.1/go.mod h1:ayKnFf/c6rvx/2iiLrJUk1e6plDbT3edrFNGqEflhK0=
github.com/tv42/httpunix v0.0.0-20150427012821-b75d8614f926/go.mod h1:9ESjWnEqriFuLhtthL60Sar/7RFoluCcXsuvEwTV5KM= github.com/tv42/httpunix v0.0.0-20150427012821-b75d8614f926/go.mod h1:9ESjWnEqriFuLhtthL60Sar/7RFoluCcXsuvEwTV5KM=
github.com/yuin/goldmark v1.1.25/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74= github.com/yuin/goldmark v1.1.25/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
@ -828,8 +829,8 @@ gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/errgo.v2 v2.1.0/go.mod h1:hNsd1EY+bozCKY1Ytp96fpM3vjJbqLJn88ws8XvfDNI= gopkg.in/errgo.v2 v2.1.0/go.mod h1:hNsd1EY+bozCKY1Ytp96fpM3vjJbqLJn88ws8XvfDNI=
gopkg.in/ini.v1 v1.67.0/go.mod h1:pNLf8WUiyNEtQjuu5G5vTm06TEv9tsIgeAvK8hOrP4k= gopkg.in/ini.v1 v1.67.0/go.mod h1:pNLf8WUiyNEtQjuu5G5vTm06TEv9tsIgeAvK8hOrP4k=
gopkg.in/telebot.v3 v3.1.3 h1:T+CTyOWpZMqp3ALHSweNgp1awQ9nMXdRAMpe/r6x9/s= gopkg.in/telebot.v3 v3.2.1 h1:3I4LohaAyJBiivGmkfB+CiVu7QFOWkuZ4+KHgO/G3rs=
gopkg.in/telebot.v3 v3.1.3/go.mod h1:GJKwwWqp9nSkIVN51eRKU78aB5f5OnQuWdwiIZfPbko= gopkg.in/telebot.v3 v3.2.1/go.mod h1:GJKwwWqp9nSkIVN51eRKU78aB5f5OnQuWdwiIZfPbko=
gopkg.in/yaml.v2 v2.2.1/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= gopkg.in/yaml.v2 v2.2.1/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.2.3/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= gopkg.in/yaml.v2 v2.2.3/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=

View File

@ -3,10 +3,14 @@ package main
import ( import (
"encoding/json" "encoding/json"
"fmt" "fmt"
"github.com/purofle/remake_bot/quotely"
tele "gopkg.in/telebot.v3" tele "gopkg.in/telebot.v3"
"math/rand" "math/rand"
crand "math/rand"
"os" "os"
"strings"
"sync" "sync"
"time"
) )
type Country struct { type Country struct {
@ -16,11 +20,12 @@ type Country struct {
var ( var (
countryList []Country countryList []Country
userList []string
totalPopulation int64 totalPopulation int64
mutex sync.Mutex mutex sync.Mutex
) )
func initCountryList() error { func initList() error {
rawJson, err := os.ReadFile("countries.json") rawJson, err := os.ReadFile("countries.json")
if err != nil { if err != nil {
return err return err
@ -35,6 +40,13 @@ func initCountryList() error {
totalPopulation += country.Population totalPopulation += country.Population
} }
rawJson, err = os.ReadFile("user_list.json")
if err != nil {
return err
}
if err = json.Unmarshal(rawJson, &userList); err != nil {
return err
}
return nil return nil
} }
@ -91,3 +103,43 @@ func CommandRemakeData(c tele.Context) error {
return c.Reply(text) return c.Reply(text)
} }
func CommandEat(c tele.Context) error {
method := []string{"炒", "蒸", "煮"}
// 获取时间段
hour := time.Now().Hour()
var hourText string
switch {
case hour > 6 && hour <= 10:
hourText = "早上"
case hour > 10 && hour <= 14:
hourText = "中午"
case hour > 14 && hour <= 17:
hourText = "下午"
case hour > 18 && hour <= 21:
hourText = "晚上"
default:
hourText = "宵夜"
}
var name string
if strings.Contains(c.Sender().FirstName, " | ") {
name = strings.Split(c.Sender().FirstName, " | ")[0]
} else {
name = c.Sender().FirstName
}
result := fmt.Sprintf("今天%s吃 %s %s %s", hourText, name, method[rand.Intn(len(method))], userList[crand.Intn(len(userList))])
return c.Reply(result)
}
func CommandOnText(c tele.Context) error {
if c.Message().ReplyTo != nil {
text := quotely.QuoteReply(c.Bot(), c.Message())
if text != "" {
return c.Reply(text, tele.ModeMarkdownV2)
}
}
return nil
}

View File

@ -22,7 +22,7 @@ func main() {
Poller: &tele.LongPoller{Timeout: 10 * time.Second}, Poller: &tele.LongPoller{Timeout: 10 * time.Second},
} }
err := initCountryList() err := initList()
if err != nil { if err != nil {
log.Fatal(err) log.Fatal(err)
} }
@ -37,6 +37,7 @@ func main() {
b.Handle("/remake", CommandRemake) b.Handle("/remake", CommandRemake)
b.Handle("/remake_data", CommandRemakeData) b.Handle("/remake_data", CommandRemakeData)
b.Handle("/eat", CommandEat)
b.Handle(tele.OnText, CommandOnText)
b.Start() b.Start()
} }

110
quotely/quotely.go Normal file
View File

@ -0,0 +1,110 @@
package quotely
import (
"fmt"
tele "gopkg.in/telebot.v3"
"strings"
"unicode"
)
func QuoteReply(bot *tele.Bot, message *tele.Message) (replyMsg string) {
if len(message.Text) < 2 {
return
}
if !strings.HasPrefix(message.Text, "/") || (isASCII(message.Text[:2]) && !strings.HasPrefix(message.Text, "/$")) {
if !strings.HasPrefix(message.Text, "\\") || (isASCII(message.Text[:2]) && !strings.HasPrefix(message.Text, "\\$")) {
return
}
}
keywords := strings.SplitN(EscapeText(strings.Replace(message.Text, "$", "", 1)[1:]), " ", 2)
if len(keywords) == 0 {
return
}
senderName := EscapeText(message.Sender.FirstName + " " + message.Sender.LastName)
senderURI := fmt.Sprintf("tg://user?id=%d", message.Sender.ID)
replyToName := ""
replyToURI := ""
if message.SenderChat != nil {
senderName = EscapeText(message.SenderChat.Title)
senderURI = fmt.Sprintf("https://t.me/%s", message.SenderChat.Username)
}
if message.ReplyTo != nil && message.TopicMessage {
if message.ReplyTo.ID == message.ThreadID {
message.ReplyTo = nil
}
}
if message.ReplyTo != nil {
replyToName = EscapeText(message.ReplyTo.Sender.FirstName + " " + message.ReplyTo.Sender.LastName)
replyToURI = fmt.Sprintf("tg://user?id=%d", message.ReplyTo.Sender.ID)
if message.ReplyTo.Sender.IsBot && len(message.ReplyTo.Entities) != 0 {
if message.ReplyTo.Entities[0].Type == "text_mention" {
replyToName = EscapeText(message.ReplyTo.Entities[0].User.FirstName + " " + message.ReplyTo.Entities[0].User.LastName)
replyToURI = fmt.Sprintf("tg://user?id=%d", message.ReplyTo.Entities[0].User.ID)
}
}
if message.ReplyTo.SenderChat != nil {
replyToName = EscapeText(message.ReplyTo.SenderChat.Title)
replyToURI = fmt.Sprintf("https://t.me/%s", message.ReplyTo.SenderChat.Username)
}
if strings.HasPrefix(message.Text, "\\") {
senderName, replyToName = replyToName, senderName
senderURI, replyToURI = replyToURI, senderURI
}
} else {
textNoCommand := strings.TrimPrefix(strings.TrimPrefix(keywords[0], "/"), "$")
if text := strings.Split(textNoCommand, "@"); len(text) > 1 {
name := getUserByUsername(bot, text[1])
if name != "" {
keywords[0] = text[0]
replyToName = EscapeText(name)
replyToURI = fmt.Sprintf("https://t.me/%s", text[1])
}
}
if replyToName == "" {
replyToName = "自己"
replyToURI = senderURI
}
}
if len(keywords) < 2 {
return fmt.Sprintf("[%s](%s) %s了 [%s](%s)", senderName, senderURI, keywords[0], replyToName, replyToURI)
} else {
return fmt.Sprintf("[%s](%s) %s [%s](%s) %s", senderName, senderURI, keywords[0], replyToName, replyToURI, keywords[1])
}
}
func isASCII(s string) bool {
for _, r := range s {
if r > unicode.MaxASCII {
return false
}
}
return true
}
// EscapeText source: https://github.com/go-telegram-bot-api/telegram-bot-api/blob/4126fa611266940425a9dfd37e0c92ba47881718/bot.go#L729
func EscapeText(text string) string {
replacer := strings.NewReplacer(
"_", "\\_", "*", "\\*", "[", "\\[", "]", "\\]", "(",
"\\(", ")", "\\)", "~", "\\~", "`", "\\`", ">", "\\>",
"#", "\\#", "+", "\\+", "-", "\\-", "=", "\\=", "|",
"\\|", "{", "\\{", "}", "\\}", ".", "\\.", "!", "\\!",
)
return replacer.Replace(text)
}
func getUserByUsername(bot *tele.Bot, username string) string {
user, err := bot.ChatByUsername(username)
if err != nil {
return ""
}
return user.FirstName + " " + user.LastName
}

3
user_list.json Normal file
View File

@ -0,0 +1,3 @@
[
"ks. ver maimaiDX", "葱在燃烧", "CODE:002", "用户名违反相关法律法规已被屏蔽", "日落果", "Molyuu", "Kevin Williams", "C17 十七年的寒蝉 历史的维列斯", "FragilEEBoy", "豆包", "FunnyStudio", "Battra32Jerboa", "Alan Cui", "Nanami Nakano", "若璃", "xtex", "Ταπυθxι", "喵? 呜!", "Chi_Tang", "huige", "KimmyXYC", "王子风度 ^_^", "耶", "HXR_QAQ", "Krau", "Salo", "Takami", "夜泊秦淮", "RoseSchr", "雪村千絵莉", "Charm", "funnysyc", "QiXi"]

30
userbot/main.py Normal file
View File

@ -0,0 +1,30 @@
import json
from pyrogram import Client
from pyrogram.types import Message
app = Client("purofle", api_id=1946684, api_hash="7455cdbbe121e3a9f7d6a4d4399c126b")
async def main():
async with app:
member = []
async for m in app.get_chat_members("-1001965344356"):
if m.user.is_bot:
continue
member_name: str = m.user.first_name
if m.user.last_name is not None:
member_name += " " + m.user.last_name
if member_name is None:
continue
if " | " in member_name:
member_name = member_name.split(" | ")[0]
member.append(member_name)
print(json.dumps(member, ensure_ascii=False))
app.run(main())

1087
userbot/poetry.lock generated Normal file

File diff suppressed because it is too large Load Diff

16
userbot/pyproject.toml Normal file
View File

@ -0,0 +1,16 @@
[tool.poetry]
name = "userbot"
version = "0.1.0"
description = ""
authors = ["purofle <purofle@gmail.com>"]
readme = "README.md"
[tool.poetry.dependencies]
python = "^3.11"
pyrogram = "^2.0.106"
fastapi = "^0.111.0"
[build-system]
requires = ["poetry-core"]
build-backend = "poetry.core.masonry.api"