Giter VIP home page Giter VIP logo

ltb's Introduction

The LDPL Telegram Bot Library

The LDPL Telegram Bot Library is a simple LDPL library lets you create a Telegram bot that can receive and send text messages.

It provides some new statements:

  • TG BOT INIT WITH TOKEN <TEXT or TEXT-VAR>
  • TG BOT SEND MESSAGE <TEXT or TEXT-VAR> TO <NUMBER or NUMBER-VAR> [WITH INLINE KEYBOARD <TEXT or TEX-VAR>]
  • TG BOT CREATE INLINE KEYBOARD IN <TEXT-VAR>
  • TG BOT ADD ROW TO INLINE KEYBOARD <TEXT-VAR>
  • TG BOT ADD BUTTON WITH TEXT <TEXT or TEXT-VAR> AND {URL | CALLBACK DATA} <TEXT or TEXT-VAR> TO INLINE KEYBOARD <TEXT-VAR>

And it announces you about new updates via the following sub-procedures that you must implement so you can handle them if you want:

  • ltb.onMessage
  • ltb.onJoin
  • ltb.onDeparture
  • ltb.onCallbackQuery

Installation

You can install this library by hand or using LPM.

๐Ÿ“ฆ Installing using LPM

Open a terminal and write lpm install ltb. Once downloaded, include it in your LDPL project by adding the line:

using package ltb

before the data and procedure sections of your source file. The library is ready to be used.

โœ‹๐Ÿป Installing by hand

Include the library into your LDPL project by copying the file ltb.ldpl and the folders extensions and thirdparty to your project directory and then adding the line:

include "ltb.ldpl"

before the data and procedure sections of your source file. The library is ready to be used.

โš ๏ธ Note

This library requires LDPL 4.3.

You also need libcurl with OpenSSL. For example, in Debian, Ubuntu or similar you can install it with:

$ sudo apt-get install libcurl4-openssl-dev

Using ltb

To use ltb, you must download this repo and in your LDPL program IMPORT the path to ltb.ldpl.

To start your bot, use this custom statement passing your bot's token:

TG BOT INIT WITH TOKEN <TEXT or TEXT-VAR>

This statement will block your main code execution, unless there is a problem initializing the bot (network problem, invalid token, etc.), in which case it will set ERRORCODE in 1, ERRORTEXT with the error description and return.

After the bot is successfully initialized, ltb will process each update that your bot receives and calls a sub-procedure passing information about the new update. There are four of them, and you must implement it in your code:

ltb.onMessage

# This sub-procedure is called each time a new text message arrives.
# Captions in animation, audio, document, photo, video or voice are captured too.
sub ltb.onMessage
parameters:
    messageId is number     # id of the sent message
    chatId is number        # id of the chat where is was sent
    userData is text map    # user data from sender
    messageText is text     # content of the message
procedure:
    # Your code here
end sub

ltb.onJoin

# This sub-procedure is called each time a user joins a chat.
sub ltb.onJoin
parameters:
    messageId is number     # id of the sent message announcing the user join
    chatId is number        # id of the chat the user joined
    userData is text map    # user data from the user who joined the chat
procedure:
    # Your code here
end sub

ltb.onDeparture

# This sub-procedure is called each time a user leaves a chat.
sub ltb.onDeparture
parameters:
    messageId is number     # id of the sent message announcing the user departure
    chatId is number        # id of the chat the user left
    userData is text map    # user data from the user who left the chat
procedure:
    # Your code here
end sub

ltb.onCallbackQuery

# This sub-procedure is called each time a user press a button with callback data.
sub ltb.onCallbackQuery
parameters:
    chatId is number        # id of the chat from the message of the button that was pressed
    userData is text map    # user data from the user who pressed the button
    callbackData is text    # callback data from the pressed button
    answerText is text      # store a text here to display a notification to the user
    answerAlert is number   # store 1 here to show answerText as an alert instead of a notification at the top of the chat screen
