Skip to content

Commit c15c8cd

Browse files
committed
Add typing indicator functionality for longer queries
1 parent 7ad94ff commit c15c8cd

File tree

6 files changed

+30
-6
lines changed

6 files changed

+30
-6
lines changed

.gitignore

+6
Original file line numberDiff line numberDiff line change
@@ -102,3 +102,9 @@ venv.bak/
102102

103103
# mypy
104104
.mypy_cache/
105+
106+
# macOS artifacts
107+
**/.DS_Store
108+
**/._*
109+
110+
**/inputs_received.txt

app.py

+6
Original file line numberDiff line numberDiff line change
@@ -35,5 +35,11 @@ def get_location(idChat):
3535
bot.get_location(idChat)
3636
return 'ok'
3737

38+
@app.route('/send_typing_act/<string:idChat>', methods['POST'])
39+
def send_typing_action(idChat):
40+
bot.send_typing_action(idChat)
41+
return 'ok'
42+
43+
3844
if __name__ == '__main__':
3945
app.run(port=5000)

bot.py

+5-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import requests
2-
from config import TELEGRAM_SEND_MESSAGE_URL, TELEGRAM_SEND_MESSAGE_URL_BASE, TELEGRAM_SEND_PHOTO_URL, TELEGRAM_SEND_AUDIO_URL, CHAT_PROCESSOR_URL
2+
from config import TELEGRAM_SEND_MESSAGE_URL, TELEGRAM_SEND_MESSAGE_URL_BASE, TELEGRAM_SEND_PHOTO_URL, TELEGRAM_SEND_AUDIO_URL, CHAT_PROCESSOR_URL, TELEGRAM_SEND_TYPING_ACTION
33
import json
44

55
def send_message_to_chat_processor(req):
@@ -44,6 +44,10 @@ def send_photo_to_user(idChat, photo_url, caption):
4444
else:
4545
return False
4646

47+
def send_typing_action(idChat):
48+
res = requests.get(TELEGRAM_SEND_TYPING_ACTION.format(idChat, 'typing'))
49+
50+
4751
def get_location(idChat):
4852

4953
reply_markup={

config.py

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
BOT_TOKEN = ''
2-
NGROK_URL = ''
1+
BOT_TOKEN = '1018188547:AAHr3fdw0Xarzg6jHuKqc80IruuS3F--Ql0'
2+
NGROK_URL = 'https://14478b6f.ngrok.io'
33
CHAT_PROCESSOR_URL = 'http://localhost:5001'
44
LOCAL_WEBHOOK_ENDPOINT = '{}/webhook'.format(NGROK_URL)
55
BASE_TELEGRAM_API_URL = 'https://api.telegram.org/bot' + BOT_TOKEN
@@ -8,3 +8,4 @@
88
TELEGRAM_SEND_MESSAGE_URL_BASE = BASE_TELEGRAM_API_URL + '/sendMessage'
99
TELEGRAM_SEND_PHOTO_URL = BASE_TELEGRAM_API_URL + '/sendPhoto?chat_id={}&photo={}&caption={}&parse_mode={}'
1010
TELEGRAM_SEND_AUDIO_URL = BASE_TELEGRAM_API_URL + '/sendAudio?chat_id={}&audio={}&caption={}&parse_mode={}'
11+
TELEGRAM_SEND_TYPING_ACTION = BASE_TELEGRAM_API_URL + '/sendChatAction?chat_id={}&action={}'

usability_tests_simulation/bot.py

+7-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
import requests
2-
from config import TELEGRAM_SEND_MESSAGE_URL, TELEGRAM_SEND_PHOTO_URL
2+
from config import TELEGRAM_SEND_MESSAGE_URL, TELEGRAM_SEND_PHOTO_URL, TELEGRAM_SEND_TYPING_ACTION
33
import json
44
import re
5+
import time
56

67
class Bot:
78

@@ -29,6 +30,9 @@ def send_movie(self, html_text, banner_url):
2930
return True
3031
else:
3132
return False
33+
34+
def send_typing_action(self):
35+
res = requests.get(TELEGRAM_SEND_TYPING_ACTION.format(self.chat, 'typing'))
3236

3337
def parse_data(self, data):
3438

@@ -55,6 +59,8 @@ def action(self):
5559
self.send_message('Ok. Estou agora no passo ' + str(count) + '.')
5660
else:
5761
if self.msg_count == 1:
62+
self.send_typing_action()
63+
time.sleep(5)
5864
# Pedir ao utilizador para consultar os cinemas mais próximos.
5965
self.send_message('Os cinemas NOS perto de si num raio de 20Km são:\nBraga Parque')
6066
elif self.msg_count == 2:

usability_tests_simulation/config.py

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
1-
BOT_TOKEN = ''
2-
NGROK_URL = ''
1+
BOT_TOKEN = '1018188547:AAHr3fdw0Xarzg6jHuKqc80IruuS3F--Ql0'
2+
NGROK_URL = 'https://ed3a3b4a.ngrok.io'
33
LOCAL_WEBHOOK_ENDPOINT = '{}/webhook'.format(NGROK_URL)
44
BASE_TELEGRAM_API_URL = 'https://api.telegram.org/bot' + BOT_TOKEN
55
TELEGRAM_INIT_WEBHOOK_URL = '{}/setWebhook?url={}'.format(BASE_TELEGRAM_API_URL, LOCAL_WEBHOOK_ENDPOINT)
66
TELEGRAM_SEND_MESSAGE_URL = BASE_TELEGRAM_API_URL + '/sendMessage?chat_id={}&text={}&parse_mode={}'
77
TELEGRAM_SEND_MESSAGE_URL_BASE = BASE_TELEGRAM_API_URL + '/sendMessage'
88
TELEGRAM_SEND_PHOTO_URL = BASE_TELEGRAM_API_URL + '/sendPhoto?chat_id={}&photo={}&caption={}&parse_mode={}'
99
TELEGRAM_SEND_AUDIO_URL = BASE_TELEGRAM_API_URL + '/sendAudio?chat_id={}&audio={}&caption={}&parse_mode={}'
10+
TELEGRAM_SEND_TYPING_ACTION = BASE_TELEGRAM_API_URL + '/sendChatAction?chat_id={}&action={}'

0 commit comments

Comments
 (0)