Jump to content
kalmen

how do i use P4D to receive Telegram bot message ?

Recommended Posts

Hi, 

 

1. Is there a discord discussion group link that i can join ? ( i found one , https://discord.com/channels/989230637342933042/990834240608415834 , this is a correct forum related to this topic ?)

2. i have this simple python code, in the attached file , main.py

    - i wish to use a simple memo, 2 buttons (one for start_bot another for stop_bot) and when python4delphi component will read and execute

      script from main.py and add message receive from Telegram bot into the memo1.

 

i have defined the following in delphi beside the main.py,

1. a pythonEngine and a pythonGUIInputOut component and set the relevant properties, i.e. IO to memo1. etc.

2. i have in my start_button try to executeStrings( memo1.lines) , but no result display

 

my problem is i am unable to get the updates from the python , telegram bot , message receive into the Delphi(10.3)'s Form memo1.

and i not even sure whether the bot is running. I have tested to run the python code. (in python 3.9) environment, it ran as expectedly. 

 

i am thinking to use the wrapDelphi to wrap the memo1 to the main.py by importing the memo1 component into the python script

to add messages or clearing the memo1.lines , can any one help me by giving some advise , what do think i need to do

or is wrapping the delphi memo1 component and change my python function i.e. echo() , handle_json() to change the content

of the wrapped delphi's memo1.lines in the python code ?

 

Thanks.

 

 

main.py

Edited by kalmen

Share this post


Link to post
10 hours ago, kalmen said:

not even sure whether the bot is running

you need to make sure your script is running at all, try running something a-la helloworld first

 

if does not, then the problem may be in the "wrong" python, so you would have to explicitly set the paths to python dll file in the properties of the PythonEngine

 

Alternatively you can check some example working with fast-Telega-based TelegramBot (voice-oriented messaging). Hope, this helps.

Share this post


Link to post

Thanks, Korif. The following advice, i can confirm the Python code was executed successfully  in my Delphi app as attached, now after clicking button start ,

i am expecting that the Delphi app will wait for any message received and display them in the memo1 component,

but it seems after the 1st run by clicking the start bot button, i think the telegram has stopped.

 

Not sure how to wait for the message from telegram and then capture it and display the message to Delphi desktop app. Look to get some more input for

this thanks.

p4d-feedback2.jpg

Share this post


Link to post

In my opinion your problem may also be thread synchronization

I don't know how the Telegram library is implemented in Python
 

it may well be that the output will still work, but after the bot stops and your application has time to close so you don't see the output

take a look at the DelayWrites and RawOutput properties of TPythonGUIInputOutput and try playing with this

 

also, take a look at the examples of triggering event handling in Delphi and try to use it for debugging (set breakpoint there)

See more examples in P4D Tutorials

 

Hope this helps

Share this post


Link to post

Hi, thanks for answering; sorry for the late reply because for down with covid virus & flu 😅(the virus is going to stay and long live like Delphi, since Delphi version 1.0 :classic_laugh:). I agree with what u mentioned that is related to the thread sync because when I send a telegram message, I am expecting an echo back from my Delphi app with python4delphi running, I did get back the reply, but it only happened when I terminal, and exit the Delphi app. The echo "messages" will then appear on my Phone's Telegram App.

 

And I am still learning how to use the thread & event in the examples, which I think managing in Python is hard and slow too. 

Thanks for sharing and for your kind assistance.

 

 

Share this post


Link to post

 

15 hours ago, kalmen said:

covid virus & flu 😅

get well & be healthy!

Share this post


Link to post

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×