PureTelegram (framework for using the Telegram API)

Share your advanced PureBasic knowledge/code with the community.
User avatar
Caronte3D
Addict
Addict
Posts: 1355
Joined: Fri Jan 22, 2016 5:33 pm
Location: Some Universe

Re: PureTelegram (framework for using the Telegram API)

Post by Caronte3D »

The program stoped here (I think is because the caption is te same already on the msg):

Image
Seymour Clufley
Addict
Addict
Posts: 1264
Joined: Wed Feb 28, 2007 9:13 am
Location: London

Re: PureTelegram (framework for using the Telegram API)

Post by Seymour Clufley »

What does MemorySize(*SourceBuffer) tell you?
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: 1355
Joined: Fri Jan 22, 2016 5:33 pm
Location: Some Universe

Re: PureTelegram (framework for using the Telegram API)

Post by Caronte3D »

Seymour Clufley wrote: Thu Jan 05, 2023 3:04 pm What does MemorySize(*SourceBuffer) tell you?
Image
User avatar
Caronte3D
Addict
Addict
Posts: 1355
Joined: Fri Jan 22, 2016 5:33 pm
Location: Some Universe

Re: PureTelegram (framework for using the Telegram API)

Post by Caronte3D »

I don't know if that's ok :?
Image
Seymour Clufley
Addict
Addict
Posts: 1264
Joined: Wed Feb 28, 2007 9:13 am
Location: London

Re: PureTelegram (framework for using the Telegram API)

Post by Seymour Clufley »

I just tried this code, and it works perfectly - whether the captions are identical or not, and whether the MessageRequester() line is there or not.

Code: Select all

