I'm new on making Telegram chatbot using pyTelegramBotAPI. In this case, i have multiple choices, the 'Trapezoid' and 'Simpson'. Here is the following code
#bot.message_handler(commands=['calculate'])
def welcome(message):
print(message)
markup = types.ReplyKeyboardMarkup(one_time_keyboard=True)
markup.add('Trapezoid', 'Simpson')
reply = bot.reply_to(message, "Select computation method" ,reply_markup=markup)
if(reply.text == 'Trapezoid'):
bot.register_next_step_handler(reply, trapezoid_handler)
elif(reply.text == 'Simpson'):
bot.register_next_step_handler(reply, simpson_handler)
def trapezoid_handler(message):
bot.send_message(message.id, "Trapezoid Block")
def simpson_handler(message):
bot.send_message(message.id, "Simpson Block")
Here is a picture when i run the /calculate command
Here is picture when i press the 'Trapezoid' button
As you can see, when i press the 'Trapezoid' button, the trapezoid_handler was not executed.
The goal is, when i press either 'Trapezoid' or 'Simpson' button, it later move to the following button value. Am i access the floating keyboard value correctly? How do i access the floating keyboard value?
Thank you for your respond
Here, it returns reply text (i.e "Select computation method") So you're not getting actual value when you click buttons.
Fixed Code :
#bot.message_handler(commands=['caluculate'])
def welcome(message):
print(message)
markup = types.ReplyKeyboardMarkup(one_time_keyboard=True)
markup.add('Trapezoid', 'Simpson')
reply = bot.reply_to(message, "Select computation method" ,reply_markup=markup)
bot.register_next_step_handler(reply, markup_handler)
def markup_handler(message):
if(message.text == 'Trapezoid'):
bot.send_message(message.chat.id, "Trapezoid Block")
elif(message.text == 'Simpson'):
bot.send_message(message.chat.id, "Simpson Block")
Related
When I try to scroll until the presence of particular text by using a scroll utility its toasting InvalidSelectorException. I need to scroll to a text in mobile screen I am using pytest Appium framework
class ScrollUtil:
#staticmethod
def scroll_to_text_by_android_uiautomator(text, driver):
driver.find_element(AppiumBy.ANDROID_UIAUTOMATOR,(
"new UiScrollable(new UiSelector().scrollable(true).instance(" "0)).scrollIntoView(new UiSelector().textContains(\"" + text + "\").instance(0)")).click()
def follow_and_unfollow_button_working_people_you_may_know(self):
ScrollUtil.scroll_to_text_by_android_uiautomator("People you may know",self.driver)
self.click("follow_button_text")
follow_working = self.display_result("unfollow_button_text")
while follow_working == True:
print("Working of follow button is verified in People you may know")
break
self.click("unfollow_button_text")
unfollow_working = self.display_result("follow_button_text")
while unfollow_working == True:
print("Working of unfollow button is working in People you may know")
break
Here the code which i am trying
I'm using the pylutron_caseta python package for use with Lutron devices. At this stage I'm trying to listen for button presses. I'm able to pair with the bridge and get the buttons, but I'm unable to listen for button presses. Here's my code:
import asyncio
from pylutron_caseta.smartbridge import Smartbridge
from pylutron_caseta.pairing import async_pair
from os.path import exists
bridgeIp = "192.168.1.40"
async def pair(host: str):
def _ready():
print("Press the small black button on the back of the bridge.")
data = await async_pair(host, _ready)
with open("caseta-bridge.crt", "w") as cacert:
cacert.write(data["ca"])
with open("caseta.crt", "w") as cert:
cert.write(data["cert"])
with open("caseta.key", "w") as key:
key.write(data["key"])
print(f"Successfully paired with {data['version']}")
async def registerButton(bridge,button_id):
print("Press the small button on the button device.")
def printThis(x):
print(x)
async def connect():
bridge = Smartbridge.create_tls(bridgeIp, "caseta.key", "caseta.crt", "caseta-bridge.crt")
await bridge.connect()
buttons = bridge.get_buttons()
print (buttons)
for b in buttons:
print (b)
loopListen = asyncio.get_event_loop()
asyncio.ensure_future(listen(bridge,int(b)))
loopListen.run_forever()
async def listen(bridge,_buttonID):
while True:
bridge.add_button_subscriber(str(_buttonID), printThis)
#Program
if exists("caseta-bridge.crt"):
print("found pair files")
loop = asyncio.get_event_loop()
loop.run_until_complete(connect())
else:
loop = asyncio.get_event_loop()
loop.run_until_complete(pair(bridgeIp))
loop = asyncio.get_event_loop()
loop.run_until_complete(connect())
I expect that when I press a button on the Lutron Pico remote that I get some sort of response printed. I get nothing printed. I'm guessing I have the "listen" function incorrect, just not sure how.
Any help is appreciated!
I was able to make my Lutron Pico remotes work with a node js module called lutronpro found here: https://www.npmjs.com/package/lutronpro.
I had to upgrade to the pro lutron controller module to make it work. I would prefer to do this in Python, but at least I could get this to work, and know when a remote button was pressed and from there I could call a python web service to do the rest. I ultimately just made it start up this program on boot on my raspberry pi, and now I use my pico remote to control almost anything in the hous
I have 2 radio buttons. The first one, radio1, is connected to a function func() and inside this function connects a push button, pushButton, to another function print_me().
This is the code stripped down:
radio = self.dockwidget.radioButton
radio.clicked.connect(func)
def func():
# Connect pushButton to "print_me" function
connect = self.dockwidget.pushButton
connect.clicked.connect(print_me)
def print_me():
print 'Connected'
When the user clicks radio1 and then pushButton, a message is printed. Problem is if the user clicks radio 10 times and then pushButton, the message is also printed 10 times. Or if the user clicks radio1, then radio2, and back to radio1, it will still print the message twice.
Is there a way to prevent this so that it will only print the message once when either radio button is clicked?
Have a global variable and set it to either True or False. Then add an if statement to your code (In this example the global variable name is clicked_radio_button):
radio = self.dockwidget.radioButton
radio.clicked.connect(func)
clicked_radio_button = False #Set global variable
def func():
if clicked_radio_button == False:
# Connect pushButton to "print_me" function
connect = self.dockwidget.pushButton
connect.clicked.connect(print_me)
clicked_radio_button = True #Set global variable to True
else:
pass #Do nothing if already pressed
def print_me():
print 'Connected'
So bassicaly the user will press the picture_pin buton first, then this calls the picture_taking() function which then it should stop and wait for either (Accept_Pin or Decline_Pin) buttons to be pressed, it should not let the user to continue unless a selection is made. so when the user makes his/her selection then it go back and wait for the picture_pin button. at this stage the Accept_Pin and Decline_Pin should have no affect at all.
I have a python program that waits for a button press from the user then it runs a function to do its thing. what I would like to accomplish is, in that function I would like to also wait for another button press.
(Main.py)
----etc
## PICTURE FUNCTION ##
def picture_taking():
.....etc
returnvalue = subprocess.check_output("sudo ./" + config.layout + ".sh", shell=True)
GPIO.wait_for_edge(Accept_pin, GPIO.FALLING)
GPIO.wait_for_edge(Decline_Pin, GPIO.FALLING)
pad1alreadyPressed = False
pad4alreadyPressed = False
while True:
pad1pressed = not GPIO.input(Accept_pin)
pad4pressed = not GPIO.input(Decline_Pin)
if pad1pressed and not pad1alreadyPressed:
print "Accepted photo:" + str(returnvalue)
pad1alreadyPressed = pad1pressed
if pad4pressed and not pad4alreadyPressed:
print "Declined photo:" + str(returnvalue)
pad4alreadyPressed = pad4pressed
(This here is my Main Program)
#Main section
while True:
time.sleep(0.2)
#camera.hflip = false
camera.shutter_speed = 2000000000
camera.exposure_mode = 'off'
#camera.iso = 1000
camera.preview_fullscreen = True
camera.preview_alpha = 150
camera.start_preview()
GPIO.wait_for_edge(picture_pin, GPIO.FALLING)
picture_taking()
So in the picture_taking() function I would like to ask the user if they accept this picture or not
if they press button (GPIO 19) then they accept or (GPIO 6) as Decline
after they do there selection the program should go back and wait for the main button to select below. and these two buttons should only be selectable inside the function.
I tried this in the Picture_taking() function
when I make the selection it only accepts the "Decline_pin" button, but after I press the "Decline button" then it reads "Accept button".
The other issue is that it does not go back and wait for the Main button "picture_pin" to be pressed. it seems to be stuck here and not exiting.
I am not sure if this is something to do with Indent stuff in Python.
Thank you.
(This should be a comment, not an answer but I don't have reputation 50 yet).
Is this your actual code? I can't understand how it ever returns from the while True: clause in picture_taking(). I'd think you'd need to have something like
while (!(pad1alreadyPressed or pad4alreadyPressed)):
I'm using this forum for some time, but first time asking question.
I have problem with one tkinter application. It's simple chat server - client program, where chat is something like Skype. I have a friend list, and when I select friend from list, click chat, new window (toplevel) opens. Then I can send messages to friend.
Problem is on the other side. It's working fine if chat window is opened on the other side, but if it's not, I'm trying to create in by calling function that creates new toplevel. And here program freezes (on the side of friend).
def bChatPress(self, event=None):
def closeChatClient():
chatClient.destroy()
del self.chatClients[friend]
if self.friend=='':
index = self.lbFriends.curselection()
if index:
friend = self.lbFriends.get(index)
if friend not in self.chatClients:
chatClient=ChatClient(self, self.user, friend)
chatClient.protocol("WM_DELETE_WINDOW", closeChatClient)
self.chatClients[friend] = chatClient
else:
chatClient=ChatClient(self, self.user, self.friend)
chatClient.protocol("WM_DELETE_WINDOW", closeChatClient)
self.chatClients[self.friend] = chatClient
self.friend=''
def receiveMessage(self):
def loop():
print('threadstart')
while self.loged:
try:
message = self.socket.recv(1024).decode('ascii')
print(message)
if '{LOGIN}' in message:
threading.Thread(target=self.login).start()
elif '{LOGOUT}' in message:
pass
elif '{CONNECT}' in message:
self.connect(message.replace('{CONNECT}',''))
elif '{DISCONNECT}' in message:
self.disconnect()
elif '{ADD FRIEND}' in message:
self.populateFriendsList(message.replace('{ADD FRIEND}',''))
elif '{DELETE FRIEND}' in message:
self.populateFriendsList(message.replace('{DELETE FRIEND}',''))
elif '{USER CONNECT}' in message:
self.checkOnline()
elif '{USER DISCONNECT}' in message:
self.checkOnline()
elif '{CHECK ONLINE}' in message:
self.populateFriendsList(message.replace('{CHECK ONLINE}',''))
elif '{MESSAGE}' in message:
self.processMessage(message.replace('{MESSAGE}',''))
except Exception as error:
pass
threading.Thread(target=loop).start()
This is function for creating chat window. It's working fine when I create window by clicking the button, but it's not working when I call this function from receiveMessage function (this is loop for listening socket)
Did anybody had this problem before?
Thanks in advance.
You cannot call tkinter functions or widget methods from any thread except the one that created the root window.