Skip to content

Latest commit

 

History

History
217 lines (175 loc) · 7.22 KB

README_CN.md

File metadata and controls

217 lines (175 loc) · 7.22 KB
simbot logo

~ Simple Robot ~
Telegram 组件

release release
stars forks watchers repo size lines issues last commit copying

中文 | English

Caution

WIP.

有些想法?欢迎 留言交流加入社群~

Telegram组件 是一个 Kotlin 多平台Telegram Bot API SDK实现库, 也是 Simple Robot 标准API下实现的组件库,异步高效、Java友好!

序列化和网络请求相关分别基于 Kotlin serializationKtor.

序列化和网络请求相关分别基于 Kotlin serializationKtor.

文档

安装

参考手册的 Telegram组件 部分。

Examples

simbot core

suspend fun main() {
    val app = launchSimpleApplication { 
        useTelegram() // install Telegram Component
    }

    // subscribe to events
    app.listeners {
        // subscribe to ChatGroupMessageEvent 
        listen<ChatGroupMessageEvent> { event ->
            // process event...
            event.reply("Hello!")
            event.reply(At(event.authorId) + "Where are you?".toText())

            // Required an result
            EventResult.empty()
        }

        // subscribe to ChatGroupMessageEvent
        process<TelegramPrivateMessageEvent> { event ->
            // process event...
            event.content().send("Welcome, " + At(event.member().id))

            // Without result in `process<T>` 
        }
    }
    
    // register bots
    app.telegramBots {
        // register a bot via token
        val bot = register(token = "botaaabbb.123123444") {
            // Config...
            // The source stdlib bot config 
            botConfiguration {
                apiClientConfigurer {
                    engine {
                        // A proxy?
                        proxy = ProxyBuilder.http("http://127.0.0.1:7790")
                    }
                }

                // Enable longPolling?
                longPolling = LongPolling(
                    limit = 100,
                    timeout = 10.minutes.inWholeSeconds.toInt(),
                    allowedUpdates = setOf(UpdateValues.MESSAGE_NAME),
                    // Enable retry?
                    retry = LongPolling.Retry()
                )
            }
        }

        // start the bot
        bot.start()
    }

    
    app.join()   
}

simbot Spring Boot starter

@SpringBootApplication
@EnableSimbot // enable
class App

fun main(args: Array<String>) {
    runApplication<App>(*args)
}

@Component
class MyHandles {

    @Listener // subscribe to ChatGroupMemberIncreaseEvent
    suspend fun handleMemberIncrease(event: ChatGroupMemberIncreaseEvent) {
        // ...
    }
    
    @Filter("Hello.*")
    @Listener // subscribe to ChatGroupMessageEvent
    suspend fun handleGroupMessage(event: ChatGroupMessageEvent) {
        event.reply("Hello!")
    }
}

The configuration file *.bot.json

Comments are not supported. Remember to clean them up when you use them.

{
    "component": "simbot.telegram",
    "ticket": {
        "token": "Your FULL Bot Token, e.g. Bot123456789:aaaabbbbcccc"
    },
    // config and its properties are optional and default to `null`.
    "config": {
        "server": null,
        "proxy": null,
        "longPolling": null
    }
}
{
    "component": "simbot.telegram",
    "ticket": {
        "token": "Your FULL Bot Token, e.g. Bot123456789:aaaabbbbcccc"
    },
    // config and its properties are optional and default to `null`.
    "config": {
        "server": null,
        "proxy": null,
        // config the `longPolling` to subscribe evnets
        "longPolling": {
            "limit": 100
        }
    }
}

贡献

前往 贡献指南 了解更多!

我们欢迎并期望着您的 反馈协助, 感谢您的贡献与支持!

License

simbot-component-telegram 使用 LGPLv3 许可证开源。

This program is free software: you can redistribute it and/or modify it under the terms of the GNU Lesser General 
Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) 
any later version.

This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied 
warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more 
details.

You should have received a copy of the GNU Lesser General Public License along with this program. 
If not, see <https://www.gnu.org/licenses/>.