-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrun.py
59 lines (48 loc) · 1.75 KB
/
run.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
#!/usr/bin/env python3.7 ++
import requests
import logging
from telegram import ForceReply, Update
from telegram.ext import Application, ContextTypes, MessageHandler, filters
############# REPLACE THIS ###############
BOT_TOKEN = "xysysysdaiyodyx"
SIMI_API_KEY = "xysydiuasoiud9sys"
logging.basicConfig(
format="%(asctime)s - %(name)s - %(levelname)s - %(message)s", level=logging.INFO
)
logging.getLogger("httpx").setLevel(logging.WARNING)
logger = logging.getLogger(__name__)
def get_simsimi_response(text):
try:
URL = "https://wsapi.simsimi.com/190410/talk"
headers = {
"Content-Type": "application/json",
"x-api-key": SIMI_API_KEY
}
payload = {
"utext": text,
"lang": "id"
}
r = requests.post(url=URL, headers=headers, json=payload)
if r.status_code == 200:
data = r.json()
if 'atext' in data:
return data['atext']
elif 'msg' in data and data['result'] == 509:
return data['msg']
else:
return "Error: Invalid response format"
else:
return f"Error: {r.status_code} {r.reason}"
except Exception as e:
print("Error:", e)
return "Error occurred"
async def echo(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None:
user_input = update.message.text
response = get_simsimi_response(user_input)
await update.message.reply_text(response)
def main() -> None:
application = Application.builder().token(BOT_TOKEN).build()
application.add_handler(MessageHandler(filters.TEXT, echo))
application.run_polling(allowed_updates=Update.ALL_TYPES)
if __name__ == "__main__":
main()