2016-06-28 83 views
2

使用twilio-ruby包連接到Twilio的IP消息系統服務的REST API並嘗試計算未讀消息數。Twilio IP消息 - 獲取REST API上的最後一條消息索引

的REST API是分頁的消息,以便像

channel.messages.list.last.index 

將返回49就曾有在渠道超過50條消息。

有沒有辦法讓通道上的最後一條消息(在android/ios SDK中似乎是可能的),以避免在所有消息歷史中分頁?

回答

1

在問候計算未讀消息計數,看看在Message Consumption Horizon和從列表中的消息的總數減去lastConsumedMessageIndex - 1.

對於消息列表(在Python):

https://www.twilio.com/docs/api/ip-messaging/rest/messages#list-all-messages

# Download the Python helper library from twilio.com/docs/python/install 
from twilio.rest.ip_messaging import TwilioIpMessagingClient 

# Your Account Sid and Auth Token from twilio.com/user/account 
account = "ACCOUNT_SID" 
token = "AUTH_TOKEN" 
client = TwilioIpMessagingClient(account, token) 

service = client.services.get(sid="SERVICE_SID") 
channel = service.channels.get(sid="CHANNEL_ID") 
messages = channel.messages.list() 

參見,Sending a Consumption Report(JavaScript中的示例):

//determine the newest message index 
var newestMessageIndex = activeChannel.messages.length ? 
    activeChannel.messages[activeChannel.messages.length-1].index : 0; 
//check if we we need to set the consumption horizon 
if (activeChannel.lastConsumedMessageIndex !== newestMessageIndex) { 
    activeChannel.updateLastConsumedMessageIndex(newestMessageIndex); 
} 
+0

謝謝,我能夠設置和檢索成員的lastConsumedMessageIndex並執行減法以獲得未讀計數。但是,我擔心的是在REST API中獲取沒有通過所有消息進行分頁的消息總數。如果你的例子中的下一行是使用'len(messages)',它是否也返回50(從REST API返回的結果的數量?) – mattwarren

+1

啊,是的,它會返回當前頁面的結果。該API目前不提供一種方式來獲取未分頁的頻道中的郵件總數。 –

相關問題