Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: sync telegram to xlog shorts #6

Merged
merged 9 commits into from
Jan 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
83 changes: 63 additions & 20 deletions README.cn.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,28 @@

目前,Nyaruko 机器人能够实现以下惊艳的功能:

### `/sync_xlog` - 将 Telegram 信息同步到 xLog Shorts 上

通过 `/sync_xlog` 命令,Nyaruko 可以将 Telegram 中的信息同步到 xLog Shorts 上。不过呢,Nyaruko 还在成长中,当前还不支持同步 POST。

详情请看 [这里](https://github.com/niracler/nyaruko-telegram-bot/pull/6), 有更完整的例子。

<img width="400" src="https://github.com/niracler/nyaruko-telegram-bot/assets/24842631/fbaac6ed-bcf0-4f25-b706-af75fafb71b0">

### `/sync_twitter` - 将 Telegram 信息同步到 Twitter

通过 `/sync_twitter` 命令,Nyaruko 可以将 Telegram 中的信息同步到 Twitter 上。让你的思绪像翅膀一样,飞跃到另一个社交圈。🕊️ 不过呢,Nyaruko 还在成长中,当前还不支持超链接以及处理 Telegram 的 `media_group`。这需要在 Cloudflare 中巧妙地缓存历史,相信不久之后,Nyaruko 将学会这项新技能!🎓
同上,通过 `/sync_twitter` 命令,Nyaruko 可以将 Telegram 中的信息同步到 Twitter 上。

详情请看 [这里](https://github.com/niracler/nyaruko-telegram-bot/pull/2), 有更完整的例子。

<div align=center>
<img width="400" src="doc/image2.png">
<img width="400" src="doc/image1.png">
<img width="400" src="https://github.com/niracler/nyaruko-telegram-bot/assets/24842631/a557d8c6-f75a-4712-9c24-cea244668acf">
<img width="400" src="https://github.com/niracler/nyaruko-telegram-bot/assets/24842631/da45b9dc-9d18-4f15-b6d1-a7ba2b10c74b">
</div>

### 所有功能列表

- `/sync_xlog` - 将 Telegram 信息同步到 xLog 上,以 Shorts 的形式。(暂不支持同步 POST)
- `/sync_twitter` - 将 Telegram 信息同步到 Twitter 上。
- `/ping` - 测试机器人是否在线。
- `/getchatid` - 获取当前对话的 ID。
Expand All @@ -37,27 +46,28 @@ Nyaruko 的能力正在不断进化中,未来将会有更多激动人心的功
2. 配置必要的环境变量,使用 `wrangler secret` 命令来设置它们。
3. 部署 Nyaruko 机器人到 Cloudflare Workers。

### 必要的环境变量

Nyaruko 需要以下环境变量的支持来发挥其作用:

设置在 wrangler.yml 中的环境变量:
### 克隆并进入项目目录

- `ALLOWED_USER_IDS`:允许使用机器人的用户 ID 列表,以逗号分隔。
- `TELEGRAM_BOT_USERNAME`:您的 Telegram 机器人密钥。

使用 `wrangler secret` 命令设置的环境变量:
```bash
git clone https://github.com/niracler/nyaruko-telegram-bot && cd nyaruko-telegram-bot
```

- `TELEGRAM_BOT_SECRET`:您的 Telegram 机器人密钥。
- `TWITTER_API_KEY`:您的 Twitter API 密钥。
- `TWITTER_API_SECRET`:您的 Twitter API 密钥密文。
- `TWITTER_ACCESS_TOKEN`:Twitter 的访问令牌。
- `TWITTER_ACCESS_TOKEN_SECRET`:Twitter 的访问令牌密文。
- `OPENAI_API_KEY`:OpenAI 的 API 密钥。
### 环境变量

[点击这里](https://developer.twitter.com/en/portal/dashboard) 来获取 Twitter 相关的 token。
Nyaruko 需要以下环境变量的支持来发挥其作用:

[点击这里](https://core.telegram.org/bots#6-botfather) 可以了解更多关于获取 Telegram Bot Token 的信息。
|变量名|是否必须|配置方式|描述|
|---|---|---|---|
|`ALLOWED_USER_IDS`|是|`wrangler.yml`|允许使用机器人的用户 ID 列表,以逗号分隔|
|`TELEGRAM_BOT_USERNAME`|否|`wrangler.yml`|您的 Telegram 机器人用户名,用于开启 ai 聊天功能|
|`TELEGRAM_BOT_SECRET`|是|`secret`|您的 Telegram 机器人密钥([详情参考](https://core.telegram.org/bots#how-do-i-create-a-bot) |
|`XLOG_TOKEN`|否|`secret`|xLog 的 token。用于开启 xlog 同步功能|
|`XLOG_CHARACTER_ID`|否|`secret`|xLog 的 characterId。|
|`TWITTER_API_KEY`|否|`secret`|您的 Twitter API 密钥([详情参考](https://developer.twitter.com/en/portal/dashboard)), 用于开启 twitter 同步功能|
|`TWITTER_API_SECRET`|否|`secret`|您的 Twitter API 密钥密文|
|`TWITTER_ACCESS_TOKEN`|否|`secret`|Twitter 的访问令牌|
|`TWITTER_ACCESS_TOKEN_SECRET`|否|`secret`|Twitter 的访问令牌密文|
|`OPENAI_API_KEY`|否|`secret`|OpenAI 的 API 密钥。用于开启 ai 聊天功能|

### 关于设置环境变量

Expand All @@ -74,6 +84,39 @@ wrangler secret put TWITTER_API_KEY

更详细的 wrangler 配置和命令说明,请查阅 [官方 wrangler 文档](https://developers.cloudflare.com/workers/wrangler/commands/) (wrangler 文档感觉要比 twitter 文档好懂得多了,起码事例会多很多~~)。

### 创建 D1 数据库

因为 media_group 的信息是通过 D1 数据库来存储的,所以需要创建一个 D1 数据库

> 注意⚠️:在奈亚子部署起来之前的多张图的消息,会因为没有在 D1 数据库中找到对应的 media_group_id 而无法同步到 xLog 上。后面会考虑弄一个同步历史消息的脚本

```bash
wrangler d1 create tg
```

然后将返回的 D1 数据库的名称填入到 `wrangler.toml` 中,将我配置文件中的 database_id 改成你的 D1 数据库 id

```toml
[[d1_databases]]
binding = "DB" # i.e. available in your Worker on env.DB
database_name = "tg"
database_id = "******"
```

创建数据库表

```bash
wrangler d1 execute tg --file=./schema.sql
```

### 部署到 cloudflare worker

```bash
wrangler deploy
```

届此,Nyaruko 机器人已经部署完成,您可以在 Telegram 上进行测试了。

## 奈亚子的小秘密

Nyaruko 的名字来源于日本轻小说《潜行吧!奈亚子》,女主角奈亚子是一位积极向上、充满活力的克苏鲁神秘生物,她的形象是根据克苏鲁神话中的尼亚拉托提普塑造的。在 Nyaruko 机器人中,它代表了智能与活力的象征,不仅助力 Niracler 处理信息,还带着点二次元的趣味,为生活添彩!🌈
Expand Down
78 changes: 64 additions & 14 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,28 @@ Welcome to the exotic Nyaruko Telegram Bot project! 🌟 This one-of-a-kind Tele

Currently, Nyaruko has the following stunning features:

### `/sync_xlog` - Synchronize Telegram messages to xLog as Shorts

With the `/sync_xlog` command, Nyaruko can synchronize messages from Telegram to xLog as Shorts. This feature is still in development and does not support POST yet. 🚧

for more details, see [here](https://github.com/niracler/nyaruko-telegram-bot/pull/6), there are more complete examples.

<img width="400" src="https://github.com/niracler/nyaruko-telegram-bot/assets/24842631/fbaac6ed-bcf0-4f25-b706-af75fafb71b0">

### `/sync_twitter` - Synchronize Telegram messages to Twitter

With the `/sync_twitter` command, Nyaruko can synchronize messages from Telegram to Twitter. Let your thoughts take flight and soar into another social media realm. 🕊️ However, Nyaruko is still growing and currently does not support hyperlinks or handle Telegram's `media_group`. Cleverly caching history in Cloudflare is needed, and it is believed that Nyaruko will soon learn this new skill! 🎓
With the `/sync_twitter` command, Nyaruko can synchronize messages from Telegram to Twitter.

for more details, see [here](https://github.com/niracler/nyaruko-telegram-bot/pull/2), there are more complete examples.

<div align=center>
<img width="400" src="doc/image2.png">
<img width="400" src="doc/image1.png">
<img width="400" src="https://github.com/niracler/nyaruko-telegram-bot/assets/24842631/a557d8c6-f75a-4712-9c24-cea244668acf">
<img width="400" src="https://github.com/niracler/nyaruko-telegram-bot/assets/24842631/da45b9dc-9d18-4f15-b6d1-a7ba2b10c74b">
</div>

### All Features list

- `/sync_xlog` - Sync msg to xLog as Shorts. (POST is not supported yet)
- `/sync_twitter` - Sync msg to Twitter.
- `/ping` - Test if the bot is online.
- `/getchatid` - Get the ID of the current chat.
Expand All @@ -39,20 +48,28 @@ To get Nyaruko up and running in your environment, please follow these steps:
2. Configure the necessary environment variables using the `wrangler secret` command to set them.
3. Deploy the Nyaruko bot to Cloudflare Workers.

### Necessary Environment Variables
### Clone and Enter the Project Directory

Nyaruko requires the following environment variables to function:
```bash
git clone https://github.com/niracler/nyaruko-telegram-bot && cd nyaruko-telegram-bot
```

- `ALLOWED_USER_IDS`: The comma-separated list of user IDs allowed to use the bot. This is set in `wrangler.yml` and does not need to be set using the `wrangler secret` command.
- `TELEGRAM_BOT_SECRET`: Your Telegram bot token.
- `TWITTER_API_KEY`: Your Twitter API key.
- `TWITTER_API_SECRET`: Your Twitter API secret.
- `TWITTER_ACCESS_TOKEN`: Twitter access token.
- `TWITTER_ACCESS_TOKEN_SECRET`: Twitter access token secret.
### Environment Variables

[Click here](https://developer.twitter.com/en/portal/dashboard) to get your Twitter-related tokens.
Nyaruko requires the following environment variables to function:

[Click here](https://core.telegram.org/bots#6-botfather) to learn more about acquiring your Telegram Bot Token.
|Variable Name|Required|Configuration Method|Description|
|---|---|---|---|
|`ALLOWED_USER_IDS`|Yes|`wrangler.yml`|List of user IDs allowed to use the bot, separated by commas|
|`TELEGRAM_BOT_USERNAME`|No|`wrangler.yml`|Your Telegram bot username, used to enable AI chat|
|`TELEGRAM_BOT_SECRET`|Yes|`secret`|Your Telegram bot secret key ([details](https://core.telegram.org/bots#how-do-i-create-a-bot))|
|`XLOG_TOKEN`|No|`secret`|xLog token. Used to enable xlog synchronization|
|`XLOG_CHARACTER_ID`|No|`secret`|xLog characterId|
|`TWITTER_API_KEY`|No|`secret`|Your Twitter API key ([details](https://developer.twitter.com/en/portal/dashboard)), used to enable Twitter synchronization|
|`TWITTER_API_SECRET`|No|`secret`|Your Twitter API secret key|
|`TWITTER_ACCESS_TOKEN`|No|`secret`|Twitter access token|
|`TWITTER_ACCESS_TOKEN_SECRET`|No|`secret`|Twitter access token secret|
|`OPENAI_API_KEY`|No|`secret`|OpenAI API key. Used to enable AI chat|

### About Setting Environment Variables

Expand All @@ -69,7 +86,40 @@ wrangler secret put TWITTER_API_KEY

For a more detailed explanation of wrangler configuration and commands, refer to the [official wrangler documentation](https://developers.cloudflare.com/workers/wrangler/commands/) (The wrangler documents are much easier to understand compared to the Twitter documentation, mainly because there are a lot more examples~~).

### Little Secret of Nyaruko
### Create D1 Database

Because Nyaruko uses D1 as its database, you need to create a D1 database and configure the corresponding environment variables. The configuration method is as follows:

```bash
wrangler d1 create tg
```

Then fill in the name of the D1 database returned into `wrangler.toml`, and change the database_id in my configuration file to your D1 database id.

> Note⚠️: Before Nyaruko is deployed, messages with multiple images will not be synchronized to xLog because the corresponding media_group_id cannot be found in the D1 database. Later, we will consider creating a script to synchronize historical messages.

```toml
[[d1_databases]]
binding = "DB" # i.e. available in your Worker on env.DB
database_name = "tg"
database_id = "******"
```

create database table

```bash
wrangler d1 execute tg --file=./schema.sql
```

### Deploy to Cloudflare Workers

After completing the above steps, you can deploy Nyaruko to Cloudflare Workers.

```bash
wrangler deploy
```

## Little Secret of Nyaruko

The name Nyaruko originates from the Japanese light novel series “Haiyore! Nyaruko-san,” where the heroine Nyaruko is an energetic and positive Cthulhu mythical creature modeled after Nyarlathotep from Lovecraftian Mythos. In the Nyaruko bot, it represents a symbol of intelligence and vitality, not only assisting Niracler in message handling but also adding a touch of two-dimensional fun to life! 🌈

Expand Down
86 changes: 86 additions & 0 deletions doc/deploy-for-xlog.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
---
score: 60
---

# 简单易懂的 TG to xLog Shorts 机器人部署方案 - v1

具体技术: cloudlfare worker + telegram bot api + xlog shorts 。总之免费就是了

(效果图)
<img width="400" src="https://github.com/niracler/nyaruko-telegram-bot/assets/24842631/fbaac6ed-bcf0-4f25-b706-af75fafb71b0">

## 事前准备

1. 安装 wrangler 并登录 (若没有 cloudflare 帐号的同学,可以趁这个机会注册,[注册链接](https://dash.cloudflare.com/sign-up),没有 nodejs 的同学也可以去安装一下,[nodejs 下载链接](https://nodejs.org/zh-cn/download/))

```bash
npm install -g wrangler
wrangler login
```

2. 找 botfather 申请一个机器人,获取 token,[申请链接](https://t.me/botfather)
(像下面这样就可以了)

<img width="549" alt="image" src="https://github.com/niracler/nyaruko-telegram-bot/assets/24842631/8348ee9e-165d-4434-95ec-73ddcdaf3f75">

3. 拿到 xLog 的 token 以及 characterId,可以参考 [辛宝 Otto 的 token 获取教程](https://blog.ijust.cc/play-xlog-02)
就是在 xSettings 的页面,点开当前的 character,在浏览器的控制台里面输入下面的代码就可以了

```javascript
JSON.parse(localStorage.getItem('connect-kit:account')).state.wallet.siwe.token
JSON.parse(localStorage.getItem('connect-kit:account')).state.wallet.characterId
```

## 开始部署

### 克隆并进入项目目录

```bash
git clone https://github.com/niracler/nyaruko-telegram-bot && cd nyaruko-telegram-bot
```

### 设置各种密钥

这三个分别是机器人的 token,xLog 的 token,xLog 的 characterId

```bash
wrangler secret put TELEGRAM_BOT_SECRET
wrangler secret put XLOG_TOKEN
wrangler secret put XLOG_CHARACTER_ID
```

### 修改 wrangler.toml

```toml
```

### 部署到 cloudflare worker

```bash
wrangler deploy
```

好了,完成了,现在可以去 tg 上测试一下了
怎么说呢,奈亚子还有其他功能的详情可以看

<https://github.com/niracler/nyaruko-telegram-bot>

## 关于技术选型为什么是长这样

肯定不是因为我只会写 typescript 啦(逃

1. cloudflare 是免费的啊,而且又不需要开代理就能访问 Telegram API。
2. crossbell 的 SDK 是 typescript 的,若用其他语言的话,就有手拼 ipfs 的上传逻辑了

## 后记 - 路漫漫其修远兮

- [ ] debug 模式
- [ ] 能用的账户列表
- [ ] README 要跟进好,加上上面的部署部分
- [ ] 后面再做的拆分项目的工作
- [ ] 教程中 clone 的版本应该是 release 版本,main 的话有点太过超前了

## 参考资料

- [cloudflare worker 文档](https://developers.cloudflare.com/workers) - cloudflare 的文档是真的好,而且还有中文版
- [技术角度折腾 xlog】更顺畅的使用体验 2 深入理解 xlog 的鉴权](https://blog.ijust.cc/play-xlog-02) - 辛宝 Otto 的 xlog 教程,写的很详细,而且还有视频教程~~
Binary file removed doc/image1.png
Binary file not shown.
Binary file removed doc/image2.png
Binary file not shown.
Loading