Ultramsg.com WhatsApp Bot using WhatsApp API and ultramsg
Demo WhatsApp API ChatBot using Ultramsg API with python.
- The output of the command list .
- The output of the server time of the bot running on .
- Sending image to phone number or group .
- Sending audio file .
- Sending ppt audio recording .
- Sending Video File.
- Sending contact .
- Ultramsg account is required to run examples. Log in or Create Account if you don't have one ultramsg.com.
- go to your instance or Create one if you haven't already.
- Scan Qr and make sure that instance Auth Status : authenticated
the WebHook URL must be provided for the server to trigger our script for incoming messages. we will deployed the server using the FLASK microframework. The FLASK server allows us to conveniently process incoming requests.
pip install flask
Then clone the repository for yourself. Then go to the ultrabot.py file and replace the ultraAPIUrl and instance token.
for local development purposes, a tunneling service is required. This example uses ngrok , You can download ngrok from here : ngrok .
Class constructor, the default one that will accept JSON, which will contain information about incoming messages (it will be received by WebHook and forwarded to the class). To see how the received JSON will look this video .
flask run
ngrok http 5000
./ngrok http 5000
Used to send requests to the Ultramsg API
defsend_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)
returnanswer.json()- type determines the type message .
- data contains the data required for sending requests.
Used to send WhatsApp text messages
defsend_message(self, chatID, text):
data= {"to" : chatID,
"body" : text} answer=self.send_requests('messages/chat', data)
returnanswer- ChatID – ID of the chat where the message should be sent for him, e.g 14155552671@c.us .
- Text – Text of the message .
Sends the current server time .
deftime(self, chatID):
t=datetime.datetime.now()
time=t.strftime('%d:%m:%Y')
returnself.send_message(chatID, time)- ChatID – ID of the chat where the message should be sent for him, e.g 14155552671@c.us .
Send a image to phone number or group
defsend_image(self, chatID):
data= {"to" : chatID,
"image" : "https://file-example.s3-accelerate.amazonaws.com/images/test.jpeg"} answer=self.send_requests('messages/image', data)
returnanswer- ChatID – ID of the chat where the message should be sent for him, e.g 14155552671@c.us .
Send a Video to phone number or group
defsend_video(self, chatID):
data= {"to" : chatID,
"video" : "https://file-example.s3-accelerate.amazonaws.com/video/test.mp4"} answer=self.send_requests('messages/video', data)
returnanswer- ChatID – ID of the chat where the message should be sent for him, e.g 14155552671@c.us .
Send a audio file to phone number or group
defsend_audio(self, chatID):
data= {"to" : chatID,
"audio" : "https://file-example.s3-accelerate.amazonaws.com/audio/2.mp3"} answer=self.send_requests('messages/audio', data)
returnanswer- ChatID – ID of the chat where the message should be sent for him, e.g 14155552671@c.us .
Send a ppt audio recording to phone number or group
defsend_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)
returnanswer- ChatID – ID of the chat where the message should be sent for him, e.g 14155552671@c.us .
Sending one contact or contact list to phone number or group
defsend_contact(self, chatID):
data= {"to" : chatID,
"contact" : "14000000001@c.us"} answer=self.send_requests('messages/contact', data)
returnanswer- ChatID – ID of the chat where the message should be sent for him, e.g 14155552671@c.us .
defProcessingـincomingـmessages(self):
ifself.dict_messages!= []:
message=self.dict_messagestext=message['body'].split()
ifnotmessage['fromMe']:
chatID=message['from'] iftext[0].lower() =='hi':
returnself.welcome(chatID)
eliftext[0].lower() =='time':
returnself.time(chatID)
eliftext[0].lower() =='image':
returnself.send_image(chatID)
eliftext[0].lower() =='video':
returnself.send_video(chatID)
eliftext[0].lower() =='audio':
returnself.send_audio(chatID)
eliftext[0].lower() =='voice':
returnself.send_voice(chatID)
eliftext[0].lower() =='contact':
returnself.send_contact(chatID)
else:
returnself.welcome(chatID, True)
else: return'NoCommand'To process incoming messages to our server
fromflaskimportFlask, request, jsonifyfromultrabotimportultraChatBotimportjsonapp=Flask(__name__)
@app.route('/', methods=['POST'])defhome():
ifrequest.method=='POST':
bot=ultraChatBot(request.json)
returnbot.Processingـincomingـmessages()
if(__name__) =='__main__':
app.run()We will write the path app.route('/', methods = ['POST']) for it. This decorator means that our home function will be called every time our FLASK server .