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 »

I think It's not possible.
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 »

pozvonok wrote: Fri Sep 30, 2022 9:07 am Hello. Thanks for your great work.
Can a bot use this API to communicate with the user in a dialogue?
without public chat.
I didn't find how to do it.
The user needs to contact the bot and open a private chat with 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."
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 »

How to use the @username to send message to it in a private chat?

I can't use the "@username" for ChatID get error 400, chat not found, the only way is to use the numeric ID, but that way I must find the number with another bot.

What I'm doing wrong?
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 »

When you say "private chat", do you mean a private group? If you mean a private one-to-one chat with a person, you can't send messages to such a chat using the API.
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 »

I mean an open chat with my bot.

I can send messages if on my #UserID I use the number, but I can't use the username its self (ex: @myname).

In other words... How to send messages to an user name (not number id)?

Sorry my poor english :?
Last edited by Caronte3D on Fri Oct 21, 2022 7:26 pm, edited 1 time in total.
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 »

This works:

Code: Select all

#ChatID = "123456789"
#BotAPIToken = "9999999999:AAE2ZZ1Vq6pbruVnwIxYbo3qrgE58pyaKX8"

        Define txt.s = "SOME TEXT"
        Telegram_PostText(#BotAPIToken,#ChatID,txt)      
This doesn't work:

Code: Select all

#ChatID = "@myName"
#BotAPIToken = "9999999999:AAE2ZZ1Vq6pbruVnwIxYbo3qrgE58pyaKX8"

        Define txt.s = "SOME TEXT"
        Telegram_PostText(#BotAPIToken,#ChatID,txt)  
From your Help:
"In PureTelegram, every command requires an input variable "chat_id". This can be either the chat's ID number or @username, but never its title."

But I can't use the @username, because I get every time an error:
ERROR: *{"ok":false,"error_code":400,"description":"Bad Request: chat not found"}*
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 happens if you omit the @ symbol?
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 Oct 22, 2022 5:07 am What happens if you omit the @ symbol?
I get the same error
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've just realised, only public chats can have a @username. I think you must have made a mistake - the private chat you're targeting doesn't have a @username.
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 »

I don't understand :?

I only want a way to send and receive messages from an user to the bot and viceversa, I do it already with the userID, but no with the @username.

Everything works well If I use de userID number, but in that case I must find that number using another telegram bot: @userinfobot
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, so it's not a group or channel, it's a private chat between a user and the bot. A private chat NEVER has a @username, and you can't send a message to a person by using their personal @username either. You need the numerical ID of the private chat between the user and your bot.

When the bot receives a message, analyse the message and get its chat ID and use that.

Code: Select all

Procedure.b HandleMessagePosted(*u.TG_Update)
  With *u
      If \message\text = "Hello bot."
         txt.s = "Hello user."
         Telegram_PostText(#BotAPIToken,Str(\message\chat\id),txt)
      EndIf
   EndWith
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 »

Oh! Ok, ok... I see now :D
Thanks for your help :wink:
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 »

Hi!
If I try to replace an image to a message in a group, the app closes because memory error.
I was expect a "false" value on return in case the message fails to update, so I could send a new message instead.

EDIT: I think doing that without the caption string runs ok :?

Code: Select all

If Not Telegram_ReplaceMessageImageWithPBImage(#BotAPIToken,#ChatID,lastMsgID,image,"caption")
    ultimoMsgID=Telegram_PostPBImage(#BotAPIToken,#ChatID,image,"caption")     
EndIf
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 »

Have you included UsePNGImageEncoder() and UseJPEGImageEncoder() ?
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 6:46 am Have you included UsePNGImageEncoder() and UseJPEGImageEncoder() ?
Yes!
Post Reply