procedure:
    # Your code here
end sub

All of the three sub-procedures must be declared with all the parameters specified above, but you may leave a procedure: subsection empty if you don't want to do anything on some updates.

The userData TEXT MAP contains the following information about the user:

Key Description
id Unique identifier of the user
first_name User's first name
last_name User's last name
username User's username

Bear in mind that some of this elements may be empty (""), a message from a channel has no user information for example, and there are users without last_name or username (but they all have id and first_name).

You can send messages with this statement:

TG BOT SEND MESSAGE <TEXT or TEXT-VAR> TO <NUMBER or NUMBER-VAR> [WITH INLINE KEYBOARD <TEXT or TEX-VAR>]

It takes the message text and the chat id where you want to send it. You must use it after you INIT the bot. If there is a problem delivering the message (network problem, invalid chat id, etc.), ltb will set ERRORCODE in 1 and ERRORTEXT with the error description.

You can also optionally send the message with an inline keyboard. The TEXT you pass here must be a serialized JSON of a InlineKeyboardMarkup object, but don't worry, you can construct it with ltb statements:

TG BOT CREATE INLINE KEYBOARD IN <TEXT-VAR>

This statement stores in the variable an inline keyboard with an empty row.

TG BOT ADD ROW TO INLINE KEYBOARD <TEXT-VAR>

This statement modifies the inline keyboard appending an empty row at the end

TG BOT ADD BUTTON WITH TEXT <TEXT or TEXT-VAR> AND {URL | CALLBACK DATA} <TEXT or TEXT-VAR> TO INLINE KEYBOARD <TEXT-VAR>

This statement modifies thee inline keyboard appending a button at the end of its last row. You must specify the the text of the button's label and:

  • an URL that will be opened when an user press the button, or
  • callback data that will be send to ltb.onCallbackQuery and lets you handle and answering it.

Warning: The last two statements are only guaranteed to work with inline keyboards that you created and modified with ltb statements. That means that if you pass some InlineKeyboardMarkup that you made manually or with some other tools, the statements may modify them in such a way that the text becomes an invalid InlineKeyboardMarkup.

All ltb errors are logged to the standard error.

See the examples to learn more on how to use ltb. If you want to test them, paste your bot's token in token.ldpl.

License

ltb is released under the MIT license. It uses the JSON for Modern C++ library by Niels Lohmann, released also under the MIT license.

ltb's People

Contributors

dgarrodc avatar lartu avatar

Stargazers

 avatar  avatar  avatar

Watchers

 avatar  avatar

Forkers

lartu

ltb's Issues

Errors

When a bot is kicked from a group and it tries to read a message received from said group the library crashes with the error LTB TG error #403: "Forbidden: bot was kicked from the supergroup chat". The same when trying to send an empty message.

Is there a way these errors could be checked with ERRORCODE and ERRORTEXT instead of crashing the whole program?

Add inline keyboard support

Add the following statements:

  • TG BOT CREATE INLINE KEYBOARD IN <TEXT or TEXT-VAR>
  • TG BOT ADD ROW TO INLINE KEYBOARD <TEXT or TEXT-VAR>
  • TG BOT ADD BUTTON WITH TEXT <TEXT or TEXT-VAR> AND URL <TEXT or TEXT-VAR> TO INLINE KEYBOARD <TEXT or TEXT-VAR>
  • TG BOT ADD BUTTON WITH TEXT <TEXT or TEXT-VAR> AND CALLBACK DATA <TEXT or TEXT-VAR> TO INLINE KEYBOARD <TEXT or TEXT-VAR>
  • TG BOT SEND MESSAGE <TEXT or TEXT-VAR> TO <NUMBER or NUMBER-VAR> WITH INLINE KEYBOARD <TEXT or TEXT-VAR>

and call ltb.onCallbackQuery

Update LTB to LDPL 4.3

Default C++ Types for LDPL have changed in LDPL 4.3. Please, update this library so it's compatible again!

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.