PureTelegram (framework for using the Telegram API)

Share your advanced PureBasic knowledge/code with the community.
Seymour Clufley
Addict
Addict
Posts: 1233
Joined: Wed Feb 28, 2007 9:13 am
Location: London

Re: PureTelegram (framework for using the Telegram API)

Post by Seymour Clufley »

New version, mostly bug fixes (linked in the OP).
  • All multipart form HTTP requests are now handled by the dedicated library
  • Added: PostMultiplePBImages()
  • Added: DownloadUserCurrentProfilePhoto()
  • Fixed: GetChatMembersCount()
  • Fixed: PostItemWithThumbnail()
  • Added some minor stuff that Telegram added with the v5.1 and v5.2 API updates
The big thing for me is that I finally got video thumbnails to work! I'm very glad about that. But there are some requirements you need to be aware of. The thumbnail image must be a JPEG, it must be square, and max 320 width/height, and the video file must be over 10mb in size in order for the thumbnail to have a chance of showing. (Depending on the user's Telegram settings, it might still skip the thumbnail.)
JACK WEBB: "Coding in C is like sculpting a statue using only sandpaper. You can do it, but the result wouldn't be any better. So why bother? Just use the right tools and get the job done."
User avatar
Caronte3D
Addict
Addict
Posts: 1015
Joined: Fri Jan 22, 2016 5:33 pm
Location: Some Universe

Re: PureTelegram (framework for using the Telegram API)

Post by Caronte3D »

Nice! :D

Any chance to send messages with buttons from PB?
Seymour Clufley
Addict
Addict
Posts: 1233
Joined: Wed Feb 28, 2007 9:13 am
Location: London

Re: PureTelegram (framework for using the Telegram API)

Post by Seymour Clufley »

Post messages to a Telegram channel/group from a PB interface:

Code: Select all

