-
Notifications
You must be signed in to change notification settings - Fork 86
/
ultrabot.py
111 lines (87 loc) · 3.64 KB
/
ultrabot.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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
import json
import requests
import datetime
class ultraChatBot():
def __init__(self, json):
self.json = json
self.dict_messages = json['data']
self.ultraAPIUrl = 'https://api.ultramsg.com/{{instance_id}}/'
self.token = '{{token}}'
def send_requests(self, type, data):
url = f"{self.ultraAPIUrl}{type}?token={self.token}"
headers = {'Content-type': 'application/json'}
answer = requests.post(url, data=json.dumps(data), headers=headers)
return answer.json()
def send_message(self, chatID, text):
data = {"to" : chatID,
"body" : text}
answer = self.send_requests('messages/chat', data)
return answer
def send_image(self, chatID):
data = {"to" : chatID,
"image" : "https://file-example.s3-accelerate.amazonaws.com/images/test.jpeg"}
answer = self.send_requests('messages/image', data)
return answer
def send_video(self, chatID):
data = {"to" : chatID,
"video" : "https://file-example.s3-accelerate.amazonaws.com/video/test.mp4"}
answer = self.send_requests('messages/video', data)
return answer
def send_audio(self, chatID):
data = {"to" : chatID,
"audio" : "https://file-example.s3-accelerate.amazonaws.com/audio/2.mp3"}
answer = self.send_requests('messages/audio', data)
return answer
def send_voice(self, chatID):
data = {"to" : chatID,
"audio" : "https://file-example.s3-accelerate.amazonaws.com/voice/oog_example.ogg"}
answer = self.send_requests('messages/voice', data)
return answer
def send_contact(self, chatID):
data = {"to" : chatID,
"contact" : "14000000001@c.us"}
answer = self.send_requests('messages/contact', data)
return answer
def time(self, chatID):
t = datetime.datetime.now()
time = t.strftime('%Y-%m-%d %H:%M:%S')
return self.send_message(chatID, time)
def welcome(self,chatID, noWelcome = False):
welcome_string = ''
if (noWelcome == False):
welcome_string = "Hi , welcome to WhatsApp chatbot using Python\n"
else:
welcome_string = """wrong command
Please type one of these commands:
*hi* : Saluting
*time* : show server time
*image* : I will send you a picture
*video* : I will send you a Video
*audio* : I will send you a audio file
*voice* : I will send you a ppt audio recording
*contact* : I will send you a contact
"""
return self.send_message(chatID, welcome_string)
def Processingـincomingـmessages(self):
if self.dict_messages != []:
message =self.dict_messages
text = message['body'].split()
if not message['fromMe']:
chatID = message['from']
if text[0].lower() == 'hi':
return self.welcome(chatID)
elif text[0].lower() == 'time':
return self.time(chatID)
elif text[0].lower() == 'image':
return self.send_image(chatID)
elif text[0].lower() == 'video':
return self.send_video(chatID)
elif text[0].lower() == 'audio':
return self.send_audio(chatID)
elif text[0].lower() == 'voice':
return self.send_voice(chatID)
elif text[0].lower() == 'contact':
return self.send_contact(chatID)
else:
return self.welcome(chatID, True)
else: return 'NoCommand'