I want to get all messages history of particular sender and receiver. i am using fronEnd angular5 and Backend Django..
my db sample table :
{
"id": 288,
"name": "Faric",
"receiver": "drit",
"text": "hiee",
"myDate": "2018-07-26T04:38:05.505000Z"
}
chatService:
url : string = 'http://127.0.0.1:8000/msg/';
messages: Subject<ChatMessage>;
getMessage(): Promise<chatMessage[]>{
return this.http.get(this.url).map(res => res.json()).topromise(); #url of messages API
}
chatcomponent:
this.chat.getMessage()
.then(msg=> this.messages = msg); # messges = []
using this service and component i get all mssages.. jst i want to get perticular sender and receiver's data. how to do this?
You can use ES6's find function to fetch data for a particular sender / receiver,
Let's say if you want to get the data based on the ID field (in your json), you can try something like,
this.messageForAnUser = this.messages.find(message=> message.id === THE_SENDER_OR_RECEIVER_ID_YOU_WANT_TO_SEARCH)
Hope this helps!
Related
I use django channels 3.0.0 and websocket using angular.
But users connect to django websocket and they are in their own rooms respectively.
And when I want to send the event to connected users outside of consumers, I used the following code.
all_users = list(Member.objects.filter(is_active = True).values_list('id', flat = True))
for user_id in all_users:
async_to_sync(channel_layer.group_send)(
"chat_{}".format(user_id),
{ "type": "tweet_send", "message": "tweet created" }
)
And in consumers.py, my consumer class's chat_message function
async def tweet_send(self, event):
content = event['message']
# Send message to WebSocket
await self.send(text_data=json.dumps({
"type": "MESSAGE",
"data": content
}))
And this "self.send" function is meant to be sent to all connected users respectively, but when I run the code, the all data are sent to only one user who has connected the last time.
I don't know why. If anyone knows the reason, please help me.
This is indeed a bug in version 3.0.0. It was reported and fixed in 3.0.1.
I'm using Postman app to interact with a Telegram bot api. I've sent photos using the sendPhoto() method, like this:
https://api.telegram.org/botToken/sendPhoto?chat_id=00000000&photo=AgAC***rgehrehrhrn
But I don't understand the sendMediaGroup() method. Can someone post an example how to compose the https string to send two photos?
Thanks
You need to send a POST request at the url https://api.telegram.org/botToken/sendPhoto with a JSON body. You are using the url to specify all the parameters of the request but urls are only 2000 characters long. The body of a POST request, instead, has no limits in terms of size. The JSON body should look something like this:
{
"chat_id": 777000,
"media": [
{
"type": "photo",
"media": "https://example.com/first_photo_url.png",
"caption": "an optional description of the first photo",
"parse_mode": "optional (you can delete this parameter) the parse mode of the caption"
},
{
"type": "photo",
"media": "https://example.com/fsecond_photo_url.png",
"caption": "an optional description of the second photo",
"parse_mode": "optional (you can delete this parameter) the parse mode of the caption"
}
],
}
For more info see:
how to send JSON (raw) data with Postman
and
sendMediaGroup Telegram API's method.
You must send JSON as a string, or serialized JSON, to Telegram API. The format is as same as #GioIacca9's answer.
Note: only the caption in the first image will be showen.
Have a try this Python code.
def send_photos(api_key, chat_id, photo_paths):
params = {
'chat_id': chat_id,
'media': [],
}
for path in photo_paths:
params['media'].append({'type': 'photo', 'media': path})
params['media'] = json.dumps(params['media'])
url = f'https://api.telegram.org/bot{api_key}/sendMediaGroup'
return requests.post(url, data=params)
if __name__ == '__main__':
send_photos('your_key', '#yourchannel', ['http://your.image.one', 'http://your.image.two'])
I am working on integrating GMB into some of our internal apps, and would like to set up to receive real-time notifications for reviews and questions.
I have created a topic, and a subscription with a valid URL.
The next step is to tell GMB to send the notifications to the topic, and I believe the endpoint is the one below. However, it is very vague about the parameters it wants.
This is the documentation
https://developers.google.com/my-business/reference/rest/v4/accounts/updateNotifications
It wants a "Notification Settings Resource Name" in the URL, but it's not explained anywhere what that actually is. I have tried every possible value, but always get a 404 error response with the message "Requested entity was not found."
Has anyone successfully set this up? What values does the "getNotifications" endpoint want, and where in the various dashboards can this be found or created?
Any help is much appreciated!
As mentioned in the comments, you need to send the accountId as part of the URL.
To find this, you will first need to send a GET request to
https://mybusiness.googleapis.com/v4/accounts
This will return something along the following lines:
{
"accounts": [
{
"name": "accounts/102647145453118950380",
"accountName": "Tom Spencer",
"type": "PERSONAL",
"state": {
"status": "UNVERIFIED",
"vettedStatus": "NOT_VETTED"
},
"profilePhotoUrl": "//lh3.googleusercontent.com/a-/AOh14GgPkuJj03DeCa1isBAJALY4eOl09WGYVFrM4mG5=s132"
},
]
}
You can see here that accounts/102647145453118950380 is returned in the name field. Take this field, and construct the following URL:
https://mybusiness.googleapis.com/v4/accounts/102647145453118950380/notifications
Send a PUT request to this URL, with a request body resembling the following:
{
"topicName": "projects/{projectId}/topics/{topicId}",
"notificationTypes": [
"NEW_REVIEW",
"UPDATED_REVIEW"
]
}
Assuming you have pub/sub setup as per the documentation, this should send a message to your topic/subscribers whenever a new review is created or a review is updated.
I've looked through the posts here, and I haven't found a solution yet.
I'm trying to send a POST request to send a file to a server.
Part of the form data accepts the binary file, and there are other mandatory fields to add to the form.
The last solution I've tried was this:
my_data = {
"repo_upload_file": ("filename.pdf", open("filename.pdf", rb), "application/pdf"),
"sesskey": (None, sess_key),
"repo_id": (None, repo_id),
"author": (None, author),
"savepath": (None, save_path),
"title": (None, title),
"itemid": (None, itemid),
"ctx_id": (None, ctx_id)
}
response = session.post("http://mywebsite/repo.php?action=upload", files=my_data)
print response.content # expecting a json object that stores the url to the uploaded file
When I try via my browser (this server has a ui), it works fine. Using fiddler, I've seen that the fields get sent as WebForms :
However, when using my code, it appears on fiddler that somehow the server doesn't recognize them as WebForms, even though it appears that the request was sent correctly
This is how the server expects them, but for some reason this doesn't seem to be working
When I got it working fine from the browser, this was the request form datas:
If you desire to post other data which is not file, you should follow one of those
my_data = {
"repo_upload_file": ("filename.pdf", open("filename.pdf", "rb"), "application/pdf")
}
data = {
"sesskey": sess_key,
"repo_id": repo_id,
"author": author,
"savepath": save_path,
"title": title,
"itemid": itemid,
"ctx_id": ctx_id
}
Type 1, post data normally
response = session.post(url, data = data , files=my_data)
Type 2, post as json
response = session.post(url, json = data , files=my_data)
Is there some way to send push notifications using Parse and a web service built in Django? I mean, I have a dashboard built in django, in this dashboard I can set it up some parameters, when I create for example a new news, this has to be notified to the user through a push notifications. How can I achieve this?
See that page... https://www.parse.com/docs/rest/guide you should try make a request similar to this....every time you make some news
import json,httplib
connection = httplib.HTTPSConnection('api.parse.com', 443)
connection.connect()
connection.request('POST', '/1/classes/GameScore', json.dumps({
"score": 1337,
"playerName": "Sean Plott",
"cheatMode": False
}), {
"X-Parse-Application-Id": "${APPLICATION_ID}",
"X-Parse-REST-API-Key": "${REST_API_KEY}",
"Content-Type": "application/json"
})
results = json.loads(connection.getresponse().read())
print results