Coder Social home page Coder Social logo

uw935 / mta-telegram-sam Goto Github PK

View Code? Open in Web Editor NEW
4.0 1.0 0.0 132 KB

Telegram library for MTA Lua. Make it easier to communicate with Telegram Bot API

License: MIT License

Lua 100.00%
mtalua mtasa mtasa-lua mtasa-resource mtasa-server telegram telegram-api telegram-lua telegrambot telegrambotapi

mta-telegram-sam's Introduction


Telegram Sam v1.0.0

Simple Multi Theft Auto Lua library for Telegram Bot API


Documentation

Getting started

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:

  1. You can just donwload "telegramsam" folder in this repository
  2. Then put this folder to your resources
  3. 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>

Usage

Preparing

For this, you must create your bot using Bot Father and copy bot token.

Writing first bot

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.

  1. Create your resource, for example it will called "mytelegrambot"
  2. Create file "server.lua"
  3. Add meta.xml
  4. 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!

image

More examples

More docs

In progress:

  1. Add more telegram API functions
  2. Work with chat
  3. Work with channels

Creators

Made by @uw935

mta-telegram-sam's People

Contributors

uw935 avatar

Stargazers

 avatar  avatar  avatar  avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.