img1.i = CreateImage(#PB_Any,300,200,24,RGB(255,0,0))
img2.i = CreateImage(#PB_Any,300,200,24,RGB(0,255,0))
lastMsgID = Telegram_PostPBImage(#BotAPIToken,#ChatID,img1,"caption")
MessageRequester("Info","Pause before changing the image.")
If Not Telegram_ReplaceMessageImageWithPBImage(#BotAPIToken,#ChatID,lastMsgID,img2,"caption")
  ultimoMsgID = Telegram_PostPBImage(#BotAPIToken,#ChatID,img2,"caption")
EndIf
I wonder if your error is happening because of memory allocation. Try this new version of the MPFR_Create() procedure and see if the code works:

Code: Select all

Procedure.i MPFR_Create()
  reqs = ArraySize(MPFRequest())
  For q = 1 To reqs
    If Not MPFRequest(q)\active
      MPFRequest(q)\active = #True
      ProcedureReturn q
    EndIf
  Next q
  
  reqs+1
  If reqs>ArraySize(MPFRequest())
    ReDim MPFRequest(reqs)
  EndIf
  MPFRequest(reqs)\active = #True
  ProcedureReturn reqs
  
EndProcedure
If that doesn't work, try replacing the MPFR_Free() procedure with this:

Code: Select all

Procedure.b MPFR_Free(q.i)
  With MPFRequest(q)
    ForEach \field()
      If \field()\SourceBuffer<>0
        FreeMemory(\field()\SourceBuffer)
      EndIf
    Next
    ClearList(\field())
    \active = #False
  EndWith
EndProcedure
If that doesn't work, keep this new version of MPFR_Free() and uncomment the line "ProcedureReturn q" in the MPFR_Create() procedure, and try again.

Let me know if any of this works.
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: 1355
Joined: Fri Jan 22, 2016 5:33 pm
Location: Some Universe

Re: PureTelegram (framework for using the Telegram API)

Post by Caronte3D »

None of these solutions works :( I get the same error at the same line:
[10:35:14] [ERROR] Invalid memory access. (write error at address 141822908)

For your information, the error does not occurs the first time, some times at the second time, sometimes 3º, 4º... but everytime fails at some point with the same error on the same line.
Seymour Clufley
Addict
Addict
Posts: 1264
Joined: Wed Feb 28, 2007 9:13 am
Location: London

Re: PureTelegram (framework for using the Telegram API)

Post by Seymour Clufley »

Try changing the MPFR_Create() procedure to this:

Code: Select all

Procedure.i MPFR_Create()
  reqs = ArraySize(MPFRequest())
  For q = 1 To reqs
    If Not MPFRequest(q)\active
      InitializeStructure(@MPFRequest(q),MPFRequestStructure)
      MPFRequest(q)\active = #True
      ProcedureReturn q
    EndIf
  Next q
  
  reqs+1
  If reqs>ArraySize(MPFRequest())
    ReDim MPFRequest(reqs)
  EndIf
  InitializeStructure(@MPFRequest(reqs),MPFRequestStructure)
  MPFRequest(reqs)\active = #True
  ProcedureReturn reqs
  
EndProcedure
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: 1355
Joined: Fri Jan 22, 2016 5:33 pm
Location: Some Universe

Re: PureTelegram (framework for using the Telegram API)

Post by Caronte3D »

Your last code doesn´t work either, but I think I found the bug.

If I activate the Purifier y get this error:

[20:04:10] [ERROR] MultipartFormDataRequests.pbi (Linea: 171)
[20:04:10] [ERROR] Overflow in a dynamically allocated memory block.

I added the "+1" to this line:
\SourceBuffer = AllocateMemory(\DataLen+1)

Maybe to add extrasize for the null terminator?

Please check it in case I was wrong, but that solved my problem :D

Code: Select all

Procedure.b MPFR_AddJSON(q.i,field_name.s,js.i)
  AddElement(MPFRequest(q)\field())
  With MPFRequest(q)\field()
    \field_type = #MPFR_FieldType_Data
    \mime = MimeType("json")
    \field_parameter("name") = field_name
    t.s = ComposeJSON(js)
    \DataLen = StringByteLength(t,#PB_UTF8)
    \SourceBuffer = AllocateMemory(\DataLen+1)
    PokeS(\SourceBuffer,t,-1,#PB_UTF8)
  EndWith
EndProcedure
Seymour Clufley
Addict
Addict
Posts: 1264
Joined: Wed Feb 28, 2007 9:13 am
Location: London

Re: PureTelegram (framework for using the Telegram API)

Post by Seymour Clufley »

Right, thank you. Did you use the MPFR_AddJSON procedure in your code?
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: 1355
Joined: Fri Jan 22, 2016 5:33 pm
Location: Some Universe

Re: PureTelegram (framework for using the Telegram API)

Post by Caronte3D »

Seymour Clufley wrote: Sat Jan 07, 2023 8:19 am Right, thank you. Did you use the MPFR_AddJSON procedure in your code?
No, on my code I only use these five procedures:
Telegram_PostPBImage
Telegram_DeleteMessage
Telegram_EditMessageCaption
Telegram_ReplaceMessageImageWithPBImage
Telegram_PinMessage
Seymour Clufley
Addict
Addict
Posts: 1264
Joined: Wed Feb 28, 2007 9:13 am
Location: London

Re: PureTelegram (framework for using the Telegram API)

Post by Seymour Clufley »

But making this change to the JSON procedure has fixed the bug...?
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: 1355
Joined: Fri Jan 22, 2016 5:33 pm
Location: Some Universe

Re: PureTelegram (framework for using the Telegram API)

Post by Caronte3D »

Seymour Clufley wrote: Sat Jan 07, 2023 9:53 am But making this change to the JSON procedure has fixed the bug...?
Yes, 100% tested.
Some of your procedures are using it indirectly (maybe)
Seymour Clufley
Addict
Addict
Posts: 1264
Joined: Wed Feb 28, 2007 9:13 am
Location: London

Re: PureTelegram (framework for using the Telegram API)

Post by Seymour Clufley »

Oh, I see now. I use it in some of the procedures.
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: 1355
Joined: Fri Jan 22, 2016 5:33 pm
Location: Some Universe

Re: PureTelegram (framework for using the Telegram API)

Post by Caronte3D »

When you fix the bug, please edit the first post with the corrected code.
Also... the initNetwork() is deprecated now :wink:
dibor
Enthusiast
Enthusiast
Posts: 160
Joined: Wed May 20, 2020 5:19 pm
Location: The 3rd planet in the Solar System
Contact:

Re: PureTelegram (framework for using the Telegram API)

Post by dibor »

Hello.
First, tnx for the module.
Any update of this module?
I got error of initNetwork() :(
[12:29:43] [COMPILER] Line 27: InitNetwork() is not a function, array, list, map or macro.
And sure, nothing work :(

Checked with PB 6.21 x64

Thank U
Last edited by dibor on Wed Jun 25, 2025 10:38 am, edited 2 times in total.
Mac Studio M1Max, PB 6.12 Arm64 and x64.
Macbook Air M2, PB 6.12 Arm64 and x64.
Windows 10, PB 6.12 x64 and x86.
Post Reply