Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

修复 富文本 消息 content 字段错误。 #5

Closed
wants to merge 1 commit into from

Conversation

fengb3
Copy link
Contributor

@fengb3 fengb3 commented Jul 25, 2024

使用 扩展方法 .SetContent() 构造富文本消息, 发送请求 后 会返回参数错误
解决需要将 PostContent 的序列化从

{
  "post": {
    "zh_cn": {...},
    "en_us": {...}
  }
}

变为

{
  "zh_cn": {...},
  "en_us": {...}
}

由于 不能同时继承自两个类型(即MessageContent, 和 I18nLanguage<T>)
且要复用 I18nLanguage<T> 这个类型, 需要使用JsonConverterPostContent 这个类的序列化自定义

修复 富文本 消息  content 字段错误。
@fengb3
Copy link
Contributor Author

fengb3 commented Jul 26, 2024

这或许不是最好的解决办法

@vicenteyu
Copy link
Owner

vicenteyu commented Jul 26, 2024

飞书这接口也是一言难尽。。。
image

@fengb3
Copy link
Contributor Author

fengb3 commented Jul 26, 2024

var dto = new PostImV1MessagesBodyDto { ReceiveId = userId }
	.SetContent(new PostContent()
	{
		Post = new I18nLanguage<PostContent.PostLanguage>()
		{
			ZhCn = new PostContent.PostLanguage()
				.AddContent([new TextElement(Text: "测试消息"), new AtElement(UserId: "ou_111222333")])
				.AddContent([new LinkElement("链接地址", "https://123")])
		}
	});
		
await _tenantApi.PostImV1MessagesAsync(userIdType, dto);

传参代码和原本的一样, 所以readme.md 不用改

@fengb3
Copy link
Contributor Author

fengb3 commented Jul 26, 2024

飞书这接口也是一言难尽。。。 image

这个文档里https://open.feishu.cn/document/server-docs/im-v1/message-content-description/create_json#45e0953e 是这样的
屏幕截图 2024-07-26 103239

@vicenteyu
Copy link
Owner

由于受到批量消息示例的影响,误以为其他 JSON结构也是一致的。修复的方法需要在 发送消息/回复消息/编辑消息 请求体的 SetContent 增加一个 switch:

Dto.Content = CardOrContent switch
            {
                Im.Dtos.PostContent post => System.Text.Json.JsonSerializer.Serialize(post.Post, options),
                _ => System.Text.Json.JsonSerializer.Serialize(CardOrContent, CardOrContent.GetType(), options)
            };

@fengb3
Copy link
Contributor Author

fengb3 commented Jul 26, 2024

没想到批量和单独发的json结构是不一样的。。

那是得用switch分类处理比较哈。。

@fengb3 fengb3 closed this Jul 26, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants