Recent

Author Topic: Telegram bots API Wrapper  (Read 31005 times)

Renat.Su

  • Full Member
  • ***
  • Posts: 230
    • Renat.Su
Telegram bots API Wrapper
« on: November 15, 2017, 11:32:10 am »
I'm writing a class to work with telegram API as plugin to BrookFramework.
https://github.com/Al-Muhandis/brook-telegram
There is also depends from library fp-telegram that I am also actively developing now. The library functionality is still small, but it is working. In the examples folder you can find a working example of a Fast-cgi application HelloWorld. For those who have any online service or website, developed on the Brookframework then can add telegram bot TBrookAction to your website.
Please write if you find bugs, tips or want to join the development
« Last Edit: February 27, 2018, 11:20:24 am by Renat.Su »

Renat.Su

  • Full Member
  • ***
  • Posts: 230
    • Renat.Su
Re: Telegram plugin to brookframwork
« Reply #1 on: December 12, 2017, 12:56:30 pm »
fp-telegram: Added Long Polling to receive updates in addition to Webhook that I already use. Not much testing, because for me it is more convenient only webhook.
But for those who do not want to bother with writing web applications a good option is writing a desktop [or console or service] application to work with the Telegram Bot API. In this case, you can get updates with long polling (even short polling for testing). [Personally, I do not like this method, the only useful application when you do not want or can not install a webhook]
/ EXAMPLES /: Added simple ready-made examples for showing how longpolling works. A multithreaded version and example quite simple without threads (see getMe) https://github.com/Al-Muhandis/fp-telegram/tree/master/examples
brook-telegram: adapted the library to work with brookframework 3.9.9 ...
Also I hope that someone will join the project;)

Renat.Su

  • Full Member
  • ***
  • Posts: 230
    • Renat.Su
Re: Telegram bots API Wrapper
« Reply #2 on: February 27, 2018, 11:35:32 am »
Work on the library continues.
Recent changes: inline mode

Done:
   
  • webhook getting updates
  • longpolling getting udates via getUpdates API method
  • Telegram bots API methods implemented:
    • getMe
    • sendMessage
    • sendPhoto
    • sendVideo
    • getUpdates
    • answerInlineQuery
    • sendLocation
  • Update events handling
    • Message & message commands
    • Callback query
    • Inline queries
  • Full json updates logging (without handling)
  • Simple statistcs (csv-format)
Todo:
  • Extensive statistics (Quantitative data on requests and unique users)
  • Botan statistics
  • Other bots API methods
  • Other update events handling
  • exception classes
  • delphi compatibility (Not sure about the need)
  • more examples
  • Please suggest me other functionalities
I need recommendations and advice from more experienced programmers on the hierarchy of classes, adding methods for interfaces to work through other libraries, examples of implementing webhooks, for example, through built-in FCL classes for web servers (for brookframework already exist, as well as adaptation to BF4).
« Last Edit: February 27, 2018, 11:37:53 am by Renat.Su »

bonmario

  • Sr. Member
  • ****
  • Posts: 346
Re: Telegram bots API Wrapper
« Reply #3 on: June 29, 2018, 07:30:40 pm »
Hi, referring to this thread http://forum.lazarus.freepascal.org/index.php/topic,41716.0/topicseen.html i am the author of the last post in the italian forum.
What's wrong in my code?

Thanks in advance, Mario

Renat.Su

  • Full Member
  • ***
  • Posts: 230
    • Renat.Su
Re: Telegram bots API Wrapper
« Reply #4 on: June 30, 2018, 08:24:42 pm »
Hi.
So
To begin with, I will describe what the code you provided does. Performs a getMe request to the telegram server. As a result, we get some data on the bot (for example, its name). Further, in the case of a positive response, the program in the long polling mode waits for any event (getUpdates). This can be a message sent to the bot by the user or any other event that can be sent by the telegram server to the bot. The bot waits for an update depending on the TimeOut parameter in the procedure. If the update comes, then apparently You do not have processing. Just move on to the next operation. As in case it doesn't wait.
Then the bot sends the message "/start". Why and who? Instead, commands are sent by users to the bot, not the other way around. The CurrentChatID property is useful inside event processing, for example, as a response to a user's message (for example, the same command) and this property will store the ChatID In which the message was sent to the bot. It is logical that the bot will send a response in the same chat to the user.
Perhaps, if there was some update of the bot in the form of a message from the user, the program will send a message to this user, although there is a lot depends on other factors.
In General, the behavior of the algorithm is difficult to predict. So if you formulate Your task, what you want to do I would suggest you the options of using this bot

