Skip to content

Commit

Permalink
Merge pull request #14 from nyancatda/TelegramBot
Browse files Browse the repository at this point in the history
Telegram bot部分完工
  • Loading branch information
nyancatda authored Nov 5, 2021
2 parents c3e756b + ffca383 commit 02ea98a
Show file tree
Hide file tree
Showing 26 changed files with 570 additions and 271 deletions.
39 changes: 26 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
中文 | [English](docs/README-en-US.md) | [日本語](docs/README-ja-JP.md)
# MediaWiki-Bot
MediaWiki的多语言QQ查询机器人
可以对使用MediaWiki搭建的站点进行信息查询,支持多种语言
通过聊天软件对MediaWiki进行查询信息的机器人
可以对使用聊天软件对MediaWiki搭建的站点进行信息查询,支持多种语言,跨平台兼容,支持QQ,Telegram

基于Gin和[mirai-api-http](https://github.com/project-mirai/mirai-api-http)制作

Expand All @@ -12,16 +12,19 @@ MediaWiki的多语言QQ查询机器人

## 启动
1.[Releases](https://github.com/nyancatda/MediaWiki-Bot/releases)下载最新构建
1. 在程序同级目录创建[config.yml](https://github.com/nyancatda/MediaWiki-Bot#configyml%E6%96%87%E4%BB%B6%E6%A8%A1%E6%9D%BF),并按照模板填写信息
1. 配置[mirai-api-http](https://github.com/nyancatda/MediaWiki-Bot#%E9%85%8D%E7%BD%AEmirai-api-http)
1. 在程序同级目录创建[config.yml](#configyml%E6%96%87%E4%BB%B6%E6%A8%A1%E6%9D%BF),并按照模板填写信息
1. 配置[聊天软件](#聊天软件配置)
1. 运行程序

## 配置mirai-api-http
## 聊天软件配置
*请至少配置一个聊天软件,否则机器人将无法工作*
### mirai-api-http(QQ)
1. 启用http和webhook
1. 启用enableVerify,并设置VerifyKey
1. 将webhook地址设置为http://127.0.0.1:+指定的机器人运行端口

setting.yml模板*仅供参考*
setting.yml模板
*仅供参考*
```
adapters:
- http
Expand All @@ -40,6 +43,10 @@ adapterSettings:
destinations:
- 'http://127.0.0.1:8000/'
```
### Telegram
1. 设置Telegram WebHook上报地址为机器人接收地址,具体请查看[官方文档](https://core.telegram.org/bots/api#setwebhook)
*注意,Telegram的WebHook上报地址需要`https`,这可能需要需要对机器人接收上报的地址做反向代理*
1. 如果你的服务器位于中国大陆,你还需要搭建Telegram Bot API的反向代理服务,关于如何搭建,请查看[TelegramBotAPI反向代理服务器搭建](docs/Telegram/ReverseProxyAPI.md)

## config.yml文件模板
```
Expand All @@ -49,13 +56,19 @@ Run:
#指定机器人的语言
#中文:zh-CN,英语:en-US,日语ja-JP
Language: zh-CN
QQBot:
#HttpAPI地址
APILink: http://127.0.0.1:8888
#机器人QQ号
BotQQNumber: 1000000000
#HttpAPI的VerifyKey
VerifyKey: 5eadce46qw58
SNS:
QQ:
#HttpAPI地址
APILink: http://127.0.0.1:8888
#机器人QQ号
BotQQNumber: 1000000000
#HttpAPI的VerifyKey
VerifyKey: 5eadce46qw58
Telegram:
#机器人toekn
Token: 688975899:DDFqpsdMwunUvwAsxzDTzl8z_UkYzStrewM
#TelegramAPI地址
BotAPILink: https://api.telegram.org/
#Wiki链接,支持多个,第一个为主Wiki
Wiki:
-
Expand Down
39 changes: 26 additions & 13 deletions docs/README-en-US.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[中文](https://github.com/nyancatda/MediaWiki-Bot) | English | [日本語](README-ja-JP.md)
# MediaWiki-Bot
QQ query bot on MedisWiki
Can query MediaWiki site for information, support multiple languages
通过聊天软件对MediaWiki进行查询信息的机器人
可以对使用聊天软件对MediaWiki搭建的站点进行信息查询,支持多种语言,跨平台兼容,支持QQ,Telegram

Based on Gin snd [mirai-api-http](https://github.com/project-mirai/mirai-api-http)

Expand All @@ -12,16 +12,19 @@ Based on Gin snd [mirai-api-http](https://github.com/project-mirai/mirai-api-htt

## Start
1. Download the latest [Releases](https://github.com/nyancatda/MediaWiki-Bot/releases).
1. Create [config.yml](https://github.com/nyancatda/MediaWiki-Bot#configyml%E6%96%87%E4%BB%B6%E6%A8%A1%E6%9D%BF) in the program's sibling directory. And follow the template to fill in the information.
1. [Configure the mirai-api-http](https://github.com/nyancatda/MediaWiki-Bot/blob/main/docs/README-en-US.md#%E9%85%8D%E7%BD%AEmirai-api-http)
1. Create [config.yml](#configyml-template) in the program's sibling directory. And follow the template to fill in the information.
1. 配置[聊天软件](#聊天软件配置)
1. Run the program.

## Configure the mirai-api-http
## 聊天软件配置
*请至少配置一个聊天软件,否则机器人将无法工作*
### mirai-api-http(QQ)
1. Enable the http and webhook
1. Enable the enableVerify and enter your VerifyKey
1. Fill the webhook address as: http://127.0.0.1:+port

setting.yml *The template is for reference only*
setting.yml
*The template is for reference only*
```
adapters:
- http
Expand All @@ -40,6 +43,10 @@ adapterSettings:
destinations:
- 'http://127.0.0.1:8000/'
```
### Telegram
1. 设置Telegram WebHook上报地址为机器人接收地址,具体请查看[官方文档](https://core.telegram.org/bots/api#setwebhook)
*注意,Telegram的WebHook上报地址需要`https`,这可能需要需要对机器人接收上报的地址做反向代理*
1. 如果你的服务器位于中国大陆,你还需要搭建Telegram Bot API的反向代理服务,关于如何搭建,请查看[TelegramBotAPI反向代理服务器搭建](docs/Telegram/ReverseProxyAPI.md)

## config.yml template
```
Expand All @@ -49,13 +56,19 @@ Run:
#Language
#Chinese:zh-CN,English:en-US,Japanese:ja-JP
Language: zh-CN
QQBot:
#HttpAPI address
APILink: http://127.0.0.1:8888
#The robot QQ number
BotQQNumber: 1000000000
#HttpAPI‘s VerifyKey
VerifyKey: 5eadce46qw58
SNS:
QQ:
#HttpAPI address
APILink: http://127.0.0.1:8888
#The robot QQ number
BotQQNumber: 1000000000
#HttpAPI‘s VerifyKey
VerifyKey: 5eadce46qw58
Telegram:
#机器人toekn
Token: 688975899:DDFqpsdMwunUvwAsxzDTzl8z_UkYzStrewM
#TelegramAPI地址
BotAPILink: https://api.telegram.org/
#Wiki urls. Multiple URLs can be added. The first one is the default Wiki
Wiki:
-
Expand Down
39 changes: 26 additions & 13 deletions docs/README-ja-JP.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[中文](https://github.com/nyancatda/MediaWiki-Bot) | [English](README-en-US.md) | 日本語
# MediaWiki-Bot
MediaWikiの多言語QQ検索ボット
MediaWikiを使うWebページの検索が可能
通过聊天软件对MediaWiki进行查询信息的机器人
可以对使用聊天软件对MediaWiki搭建的站点进行信息查询,支持多种语言,跨平台兼容,支持QQ,Telegram

Ginと[mirai-api-http](https://github.com/project-mirai/mirai-api-http)に基づいて作られた

Expand All @@ -12,16 +12,19 @@ Ginと[mirai-api-http](https://github.com/project-mirai/mirai-api-http)に基づ

## スタートアップ
1. [Releases](https://github.com/nyancatda/MediaWiki-Bot/releases)から最新バージョンの構築をダウンロードする
1. プログラムの同じディレクトリで[config.yml](https://github.com/nyancatda/MediaWiki-Bot#configyml%E6%96%87%E4%BB%B6%E6%A8%A1%E6%9D%BF)を作成して、それからテンプレートにしたがってメッセージを入力する
1. [mirai-api-http](https://github.com/nyancatda/MediaWiki-Bot#%E9%85%8D%E7%BD%AEmirai-api-http)を設定する
1. プログラムの同じディレクトリで[config.yml](#configyml%E3%83%95%E3%82%A1%E3%82%A4%E3%83%AB%E3%83%86%E3%83%B3%E3%83%97%E3%83%AC%E3%83%BC%E3%83%88)を作成して、それからテンプレートにしたがってメッセージを入力する
1. 配置[聊天软件](#聊天软件配置)
1. プログラムを実行する

## mirai-api-httpの設定
## 聊天软件配置
*请至少配置一个聊天软件,否则机器人将无法工作*
### mirai-api-http(QQ)
1. httpとwebhookを起動する
1. enableVerifyを起動してから、VerifyKeyを設定する
1. webhookアドレスをhttp://127.0.0.1:+指定されたボットのポート に設定する

setting.ymlテンプレート*参考だけ*
setting.ymlテンプレート
*参考だけ*
```
adapters:
- http
Expand All @@ -40,6 +43,10 @@ adapterSettings:
destinations:
- 'http://127.0.0.1:8000/'
```
### Telegram
1. 设置Telegram WebHook上报地址为机器人接收地址,具体请查看[官方文档](https://core.telegram.org/bots/api#setwebhook)
*注意,Telegram的WebHook上报地址需要`https`,这可能需要需要对机器人接收上报的地址做反向代理*
1. 如果你的服务器位于中国大陆,你还需要搭建Telegram Bot API的反向代理服务,关于如何搭建,请查看[TelegramBotAPI反向代理服务器搭建](docs/Telegram/ReverseProxyAPI.md)

## config.ymlファイルテンプレート
```
Expand All @@ -49,13 +56,19 @@ Run:
#ボットの言語を選ぶ
#中国語:zh-CN,英語:en-US,日本語ja-JP
Language: zh-CN
QQBot:
#HttpAPIアドレス
APILink: http://127.0.0.1:8888
#ボットのQQアプリ番号
BotQQNumber: 1000000000
#HttpAPIのVerifyKey
VerifyKey: 5eadce46qw58
SNS:
QQ:
#HttpAPIアドレス
APILink: http://127.0.0.1:8888
#ボットのQQアプリ番号
BotQQNumber: 1000000000
#HttpAPIのVerifyKey
VerifyKey: 5eadce46qw58
Telegram:
#机器人toekn
Token: 688975899:DDFqpsdMwunUvwAsxzDTzl8z_UkYzStrewM
#TelegramAPI地址
BotAPILink: https://api.telegram.org/
#Wikiアドレス 複数、一番目が優先のWiki
Wiki:
-
Expand Down
40 changes: 40 additions & 0 deletions docs/Telegram/ReverseProxyAPI-en-US.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
[中文](ReverseProxyAPI.md) | English | [日本語](ReverseProxyAPI-ja-JP.md)
# TelegramBotAPI反向代理服务器搭建
*本篇教程将告诉你如何使用CloudFlare Workers搭建一个自己的TelegramBotAPI反向代理服务器*
1. 前往[CloudFlare Workers](https://workers.cloudflare.com/)官网,注册一个账号,并新建一个Worker
2. 在脚本中写入以下代码
`<Bot API Token>`:你的机器人token
```
const whitelist = ["/bot<Bot API Token>"];
const tg_host = "api.telegram.org";
addEventListener('fetch', event => {
event.respondWith(handleRequest(event.request))
})
function validate(path) {
for (var i = 0; i < whitelist.length; i++) {
if (path.startsWith(whitelist[i]))
return true;
}
return false;
}
async function handleRequest(request) {
var u = new URL(request.url);
u.host = tg_host;
if (!validate(u.pathname))
return new Response('Unauthorized', {
status: 403
});
var req = new Request(u, {
method: request.method,
headers: request.headers,
body: request.body
});
const result = await fetch(req);
return result;
}
```
3. 点击保存并部署
4. 在将配置文件中的`BotAPILink`修改为这个Worker的地址即可
40 changes: 40 additions & 0 deletions docs/Telegram/ReverseProxyAPI-ja-JP.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
[中文](ReverseProxyAPI.md) | [English](ReverseProxyAPI-en-US.md) | 日本語
# TelegramBotAPI反向代理服务器搭建
*本篇教程将告诉你如何使用CloudFlare Workers搭建一个自己的TelegramBotAPI反向代理服务器*
1. 前往[CloudFlare Workers](https://workers.cloudflare.com/)官网,注册一个账号,并新建一个Worker
2. 在脚本中写入以下代码
`<Bot API Token>`:你的机器人token
```
const whitelist = ["/bot<Bot API Token>"];
const tg_host = "api.telegram.org";
addEventListener('fetch', event => {
event.respondWith(handleRequest(event.request))
})
function validate(path) {
for (var i = 0; i < whitelist.length; i++) {
if (path.startsWith(whitelist[i]))
return true;
}
return false;
}
async function handleRequest(request) {
var u = new URL(request.url);
u.host = tg_host;
if (!validate(u.pathname))
return new Response('Unauthorized', {
status: 403
});
var req = new Request(u, {
method: request.method,
headers: request.headers,
body: request.body
});
const result = await fetch(req);
return result;
}
```
3. 点击保存并部署
4. 在将配置文件中的`BotAPILink`修改为这个Worker的地址即可
40 changes: 40 additions & 0 deletions docs/Telegram/ReverseProxyAPI.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
中文 | [English](ReverseProxyAPI-en-US.md) | [日本語](ReverseProxyAPI-ja-JP.md)
# TelegramBotAPI反向代理服务器搭建
*本篇教程将告诉你如何使用CloudFlare Workers搭建一个自己的TelegramBotAPI反向代理服务器*
1. 前往[CloudFlare Workers](https://workers.cloudflare.com/)官网,注册一个账号,并新建一个Worker
2. 在脚本中写入以下代码
`<Bot API Token>`:你的机器人token
```
const whitelist = ["/bot<Bot API Token>"];
const tg_host = "api.telegram.org";
addEventListener('fetch', event => {
event.respondWith(handleRequest(event.request))
})
function validate(path) {
for (var i = 0; i < whitelist.length; i++) {
if (path.startsWith(whitelist[i]))
return true;
}
return false;
}
async function handleRequest(request) {
var u = new URL(request.url);
u.host = tg_host;
if (!validate(u.pathname))
return new Response('Unauthorized', {
status: 403
});
var req = new Request(u, {
method: request.method,
headers: request.headers,
body: request.body
});
const result = await fetch(req);
return result;
}
```
3. 点击保存并部署
4. 在将配置文件中的`BotAPILink`修改为这个Worker的地址即可
14 changes: 7 additions & 7 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,15 @@ import (

"github.com/gin-gonic/gin"
"xyz.nyan/MediaWiki-Bot/src/InformationProcessing"
"xyz.nyan/MediaWiki-Bot/src/MessageProcessingAPI/SNSAPI/QQAPI"
"xyz.nyan/MediaWiki-Bot/src/MessagePushAPI/SNSAPI/QQAPI"
"xyz.nyan/MediaWiki-Bot/src/Struct"
"xyz.nyan/MediaWiki-Bot/src/utils"
"xyz.nyan/MediaWiki-Bot/src/utils/Language"
"xyz.nyan/MediaWiki-Bot/src/utils/ReleaseFile"
)

func Error() {
fmt.Printf(Language.Message("").MainErrorTips)
fmt.Printf(Language.Message("","").MainErrorTips)
key := make([]byte, 1)
os.Stdin.Read(key)
os.Exit(1)
Expand All @@ -30,10 +30,10 @@ func CycleGetKey() {
time.Sleep(299 * time.Second)
_, resp, err := QQAPI.CreateSessionKey()
if err != nil {
fmt.Println(Language.Message("").UnableApplySession)
fmt.Println(Language.Message("","").UnableApplySession)
fmt.Println(err)
} else if resp.Status != "200 OK" {
fmt.Println(Language.Message("").UnableApplySession)
fmt.Println(Language.Message("","").UnableApplySession)
}
}
}
Expand All @@ -48,10 +48,10 @@ func main() {
//缓存mirai-api-http Session并启动定时获取进程
_, resp, err := QQAPI.CreateSessionKey()
if err != nil {
fmt.Println(Language.Message("").CannotConnectMirai)
fmt.Println(Language.Message("","").CannotConnectMirai)
Error()
} else if resp.Status != "200 OK" {
fmt.Println(Language.Message("").CannotConnectMirai)
fmt.Println(Language.Message("","").CannotConnectMirai)
Error()
}
go CycleGetKey()
Expand All @@ -60,7 +60,7 @@ func main() {
gin.SetMode(gin.ReleaseMode)
r := gin.Default()
Port := Config.Run.WebHookPort
fmt.Println(Language.StringVariable(1, Language.Message("").RunOK, Port, ""))
fmt.Println(Language.StringVariable(1, Language.Message("","").RunOK, Port, ""))
r.POST("/", func(c *gin.Context) {
var json Struct.WebHookJson
if err := c.ShouldBindJSON(&json); err != nil {
Expand Down
Loading

0 comments on commit 02ea98a

Please sign in to comment.