Simple Multi Theft Auto Lua library for Telegram Bot API
This library was tested with MTA 1.5.9. But minimum MTA version is 1.5.3. Library use longpoll to receive telegram updates. Does not even overload the server.
To install library:
- You can just donwload "telegramsam" folder in this repository
- Then put this folder to your resources
- Important: Add resource "telegramsam" to ACL and give it RPC rights: (it need to fetchRemote correctly work)
<group name="RPC">
<acl name="RPC"></acl>
<object name="resource.telegramsam"></object>
</group>
For this, you must create your bot using Bot Father and copy bot token.
Let's write simple echo bot
Important note: if you want to restart some resource that using telegramsam, so you must restart all resources that use it too. For example, i have 2 resource using telegramsam, and i want to restart 1 of them. So first i need to restart telegramsam, and then restart all of these 2 resources.
- Create your resource, for example it will called "mytelegrambot"
- Create file "server.lua"
- Add meta.xml
- Resource "telergamsam" must be running before we start our resource.
local Bot = exports["telegramsam"]:BotLogin(getResourceName(getThisResource()), "yourtokenhere"))
First we login our bot and registr resource in library. Local variable Bot will receive table. Table contain our key. By this key we will contact with library. More about BotLogin
function Bot:SendRequest(requestName, ...)
return exports["telegramsam"]:BotSendRequest(self.key, requestName, ...)
end
We create function called SendRequest in Bot table. Using this function we will contact with library and make easier to call library functions.
local function MessageHandler(message)
end
addEventHandler("onTelegramNewMessage", root, MessageHandler)
In this part, we create local function MessageHandler and add it to event handler called "onTelegramNewMessage".
local function MessageHandler(message)
Bot:SendRequest("SendMessage", message.chat.id, message.text)
end
addEventHandler("onTelegramNewMessage", root, MessageHandler)
We just add one line to MessageHandler function. This line will send user message, requesting library for send message with SendMessage args, such as chat id and text. Here we are! Echo bot created. Total code will looks like this:
local Bot = exports["telegramsam"]:BotLogin(getResourceName(getThisResource()), "yourtokenhere")
function Bot:SendRequest(requestName, ...)
return exports["telegramsam"]:BotSendRequest(self.key, requestName, ...)
end
local function MessageHandler(message)
Bot:SendRequest("SendMessage", message.chat.id, message.text)
end
addEventHandler("onTelegramNewMessage", root, MessageHandler)
Then we can start our resource, wait until "Successfully connected as ...", after that we can write something to our telegram bot. Profit!
- Work with keyboard: reply and callback
- Simple bot examples, that using Telegram Sam functions
- Simple bot example
- Add more telegram API functions
- Work with chat
- Work with channels
Made by @uw935