ww = 600
wh = 120
win = OpenWindow(#PB_Any,0,0,ww,wh,"Send message to Telegram chat",#PB_Window_ScreenCentered)
sg = StringGadget(#PB_Any,20,20,ww-40,40,"message text")
bg = ButtonGadget(#PB_Any,ww-20-100,70,100,30,"Send")
qu = 7
AddKeyboardShortcut(win,#PB_Shortcut_Escape,qu)


Repeat
  we = WaitWindowEvent()
  Select we
    Case #PB_Event_Menu
      Select EventMenu()
        Case qu
          Break
      EndSelect
    Case #PB_Event_Gadget
      Select EventGadget()
        Case bg
          txt.s = GetGadgetText(sg)
          
          #BotAPIToken = "716306084:AAGUrvMNGzddZ2CkN_MbCLBv7ra3RORJ-cF"
          chat_id.s = Str(-1001212492478)
          ;chat_id.s = "@mygroupusername"
          Telegram_PostText(#BotAPIToken,chat_id,txt)
      EndSelect
  EndSelect
ForEver
In order for this code to work, you have to:
  • create a Telegram group/channel
  • create a Telegram bot
  • add that bot as an admin to your group/channel
  • either know the ID number for your group/channel, or have its @username (either of these will do for the chat_id string variable)
Last edited by Seymour Clufley on Fri Nov 12, 2021 10:20 am, edited 1 time in total.
JACK WEBB: "Coding in C is like sculpting a statue using only sandpaper. You can do it, but the result wouldn't be any better. So why bother? Just use the right tools and get the job done."
User avatar
Caronte3D
Addict
Addict
Posts: 1015
Joined: Fri Jan 22, 2016 5:33 pm
Location: Some Universe

Re: PureTelegram (framework for using the Telegram API)

Post by Caronte3D »

I'm sorry (my poor English) but you didn't understand me, I wanted to say something like this:
Image
Buttons on the telegram message it self
Seymour Clufley
Addict
Addict
Posts: 1233
Joined: Wed Feb 28, 2007 9:13 am
Location: London

Re: PureTelegram (framework for using the Telegram API)

Post by Seymour Clufley »

Interesting. I have never looked into this before. I have researched it now, and will add it in a future update.
JACK WEBB: "Coding in C is like sculpting a statue using only sandpaper. You can do it, but the result wouldn't be any better. So why bother? Just use the right tools and get the job done."
Seymour Clufley
Addict
Addict
Posts: 1233
Joined: Wed Feb 28, 2007 9:13 am
Location: London

Re: PureTelegram (framework for using the Telegram API)

Post by Seymour Clufley »

Okay, I have gone ahead and implemented this just now. (Don't forget to download the latest version, linked in the OP.)

Telegram handles different types of buttons, but I have only implemented URL buttons. I will see about adding the other types in future if somebody needs them.

Post a message with URL buttons to a Telegram channel/group:

Code: Select all

#BotAPIToken = "716306084:AAGUrvMNGzddZ2CkN_MbCLBv7ra3RORJ-cF"
chat_id.s = Str(-1001212492478)
;chat_id.s = "@mygroupusername"

AddTelegramButton("Website","https://www.purebasic.com")
AddTelegramButton("Forum","https://www.purebasic.fr/english/index.php")
NewTelegramButtonRow()
AddTelegramButton("Blog","https://www.purebasic.fr/blog/")

txt.s = "A set of useful PureBasic-related links"
Telegram_PostText(#BotAPIToken,chat_id,txt)

Telegram_ClearButtons() ; so that the buttons aren't included with the next message you send
In order for this code to work, you have to:
  • create a Telegram group/channel
  • create a Telegram bot
  • add that bot as an admin to your group/channel
  • either know the ID number for your group/channel, or have its @username (either of these will do for the chat_id string variable)
JACK WEBB: "Coding in C is like sculpting a statue using only sandpaper. You can do it, but the result wouldn't be any better. So why bother? Just use the right tools and get the job done."
User avatar
Caronte3D
Addict
Addict
Posts: 1015
Joined: Fri Jan 22, 2016 5:33 pm
Location: Some Universe

Re: PureTelegram (framework for using the Telegram API)

Post by Caronte3D »

Your framework gets better and better :D
Thank you very much! :wink:
User avatar
Caronte3D
Addict
Addict
Posts: 1015
Joined: Fri Jan 22, 2016 5:33 pm
Location: Some Universe

Re: PureTelegram (framework for using the Telegram API)

Post by Caronte3D »

A nice addition would be to be able of create buttons on the telegram interface it self (not only in messages).

Like that (it's python code, but you you can take the idea):
https://www.youtube.com/watch?v=5qzsKXx ... himSammour
Seymour Clufley
Addict
Addict
Posts: 1233
Joined: Wed Feb 28, 2007 9:13 am
Location: London

Re: PureTelegram (framework for using the Telegram API)

Post by Seymour Clufley »

That is something that I have never used before in Telegram, so I wouldn't know how to accommodate it in code.

It could be implemented in PureTelegram in the future. Let me finish the buttons stuff first.
JACK WEBB: "Coding in C is like sculpting a statue using only sandpaper. You can do it, but the result wouldn't be any better. So why bother? Just use the right tools and get the job done."
salazar
New User
New User
Posts: 1
Joined: Tue Nov 02, 2021 11:55 am

Re: PureTelegram (framework for using the Telegram API)

Post by salazar »

Tell me how to implement an interface with buttons for Telegram_GetNewUpdates
Implement OpenWindow .. WaitWindowEvent for buttons

I apologize for the incorrectly posed question. And for his not very good English.
Thank you very much for your work on the library.
Sorry again, I didn’t mean to offend you!
Thank you
Last edited by salazar on Mon Dec 20, 2021 1:01 pm, edited 1 time in total.
Seymour Clufley
Addict
Addict
Posts: 1233
Joined: Wed Feb 28, 2007 9:13 am
Location: London

Re: PureTelegram (framework for using the Telegram API)

Post by Seymour Clufley »

salazar wrote: Sun Dec 19, 2021 8:10 pm Tell me how to implement an interface with buttons for Telegram_GetNewUpdates
Implement OpenWindow .. WaitWindowEvent for buttons
That would be a lot of work for me or anyone else. My advice would be to start small, try to make a simple program first.
JACK WEBB: "Coding in C is like sculpting a statue using only sandpaper. You can do it, but the result wouldn't be any better. So why bother? Just use the right tools and get the job done."
Seymour Clufley
Addict
Addict
Posts: 1233
Joined: Wed Feb 28, 2007 9:13 am
Location: London

Re: PureTelegram (framework for using the Telegram API)

Post by Seymour Clufley »

A few changes to fill some gaps and get up to date with Telegram's latest API changes:
  • Added: GetChat
  • Added: GetChatAdministrators
  • Added: BanChatSenderChat
  • Added: BanChatSenderChat
  • Added: CreateChatInviteLink
  • Added: EditChatInviteLink
  • Added: RevokeChatInviteLink
  • Added: ApproveChatJoinRequest
  • Added: DeclineChatJoinRequest
  • Added support for WebM video stickers
  • Added "protect_content" variable to PostText, PostItem, PostItemWithThumbnail, PostPBImage and PostOnlineImage

Demo of GetChat(). This returns a TG_ChatStructure which has many fields, giving you lots of info about the specified chat:

Code: Select all

Telegram_GetChat(#TelegramBotAPIToken,#Telegram_ChatID,@chat.TG_ChatStructure)
With chat
  Debug "Username = "+\username
  Debug "Title = "+\title
  Debug "Type = "+\type
  Debug "ID = "+\id
  Debug "First name = "+\first_name
  Debug "Last name = "+\last_name
  Debug "Description = "+\description
  Debug "HasPrivateForwards = "+StrB(\has_private_forwards)
  Debug "HasProtectedContent = "+StrB(\has_protected_content)
EndWith

Demo of GetChatAdministrators(). This returns an array of TG_ChatMemberStructure:

Code: Select all

Dim admin.TG_ChatMemberStructure(0)
Telegram_GetChatAdministrators(#TelegramBotAPIToken,#Telegram_ChatID,admin())
For a = 0 To ArraySize(admin())
  Debug "----------------"
  Debug "ADMIN #"+Str(a)
  With admin(a)\user
    Debug "Username = "+\username
    Debug "First name = "+\first_name
    Debug "Last name = "+\last_name
    Debug "ID = "+\id
    Debug "is bot = "+StrB(\is_bot)
  EndWith
  With admin(a)
    Debug "Custom title = "+\custom_title
    Debug "status = "+\status
    Debug "is_anonymous = "+StrB(\is_anonymous)
  EndWith
  Debug ""
Next a

Demo of CreateChatInviteLink(). This returns a TG_ChatInviteLinkStructure:

Code: Select all

dl = Date(2022,10,30,12,0,0)
If Telegram_CreateChatInviteLink(#TelegramBotAPIToken,#Telegram_ChatID,"invite link name",dl,5,#False,@inv.TG_ChatInviteLinkStructure)
  With inv
    Debug "invite link URL: "+\invite_link
    Debug "creator: "+\creator\username
    Debug "creates join request: "+StrB(\creates_join_request)
    Debug "is primary: "+StrB(\is_primary)
    Debug "is revoked: "+StrB(\is_revoked)
    Debug "name: "+\name
    Debug "expire date: "+FormatDate("%dd-%mm-%yyyy @ %hh:%ii",\expire_date)
    Debug "member limit: "+Str(\member_limit)
    Debug "pending_join_request_count: "+Str(\pending_join_request_count)
  EndWith
EndIf
This invite link can later be edited or revoked. When using those two functions, specify the link using the URL given in the \invite_link field.
JACK WEBB: "Coding in C is like sculpting a statue using only sandpaper. You can do it, but the result wouldn't be any better. So why bother? Just use the right tools and get the job done."
Seymour Clufley
Addict
Addict
Posts: 1233
Joined: Wed Feb 28, 2007 9:13 am
Location: London

Re: PureTelegram (framework for using the Telegram API)

Post by Seymour Clufley »

I've fixed a few bugs related to stickers.

For a sticker to be accepted, neither of its dimensions can exceed 512px, and one of them must be exactly 512px.

There are three types of sticker: Each sticker set can contain only one of these types. You cannot mix types in a set. The type is established when you first create the set, because you must supply the first file at that point.

The sticker set's name (not its title, which is a different thing) will form its URL: https://t.me/addstickers/[NAME]. As such, the name must be strictly alphanumeric, no spaces, and it must end with _by_[botname].

Demo for creating a video sticker set:

Code: Select all

XIncludeFile "PureTelegram.pbi"

#MyPersonalTelegramID = 364612045
#MyBotName = "mycleverbot"

fn.s = "E:\my sticker video.webm" ; this video must be 512kb or smaller, have duration 3 seconds or shorter, one dimension must be 512px and the other must be 512px or smaller
sticker_set_name.s = "my_video_stickers_by_"+#MyBotName
sticker_set_title.s = "My Video Stickers"
emoji.s = "😀"
Telegram_CreateNewStickerSet(#BotAPIToken,#MyPersonalTelegramID,fn,"",sticker_set_name,sticker_set_title,emoji,#False,0)

To add a sticker to a pre-existing set:

Code: Select all

Telegram_AddStickerToSet(#BotAPIToken,#MyPersonalTelegramID,fn2,"",sticker_set_name,"🤩",0)

Set the thumbnail for a sticker set. (Make sure to read the requirements for thumbnails.)

Code: Select all

#MyPersonalTelegramID = 364612045
#MyBotName = "mycleverbot"
sticker_set_name.s = "my_video_stickers_by_"+#MyBotName
fn.s = "D:\thumbnail.png"
Telegram_SetStickerSetThumb(#BotAPIToken,#MyPersonalTelegramID,sticker_set_name,fn)
The third type of sticker, TGS, is something very new to me. There doesn't seem to be any easy way to create TGS files - you either need to use Adobe After Effects (with a special plug-in) or a website that converts SVGs into the format. The format is JSON-based, and then compressed using Gzip. PB has Gzip support, but the JSON stage looks nightmarishly difficult, so I'm not going to attempt it.
JACK WEBB: "Coding in C is like sculpting a statue using only sandpaper. You can do it, but the result wouldn't be any better. So why bother? Just use the right tools and get the job done."
Seymour Clufley
Addict
Addict
Posts: 1233
Joined: Wed Feb 28, 2007 9:13 am
Location: London

Re: PureTelegram (framework for using the Telegram API)

Post by Seymour Clufley »

More sticker-related stuff...
  • Fixed: SetStickerSetThumb()
  • Fixed: UploadStickerFile()
  • Added: DownloadStickerSet()

Upload a sticker, then add it to three pre-existing sets:

Code: Select all

Telegram_UploadStickerFile(#BotAPIToken,fn,#MyPersonalTelegramID,@ret.TG_FileStructure)
; the sticker's unique ID can be found at ret\file_id. That ID can be used to refer to it at any time from now on
ext.s = GetExtensionPart(fn)
Telegram_AddStickerToSet(#BotAPIToken,#MyPersonalTelegramID,ret\file_id,ext,sticker_set_1_name,"🤩",0)
Telegram_AddStickerToSet(#BotAPIToken,#MyPersonalTelegramID,ret\file_id,ext,sticker_set_2_name,"🤩",0)
Telegram_AddStickerToSet(#BotAPIToken,#MyPersonalTelegramID,ret\file_id,ext,sticker_set_3_name,"🤩",0)

Get info about a sticker set:

Code: Select all

Procedure.b GetTelegramStickerSetInfo(api_token.s,set_name.s)
  Telegram_GetStickerSet(api_token,set_name,@stkrset.TG_StickerSetStructure)
  With stkrset
    Debug "NAME: "+\name
    Debug "TITLE: "+\title
    Debug "IS ANIMATED: "+StrB(\is_animated)
    Debug "IS VIDEO: "+StrB(\is_video)
    Debug "CONTAINS MASKS: "+StrB(\contains_masks)
    Debug "--------------------"
    
    Debug "STICKERS: "+Str(ArraySize(\stickers())+1)
    For a = 0 To ArraySize(\stickers())
      Debug "----"
      Debug "#"+Str(a)
      Debug "FILE ID: "+\stickers(a)\file_id
      Debug "DIMENSIONS: "+Str(\stickers(a)\width)+"x"+Str(\stickers(a)\height)
    Next a
    
    Debug "--------------------"
    Debug "THUMB FILE ID: *"+\thumb\file_id+"*"
  EndWith
EndProcedure

setname.s = "Guardians_of_the_Galaxy"
GetTelegramStickerSetInfo(#BotAPIToken,setname)

Download a sticker set:

Code: Select all

folder.s = "D:\29348429\"
setname.s = "Guardians_of_the_Galaxy"
Telegram_DownloadStickerSet(setname,folder)

A note about posting a sticker in a chat, using Telegram_PostItem(). This delivers the sticker as a file (that can be downloaded) rather than as a sticker (which gets displayed). Obviously, this is not what is desired. I wrote a separate PostSticker() procedure so as to isolate anything that I might be doing wrong to cause this to happen, but I couldn't find anything. It seems that this is a bug in Telegram itself.
Last edited by Seymour Clufley on Fri Feb 04, 2022 8:25 pm, edited 1 time in total.
JACK WEBB: "Coding in C is like sculpting a statue using only sandpaper. You can do it, but the result wouldn't be any better. So why bother? Just use the right tools and get the job done."
User avatar
idle
Always Here
Always Here
Posts: 5019
Joined: Fri Sep 21, 2007 5:52 am
Location: New Zealand

Re: PureTelegram (framework for using the Telegram API)

Post by idle »

just wanted to say this looks really interesting. there's a lot of potential. thanks for sharing. :mrgreen:
Post Reply