send announcement

This commit is contained in:
luo123 2020-03-14 00:07:52 +08:00
parent aa61e9441a
commit e57215a603
5 changed files with 74 additions and 22 deletions

View File

@ -579,9 +579,9 @@ internal class GroupImpl(
append("qid", id) append("qid", id)
append("bkn", getBkn()) append("bkn", getBkn())
append("ft", 23) //好像是一个用来识别应用的参数 append("ft", 23) //好像是一个用来识别应用的参数
append("s", if (page ==1) 0 else -(page*amount+1)) // 第一页这里的参数应该是-1 append("s", if (page == 1) 0 else -(page * amount + 1)) // 第一页这里的参数应该是-1
append("n", amount) append("n", amount)
append("ni",if (page ==1) 1 else 0) append("ni", if (page == 1) 1 else 0)
append("format", "json") append("format", "json")
}) })
headers { headers {
@ -594,12 +594,43 @@ internal class GroupImpl(
} }
val rep = data.await() val rep = data.await()
bot.network.logger.error(rep) // bot.network.logger.error(rep)
return json.parse(GroupAnnouncementList.serializer(), rep)
return json.parse(GroupAnnouncementList.serializer(), rep)
} }
@MiraiExperimentalAPI
override suspend fun sendAnnouncement(announcement: GroupAnnouncement) {
val json = Json(JsonConfiguration.Stable)
bot.network.launch {
HttpClient().post<String> {
url("https://web.qun.qq.com/cgi-bin/announce/add_qun_notice")
body = MultiPartFormDataContent(formData {
append("qid", id)
append("bkn", getBkn())
append("text", announcement.msg.text)
append("pinned", announcement.pinned)
append("settings", json.stringify(GroupAnnouncementSettings.serializer(), announcement.settings?:GroupAnnouncementSettings()))
append("format", "json")
})
headers {
append(
"cookie",
"uin=o${bot.selfQQ.id};" +
" skey=${bot.client.wLoginSigInfo.sKey.data.encodeToString()};" +
" p_uin=o${bot.selfQQ.id};" +
" p_skey=${bot.client.wLoginSigInfo.psKeyMap["qun.qq.com"]?.data?.encodeToString()}; "
)
}
}.also {
val jsonObj = json.parseJson(it)
if (jsonObj.jsonObject["ec"]?.int ?:1 != 0){
throw IllegalStateException("Send Announcement fail group:$id msg:${jsonObj.jsonObject["em"]} content:${announcement.msg.text}")
}
}
}
}
@OptIn(MiraiExperimentalAPI::class) @OptIn(MiraiExperimentalAPI::class)
override fun Member(memberInfo: MemberInfo): Member { override fun Member(memberInfo: MemberInfo): Member {
return MemberImpl( return MemberImpl(
@ -761,10 +792,10 @@ internal class GroupImpl(
return this.id == other.id && this.bot == other.bot return this.id == other.id && this.bot == other.bot
} }
/* /**
* 获取 获取群公告 所需的bkn参数 * 获取 获取群公告 所需的bkn参数
* */ * */
fun getBkn(): Int { private fun getBkn(): Int {
val str = bot.client.wLoginSigInfo.sKey.data.encodeToString() val str = bot.client.wLoginSigInfo.sKey.data.encodeToString()
var magic = 5381 var magic = 5381
for (i in str) { for (i in str) {

View File

@ -602,7 +602,7 @@ internal class WtLogin {
userA5 = UserA5(tlvMap119.getOrEmpty(0x10b), creationTime), userA5 = UserA5(tlvMap119.getOrEmpty(0x10b), creationTime),
userA8 = UserA8(tlvMap119.getOrEmpty(0x102), creationTime, expireTime) userA8 = UserA8(tlvMap119.getOrEmpty(0x102), creationTime, expireTime)
) )
//bot.network.logger.error(client.wLoginSigInfo.sKey.data.encodeToString()) //bot.network.logger.error(client.wLoginSigInfo.psKeyMap["qun.qq.com"]?.data?.encodeToString())
} }
} }

View File

@ -16,6 +16,7 @@ import kotlinx.serialization.SerialInfo
import kotlinx.serialization.SerialName import kotlinx.serialization.SerialName
import kotlinx.serialization.Serializable import kotlinx.serialization.Serializable
import net.mamoe.mirai.Bot import net.mamoe.mirai.Bot
import net.mamoe.mirai.data.GroupAnnouncement
import net.mamoe.mirai.data.GroupAnnouncementList import net.mamoe.mirai.data.GroupAnnouncementList
import net.mamoe.mirai.data.MemberInfo import net.mamoe.mirai.data.MemberInfo
import net.mamoe.mirai.event.events.* import net.mamoe.mirai.event.events.*
@ -159,6 +160,13 @@ expect abstract class Group() : Contact, CoroutineScope {
@MiraiExperimentalAPI @MiraiExperimentalAPI
abstract suspend fun getAnnouncements(page: Int = 1, amount: Int = 10):GroupAnnouncementList? abstract suspend fun getAnnouncements(page: Int = 1, amount: Int = 10):GroupAnnouncementList?
/**
* 发送群公告
*
* */
@MiraiExperimentalAPI
abstract suspend fun sendAnnouncement(announcement: GroupAnnouncement)
/** /**
* 让机器人退出这个群. 机器人必须为非群主才能退出. 否则将会失败 * 让机器人退出这个群. 机器人必须为非群主才能退出. 否则将会失败
*/ */

View File

@ -7,34 +7,39 @@ import kotlinx.serialization.Serializable
* 群公告数据类 * 群公告数据类
* getGroupAnnouncementList时如果page=1那么你可以在inst里拿到一些置顶公告 * getGroupAnnouncementList时如果page=1那么你可以在inst里拿到一些置顶公告
* *
* 发公告时只需要填写text
* *
*/ */
@Serializable @Serializable
data class GroupAnnouncementList( data class GroupAnnouncementList(
val ec: Int, //状态码 0 是正常的 val ec: Int, //状态码 0 是正常的
@SerialName("em") val msg:String, //信息 @SerialName("em") val msg: String, //信息
val feeds: List<GroupAnnouncement>?, //群公告列表 val feeds: List<GroupAnnouncement>? = null, //群公告列表
val inst: List<GroupAnnouncement>? //置顶列表? val inst: List<GroupAnnouncement>? = null //置顶列表?
) )
@Serializable @Serializable
data class GroupAnnouncement( data class GroupAnnouncement(
@SerialName("u") val sender: Long, @SerialName("u") val sender: Long = 0,
val msg: GroupAnnouncementMsg, val msg: GroupAnnouncementMsg,
val settings: GroupAnnouncementSettings val settings: GroupAnnouncementSettings? = null,
@SerialName("pubt") val time: Long = 0,
@SerialName("read_num") val readNum: Int = 0,
@SerialName("is_read") val isRead: Int = 0,
val pinned: Int = 0
) )
@Serializable @Serializable
data class GroupAnnouncementMsg( data class GroupAnnouncementMsg(
val text: String, val text: String,
val text_face: String, val text_face: String? = null,
val title: String val title: String? = null
) )
@Serializable @Serializable
data class GroupAnnouncementSettings( data class GroupAnnouncementSettings(
@SerialName("is_show_edit_card") val isShowEditCard: Int, @SerialName("is_show_edit_card") val isShowEditCard: Int = 0,
@SerialName("remind_ts") val remindTs: Int, @SerialName("remind_ts") val remindTs: Int = 0,
@SerialName("tip_window_type") val tipWindowType: Int, @SerialName("tip_window_type") val tipWindowType: Int = 0,
@SerialName("confirm_required") val confirmRequired: Int @SerialName("confirm_required") val confirmRequired: Int = 0
) )

View File

@ -11,6 +11,7 @@ package net.mamoe.mirai.contact
import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.CoroutineScope
import net.mamoe.mirai.Bot import net.mamoe.mirai.Bot
import net.mamoe.mirai.data.GroupAnnouncement
import net.mamoe.mirai.data.GroupAnnouncementList import net.mamoe.mirai.data.GroupAnnouncementList
import net.mamoe.mirai.data.MemberInfo import net.mamoe.mirai.data.MemberInfo
import net.mamoe.mirai.event.events.* import net.mamoe.mirai.event.events.*
@ -153,7 +154,14 @@ actual abstract class Group : Contact(), CoroutineScope {
* *
* */ * */
@MiraiExperimentalAPI @MiraiExperimentalAPI
actual suspend abstract fun getAnnouncements(page: Int, amount: Int ):GroupAnnouncementList? actual suspend abstract fun getAnnouncements(page: Int, amount: Int): GroupAnnouncementList?
/**
* 发送群公告
* */
@MiraiExperimentalAPI
actual suspend abstract fun sendAnnouncement(announcement: GroupAnnouncement)
/** /**
* 检查此 id 的群成员是否存在 * 检查此 id 的群成员是否存在