I have configured and have the code written out in my Django app to integrate Twilio SMS sending and receiving. It is successfully sending messages.
However, the problem I'm coming across is that the public facing URL I have to communicate with the webhook and TwiML is only accessible for the admin so you have to have a certain authentication to access the messaging dashboard (where the messages are coming in). I'm not sure how to configure this in Twilio
I was wondering how I can configure this webhook in Twilio to ensure a user can respond via SMS from their phone and the message will display in the messaging dashboard within the app.
I'm not super familiar with Twilio or this situation so any help would be greatly appreciated. Thanks in advance!
Twilio's serverless offering, Functions, could help you out here. It's not available in Python but uses Node.js instead. However, it's pretty straightforward, allows you to call your own URLs with any authentication mechanisms, and helps you store secrets securely.
Here's a small sample snippet from the docs:
const axios = require('axios');
exports.handler = async (context, event, callback) => {
const twiml = new Twilio.twiml.MessagingResponse();
const response = await axios
.get('https://dog.ceo/api/breeds/image/random');
twiml
.message(`Hello, ${event.From}! Enjoy this doge!`)
.media(response.data.message);
return callback(null, twiml);
};
Related
I was wondering, if it is possible to connect (as an admin) with a user and to write messages? I already modified the message-table in the database, however, the message does only appear after reloading the whole website - which is not the optimal outcome. I appreciate every hint. Thank you.
You have to use a pub/sub service to get notification on the client side when a message is sent in server side
A good start is pusher.com service, in your server side code, you will run this example code whenever you send a message to your user:
pusher.trigger('my-channel', 'my-event', {
'message': 'hello world'
})
and in your client side, you add a code that listen to the above channel/topic and act accordingly
var channel = pusher.subscribe('my-channel');
channel.bind('my-event', function(data) {
alert('Received my-event with message: ' + data.message);
});
I am planning to build a home automation system where IoT devices communicate with the MQTT broker.The system also involves a Django web server that serves API for ios/android devices. I will describe an example of what I want to implement.
An API call is made from mobile app to Django server to turn ON a device. When such API request is made to Django, it should push 'Turn ON' data to IoT device via MQTT protocol.
Also, IoT devices send some real time data to the MQTT broker. Upon receiving such data I want to send push notifications to a mobile app via apns/fcm.
How can I implement this?. Will Django channels serve the purpose or should I code my Django server to act as an MQTT client and communicate with the MQTT broker?. Or is there any other methods to implement this.
well, i did a little project on paho-MQTT it's a nice experience with google chrome extension MQTTLens.(you should try this if u aren't using this already)
in your case, I think you can use rest-framework of Django for building an API and on the front-end, you can use crispy-form to make ON-OFF signals and this will directly communicate to the views of Django in which you can write the client and subscriber details.
lets focus on An API call is made from mobile app to Django server to turn ON a device. When such API request is made to Django, it should push 'Turn ON' data to IoT device via MQTT protocol.
you can make views which response to the API call from any devices for that you can check django-rest-framework this is the best option that we have.
and now IoT devices send some real time data to the MQTT broker
for this, you can check Google's this artical. MQTT broker can be handed with the Django views easily and this process not very complex if you use the modular structure with Django's DRY concpet.
on the other hand, you can also make different views for just client or for broker it's up to you but i think this approach will take a long time to devlope such application I don't know about mobile development so i can't help you with that :(.
You can handle the task using JavaScript. I have experience in implementing MQTT protocol in Django and Django-REST projects using JavaScript. You should embed a JavaScript code block in your frontend file (in my case, HTML). First, you should call the Paho-MQTT jQuery package in your file.
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/paho-mqtt/1.0.2/mqttws31.min.js"></script>
Then add this block of code.
#parameters
var hostname = "mqtt.eclipse.org"; #There are different brokers. You should enter the broker's hostname.
var port = 80; #The port number can be different based on a TLS or non-TLS connection.
var ClientID = "ClientID_" + parseInt(Math.random()*100);
#Create a client instance
var client = new Paho.MQTT.Client(hostname, Number(port), ClientID);
#Set callback handlers
client.onConnectionLost = onConnectionLost;
client.onMessageArrived = onMessageArrived;
#Connect the client
client.connect(
{onSuccess: onConnect}
);
#Called when client connects
function onConnect() {
#Once a connection has been established, make a subscription and send a message
console.log("onConnect");
client.subscribe("subTopic");
alert("Connected.");
}
#Called when the client loses its connection
function onConnectionLost(responseObject){
if(responseObject.errorCode != 0){
console.log("onConnectionLost:" + responseObject.errorMessage);
}
}
#Called when a message arrives
function onMessageArrived(message) {
console.log("Message arrived: topic=" + message.destinationName + ", message=" + message.payloadString);
if (message.destinationName == "subTopic") {
#Do something
}
By using the code your application will be connected to the broker and listen to one or multiple topics. It means you can get sensors' data realtime. This necessitates to publish sensor's data on your hardware device, say ESP module or Raspberry PI.
It is very likely that you want to send commands from your application to the actuators to turn them on or off. For doing that you need to publish some messages from your application that your hardware will listen to.
Assume you have a toggle switch that you want to publish a message by toggling that.
<label id="switch{{ device.unique_id }}" class="switch">
<input id="state{{ device.unique_id }}" type="checkbox" onclick="publish('{{ device.unique_id }}')">
<span class="slider round"></span>
</label>
The above HTML block should reside into a django for block. Then you should write the publish onclick function to call that on toggling the switch. You can see an example of such a function below.
function publish(x) {
if(!client){
return;
}
var status = document.getElementById(x);
if (status.innerHTML == 'ON'){
status.innerHTML = 'OFF';
var message = new Paho.MQTT.Message("TurnOFF");
message.destinationName = "pubTopic";
client.send(message);
} else {
status.innerHTML = 'ON';
var message = new Paho.MQTT.Message("TurnON");
message.destinationName = "pubTopic";
client.send(message);
}
}
x in the publish function is the id that is embedded in the HTML file. To receive your published messages you should listen to the specific topic(s) on your hardware device.
I'm developing a Twilio app that leverages the Machine Detection features. I make a request using the Twilio python client as follows:
import twilio
twilio_client = Client(ACCOUNT_SID, AUTH_TOKEN)
call = twilio_client.calls.create(
to=number,
from_=TWILIO_PHONE_NUMBER,
url=EXTERNAL_URL + '/call',
status_callback=EXTERNAL_URL + '/call_status',
status_callback_event=["initiated", "ringing", "answered",
"completed", "no-answer", "busy",
"failed", "canceled", "queued"],
machine_detection='Enable',
machine_detection_timeout=20,
)
but when I try to access the call.answered_by field I discover that the value is None. I also get back None when I use the call status API to fetch the call status 10 seconds later:
call = twilio_client.calls(sid).fetch()
Also in my my webhook that I register at `EXTERNAL_URL + '/call_status' to get notifications I don't get an 'AnsweredBy' field. I've also made sure I enabled the feature in the Twilio web console. What else I am missing?
Twilio developer evangelist here.
Thanks to Andy and Alex in the comments, I think they actually do cover everything.
The key here is that the AnsweredBy field will be sent as part of the webhook to the URL you set as the url parameter in the API request. In this case, that is the URL EXTERNAL_URL + '/call'.
The AnsweredBy field should eventually appear in the REST API for the call resource itself, but the way to use it is via the webhook.
Let me know if this helps at all.
I'm trying to figure how to properly implement Google+ Sign In (https://developers.google.com/+/web/signin/) in my AngularJS app to secure the REST API that it depends on.
I have 2 components to my application: the AngularJS app and a Python Flask REST API. I want to keep the REST API stateless. I see that there is the "client-side flow" and the "hybrid server-side flow", but I'm not sure which one to use.
I was imagining this flow in my head:
1) User signs in through the AngularJS app and receives a token from Google
2) User passes the token along with every REST API request
3) The API server verifies the token with Google before processing the request and returns an error if the token is invalid
Would I just use the client-side flow then and have my server call some Google API to verify token it receives with every request?
I think satellizer would be a great solution.
Satellizer is a simple to use, end-to-end, token-based authentication module for AngularJS with built-in support for Google, Facebook, LinkedIn, Twitter authentication providers, plus Email and Password sign-in method. You are not limited to the sign-in options above, in fact you can add any OAuth 1.0 or OAuth 2.0 provider by passing provider-specific information during the configuration step.
Installation
bower install satellizer --save
Usage
"Google+" in your case
angular.module('MyApp', ['satellizer']).config(function($authProvider) {
$authProvider.google({
clientId: '631036554609-v5hm2amv4pvico3asfi97f54sc51ji4o.apps.googleusercontent.com'
});
});
Controller
angular.module('MyApp').controller('LoginCtrl', function($scope, $auth) {
$scope.authenticate = function(provider) {
$auth.authenticate(provider);
};
});
Template
<button ng-click="authenticate('google')">Sign in with Google</button>
Full Documentation you can find it here https://github.com/sahat/satellizer
and for server side "python" in your case you'll be able to see an example here https://github.com/sahat/satellizer/tree/master/examples/server/python
I have an app which amounts to a Python script, running on the user's phone, and a JS client, running in the user's browser. The Python script sends messages to App Engine as HTTP requests. The server then pushes the messages to the JS client.
The problem is authentication: The server can easily use Google Accounts to authenticate anything coming from the JS client as being sent by a particular user, but I do not know how to get the Python script to make HTTP requests which will also authenticate.
Any ideas?
According to its homepage, httplib2 has support for Google Account authentication, maybe that may help you?
Can you use OAUth to authenticate with Google, then use the OAuth token to ensure the messages are legitimate?