-
Notifications
You must be signed in to change notification settings - Fork 0
/
testT.py
68 lines (50 loc) · 1.8 KB
/
testT.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
from typing import Counter
import requests
import pandas as pd
resp = ["Sorry The quesion is not in the Database","Sorry ! Try again please"]
#dburl = "https://raw.githubusercontent.com/Noraldim/RESOURCES/master/FULL%20BOT%20RESORSEs.tsv"
url = "https://api.telegram.org/bot5877922080:AAF3bU4om-Yoksl39wZyLR_hvxn87vAqeQw/"
counter = 0
# download the tsv file from the link and save it locally
#df = pd.read_csv(dburl, sep = "\t")
df = pd.read_parquet("dialogues.parquet")
def read(offset):
para = {
"offset" : offset
}
req = requests.get(url + "getUpdates", data = para)
data = req.json()
print(data)
for result in data["result"]:
send(result)
if data["result"]:
return data["result"][-1]["update_id"] + 1
def out(message):
global counter
# Normalize the message for comparison
normalized_message = message.lower()
# Find rows where any Description word in the Description matches words in the message
matches = df[df['Description'].str.lower().str.split().apply(
lambda desc_words: any(word in normalized_message.split() for word in desc_words)
)]
# If we found matches,Doctor return the answer from the Doctor column of the first match
if not matches.empty:
answer = matches.iloc[0]['Doctor']
return answer
else:
# If no exact or partial match found, return the next response
counter = (counter + 1) % len(resp)
return resp[counter]
def send(result):
text = result["message"]["text"]
answer = out(text)
parameter = {
"chat_id" : result["message"]["chat"]["id"],
"text" : answer ,
"reply_to_message_id" : None
}
req = requests.get(url + "sendMessage", data = parameter)
print(req.text)
offset = 0
while True :
offset = read(offset)