lua-telegram-bot-api

Telegram Bot API 绑定和实用程序

$ opm get yangm97/lua-telegram-bot-api

Lua Telegram Bot API

此包为 Openresty 用户提供 Telegram API 绑定和实用程序,用于开发 Telegram 机器人。包含所有 Bot API 3.5 方法的绑定。支持 openresty 和纯 lua。

用法

您可以使用位置参数或发送单个主体表来调用方法,无论哪种方式,该表都将序列化为 json 并发送请求。返回时,json 响应将为您反序列化,如果请求成功,您将收到.result作为 lua 表,如果出现错误,您将收到nil和整个错误表。

示例

使用位置参数

    local api = require "telegram-bot-api.methods".init("123456789:ABCDefGhw3gUmZOq36-D_46_AMwGBsfefbcQ")
    
    local ok, err = api.sendMessage(12345678, "<b>Hello World</b>", "html")
    
    if not ok then
      print("Error while sending message: "..err.description)
    end

使用主体表(也支持 snake_case!)

    local api = require "telegram-bot-api.methods".init("123456789:ABCDefGhw3gUmZOq36-D_46_AMwGBsfefbcQ")
    
    local ok, err = api.send_message({
      chat_id = 12345678,
      text = "Hello World",
      parse_mode = "html"
      })
    
    if not ok then
      print("Error while sending message: "..err.description)
    end

自定义

您还可以设置自定义服务器作为第二个初始化参数,并调用自定义方法,如下所示

    local api = require "telegram-bot-api.methods".init("123456789:ABCDefGhw3gUmZOq36-D_46_AMwGBsfefbcQ", {server="api.pwrtelegram.xyz"})
    
    local ok, err = api.phoneLogin({phone="+3984748839"})
    
    if not ok then
      print("Error while using phoneLogin: "..err.description)
    end

结束语

参数名称和变量位置都尽量与 Telegram 文档保持一致,但您可能更喜欢使用lib/telegram-bot-api/methods.lua作为参考。库还将尝试在发出请求之前警告您缺少必需的参数,并缓存getMe()方法(很快将缓存更多内容。测试或其他)。

作者

Yan Minari

许可证

mit

依赖项

版本