|
| 1 | +import json |
| 2 | +import requests |
| 3 | +import datetime |
| 4 | + |
| 5 | +class ultraChatBot(): |
| 6 | + def __init__(self, json): |
| 7 | + self.json = json |
| 8 | + self.dict_messages = json['data'] |
| 9 | + self.ultraAPIUrl = 'https://api.ultramsg.com/{{instance_id}}/' |
| 10 | + self.token = '{{token}}' |
| 11 | + |
| 12 | + |
| 13 | + def send_requests(self, type, data): |
| 14 | + url = f"{self.ultraAPIUrl}{type}?token={self.token}" |
| 15 | + headers = {'Content-type': 'application/json'} |
| 16 | + answer = requests.post(url, data=json.dumps(data), headers=headers) |
| 17 | + return answer.json() |
| 18 | + |
| 19 | + def send_message(self, chatID, text): |
| 20 | + data = {"to" : chatID, |
| 21 | + "body" : text} |
| 22 | + answer = self.send_requests('messages/chat', data) |
| 23 | + return answer |
| 24 | + |
| 25 | + def send_image(self, chatID): |
| 26 | + data = {"to" : chatID, |
| 27 | + "image" : "https://file-example.s3-accelerate.amazonaws.com/images/test.jpeg"} |
| 28 | + answer = self.send_requests('messages/image', data) |
| 29 | + return answer |
| 30 | + |
| 31 | + def send_video(self, chatID): |
| 32 | + data = {"to" : chatID, |
| 33 | + "video" : "https://file-example.s3-accelerate.amazonaws.com/video/test.mp4"} |
| 34 | + answer = self.send_requests('messages/video', data) |
| 35 | + return answer |
| 36 | + |
| 37 | + def send_audio(self, chatID): |
| 38 | + data = {"to" : chatID, |
| 39 | + "audio" : "https://file-example.s3-accelerate.amazonaws.com/audio/2.mp3"} |
| 40 | + answer = self.send_requests('messages/audio', data) |
| 41 | + return answer |
| 42 | + |
| 43 | + |
| 44 | + def send_voice(self, chatID): |
| 45 | + data = {"to" : chatID, |
| 46 | + "audio" : "https://file-example.s3-accelerate.amazonaws.com/voice/oog_example.ogg"} |
| 47 | + answer = self.send_requests('messages/voice', data) |
| 48 | + return answer |
| 49 | + |
| 50 | + def send_contact(self, chatID): |
| 51 | + data = {"to" : chatID, |
| 52 | + "contact" : "14000000001@c.us"} |
| 53 | + answer = self.send_requests('messages/contact', data) |
| 54 | + return answer |
| 55 | + |
| 56 | + |
| 57 | + def time(self, chatID): |
| 58 | + t = datetime.datetime.now() |
| 59 | + time = t.strftime('%d:%m:%Y') |
| 60 | + return self.send_message(chatID, time) |
| 61 | + |
| 62 | + |
| 63 | + def welcome(self,chatID, noWelcome = False): |
| 64 | + welcome_string = '' |
| 65 | + if (noWelcome == False): |
| 66 | + welcome_string = "Hi , welcome to WhatsApp chatbot using Python\n" |
| 67 | + else: |
| 68 | + welcome_string = """wrong command |
| 69 | +Please type one of these commands: |
| 70 | +*hi* : Saluting |
| 71 | +*time* : show server time |
| 72 | +*image* : I will send you a picture |
| 73 | +*video* : I will send you a Video |
| 74 | +*audio* : I will send you a audio file |
| 75 | +*voice* : I will send you a ppt audio recording |
| 76 | +*contact* : I will send you a contact |
| 77 | +""" |
| 78 | + return self.send_message(chatID, welcome_string) |
| 79 | + |
| 80 | + |
| 81 | + def Processingـincomingـmessages(self): |
| 82 | + if self.dict_messages != []: |
| 83 | + message =self.dict_messages |
| 84 | + text = message['body'].split() |
| 85 | + if not message['fromMe']: |
| 86 | + chatID = message['from'] |
| 87 | + if text[0].lower() == 'hi': |
| 88 | + return self.welcome(chatID) |
| 89 | + elif text[0].lower() == 'time': |
| 90 | + return self.time(chatID) |
| 91 | + elif text[0].lower() == 'image': |
| 92 | + return self.send_image(chatID) |
| 93 | + elif text[0].lower() == 'video': |
| 94 | + return self.send_video(chatID) |
| 95 | + elif text[0].lower() == 'audio': |
| 96 | + return self.send_audio(chatID) |
| 97 | + elif text[0].lower() == 'voice': |
| 98 | + return self.send_voice(chatID) |
| 99 | + elif text[0].lower() == 'contact': |
| 100 | + return self.send_contact(chatID) |
| 101 | + else: |
| 102 | + return self.welcome(chatID, True) |
| 103 | + else: return 'NoCommand' |
| 104 | + |
| 105 | + |
| 106 | + |
| 107 | + |
| 108 | + |
| 109 | + |
| 110 | + |
| 111 | + |
0 commit comments