bonmario

  • Sr. Member
  • ****
  • Posts: 346
Re: Telegram bots API Wrapper
« Reply #5 on: July 01, 2018, 07:58:10 am »
Hello,
I subscribed the bot to a group.
I just want to make sure that when I send a message to the bot from my program, the same message is sent to the group.

Hi Mario

Renat.Su

  • Full Member
  • ***
  • Posts: 230
    • Renat.Su
Re: Telegram bots API Wrapper
« Reply #6 on: July 01, 2018, 08:10:15 pm »
If I understand you correctly, it's just an example with getMe. In fact, you need to send (redirect) messages to a group on behalf of the bot.
For this, for example, we could use the OnReceiveMessage event. Regardless of how we will use the getting update mode: webhook or long polling.
Code: Pascal  [Select][+][-]
  1. procedure TYourBot.BotReceiveMessage(ASender: TObject; AMessage: TTelegramMessageObj);
  2. var
  3.   AGroupChatID: Int64;
  4. begin
  5.   AGroupChatID := xxxxxxxx; // Your group chat ID
  6. { It must to include the sender's userID checks. On good to override function IsSimpleUser or simply to check if CurrentChatID = xxxx where xxxx is your userID }
  7.   if AMessage.Text<>EmptyStr then
  8.     sendMessage(AGroupChatID, AMessage.Text);
  9. end;
The rest of the code depends on which method of obtaining the update you selected.
If Webhook, then you need to teach the program how the server to receive and process the request from the telegram. As an example, you can see the implementation of the bot as part of the brook-framework (you can use other libraries or the native fcl-web)
If long polling, then you can see an example implementation of LongPollingDesktop.
I hope I missed important moments. If something does not work or it is not clear, write again
« Last Edit: July 01, 2018, 08:44:58 pm by Renat.Su »

bonmario

  • Sr. Member
  • ****
  • Posts: 346
Re: Telegram bots API Wrapper
« Reply #7 on: July 03, 2018, 04:29:40 pm »
Ok.
Thanks, Mario

bonmario

  • Sr. Member
  • ****
  • Posts: 346
Re: Telegram bots API Wrapper
« Reply #8 on: July 04, 2018, 11:45:17 am »
Hello,
I did a bit of testing, but I still could not make a working program.

Is it possible to have a source such as sending only a message to the bot?

Thanks in advance, Mario

Renat.Su

  • Full Member
  • ***
  • Posts: 230
    • Renat.Su
Re: Telegram bots API Wrapper
« Reply #9 on: July 05, 2018, 03:39:13 pm »
Hello,
I did a bit of testing, but I still could not make a working program.

Is it possible to have a source such as sending only a message to the bot?

Thanks in advance, Mario
In the previous code is sending only a message, just this is done in the onreceivemessage method. I can show a more extensive piece of code, if you explain what exactly you need and I can put in the examples folder in lib

bonmario

  • Sr. Member
  • ****
  • Posts: 346
Re: Telegram bots API Wrapper
« Reply #10 on: July 06, 2018, 07:48:36 am »
Hello, I would need only a sample program that sends a message to the bot. I tried to use the code you posted, but I probably did not understand how to use it, since it does not work.

Thanks, Mario

Renat.Su

  • Full Member
  • ***
  • Posts: 230
    • Renat.Su
Re: Telegram bots API Wrapper
« Reply #11 on: July 06, 2018, 05:49:34 pm »
Ok. I'll try to do a sample code/program the other day.

bonmario

  • Sr. Member
  • ****
  • Posts: 346
Re: Telegram bots API Wrapper
« Reply #12 on: July 06, 2018, 07:27:47 pm »
Thanks

Renat.Su

  • Full Member
  • ***
  • Posts: 230
    • Renat.Su
Re: Telegram bots API Wrapper
« Reply #13 on: July 11, 2018, 12:59:56 pm »
Added an example of the simplest console application that only sends a message from the bot. The program does not have the functionality to receive updates

Renat.Su

  • Full Member
  • ***
  • Posts: 230
    • Renat.Su
Re: Telegram bots API Wrapper
« Reply #14 on: July 11, 2018, 01:03:01 pm »
Please note, the bot cannot send a message to users who have not sent the /start command before. This is a limitation of the Telegram itself. However, a bot can send messages to a group if it is a member

 

TinyPortal © 2005-2018