typing events

This commit is contained in:
rexy712 2019-03-30 10:09:46 -07:00
parent 5e1408be50
commit 8808608ffd
4 changed files with 30 additions and 5 deletions

View File

@ -9,7 +9,6 @@ matrix:
handle user admin levels (/_matrix/client/r0/rooms/{roomid}/state/m.room.power_levels)
presence
get other user presence
send typing events
handle user join events
handle user leave events
send custom html messages

View File

@ -263,6 +263,8 @@ namespace matrix{
raii::rjp_string send_audio(const raii::string_base& room, const audio_info& audio)const;
raii::rjp_string send_file(const raii::string_base& room, const file_info& file)const;
void send_typing(const raii::string_base& room, bool active, int timeout = 5000)const;
template<class Func>
void set_message_callback(Func&& f){
m_message_callback = std::forward<Func>(f);

View File

@ -405,6 +405,13 @@ namespace matrix{
raii::rjp_string bot::send_message(const raii::string_base& room, const raii::string_base& text)const{
return _send_message(room, detail::_message_body(text));
}
void bot::send_typing(const raii::string_base& room, bool active, int timeout)const{
if(active)
_put_curl(raii::string("{\"timeout\":" + raii::itostr(timeout) + ",\"typing\":true}"), m_urls.typing(m_homeserver, m_access_token, m_curl.encode(room), m_curl.encode(m_userid)), raii::curl_llist());
else
_put_curl("{\"typing\":false}"_ss, m_urls.typing(m_homeserver, m_access_token, m_curl.encode(room), m_curl.encode(m_userid)), raii::curl_llist());
}
void bot::logout(void){
_get_curl(m_urls.logout(m_homeserver, m_access_token));
m_urls.invalidate_accesstoken();
@ -412,6 +419,8 @@ namespace matrix{
raii::string bot::sync(size_t timeout){
raii::string reply = _get_curl(m_urls.sync(m_homeserver, m_access_token, m_next_batch, raii::itostr(timeout)));
if(!reply)
return {};
raii::rjp_ptr root(rjp_parse(reply));
if(!root)
return reply;
@ -480,8 +489,8 @@ namespace matrix{
if(!age.value) continue;
raii::rjp_string room_str = rjp_member_name(roomid);
raii::rjp_string eventid_str = results[2].value;
m_message_callback(*this, msg_info{room_str, sender_str, msg::from_str(rjp_value_string(msg.value)), raii::rjp_string(body.value), eventid_str, rjp_value_integer(age.value)});
_send_read_receipt(room_str, eventid_str);
m_message_callback(*this, msg_info{room_str, sender_str, msg::from_str(rjp_value_string(msg.value)), raii::rjp_string(body.value), eventid_str, rjp_value_integer(age.value)});
}
}
}

View File

@ -283,6 +283,7 @@ int do_reddit_post(reddit::bot& redditbot, const matrix::bot& matbot, const raii
raii::curler curl;
if(reply.type() == reddit::post_type::image)
{
matbot.send_typing(roomid, true, 10000);
DEBUG_PRINT("Got an image\n");
file_output_curl(curl, "testout"_ss, reply.mediaurl());
auto img_data = matbot.upload_image("testout"_ss, reply.name());
@ -290,9 +291,12 @@ int do_reddit_post(reddit::bot& redditbot, const matrix::bot& matbot, const raii
DEBUG_PRINT("image event: %s\n", val.get());
val = matbot.send_message(roomid, raii::string(reply.title() + "\n" + reply.posturl()));
DEBUG_PRINT("text event: %s\n", val.get());
remove("testout");
matbot.send_typing(roomid, false);
}
else if(reply.type() == reddit::post_type::video)
{
matbot.send_typing(roomid, true, 10000);
DEBUG_PRINT("Got a video\n");
if(reply.hosted_video_audio()){
@ -306,8 +310,11 @@ int do_reddit_post(reddit::bot& redditbot, const matrix::bot& matbot, const raii
bool b = mux_audio_video("audio"_ss, "video"_ss, "testout"_ss);
remove("audio");
remove("video");
if(!b)
if(!b){
matbot.send_message(roomid, "[ERROR] Unable to mux reddit hosted video/audio!"_ss);
matbot.send_typing(roomid, false);
return 5;
}
}
}else{
file_output_curl(curl, "testout"_ss, reply.mediaurl());
@ -319,14 +326,20 @@ int do_reddit_post(reddit::bot& redditbot, const matrix::bot& matbot, const raii
DEBUG_PRINT("video event: %s\n", val.get());
val = matbot.send_message(roomid, raii::string(reply.title() + "\n" + reply.posturl()));
DEBUG_PRINT("text event: %s\n", val.get());
remove("testout");
matbot.send_typing(roomid, false);
}
else if(reply.type() == reddit::post_type::audio){
matbot.send_typing(roomid, true, 10000);
DEBUG_PRINT("Got an audio file\n");
auto audio_data = matbot.upload_audio(reply.name());
file_output_curl(curl, "testout"_ss, reply.mediaurl());
auto audio_data = matbot.upload_audio("testout"_ss, reply.name());
auto val = matbot.send_audio(roomid, audio_data);
DEBUG_PRINT("audio event: %s\n", val.get());
val = matbot.send_message(roomid, raii::string(reply.title() + "\n" + reply.posturl()));
DEBUG_PRINT("text event: %s\n", val.get());
remove("testout");
matbot.send_typing(roomid, false);
}
else if(reply.type() == reddit::post_type::link)
{
@ -416,6 +429,7 @@ int main(){
"!post <period>: get a top post from a subreddit (default ProgrammerHumor). Specify hour,day,week,month,year,all.\n"
"!subreddit <sub>: set the current subreddit.\n"
"!lssub: get current subreddit.\n"
"!nipple: 'mxc://matrix.org/SYkDDTUwcfscliYTuIfYFIrx'\n"
"!license: print out a summary of the GNU Affero GPL\n"
"!fulllicense: print out the entire GNU Affero GPL\n"
"!source: link to the source code\n"
@ -440,7 +454,8 @@ int main(){
while(!should_quit){
sync_reply = matbot.sync(30000);
DEBUG_PRINT("%s\n", sync_reply.get());
DEBUG_PRINT("syncing\n");
//DEBUG_PRINT("%s\n", sync_reply.get());
}
}