Skip to content

Instantly share code, notes, and snippets.

@Taifunov
Created January 2, 2020 20:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Taifunov/0ae73a7a3eaedcaf2d0bc2fd1874e67e to your computer and use it in GitHub Desktop.
Save Taifunov/0ae73a7a3eaedcaf2d0bc2fd1874e67e to your computer and use it in GitHub Desktop.
internal async Task PostPhotoAsync(Photo instance)
{
try
{
var getById = _parser.GetById();
var descrs = _parser.GetDescription();
for (var i = 0; i < getById.Count && i < descrs.Count; i++)
{
string photo = getById[i];
var descr = descrs[i];
using (var source = new CancellationTokenSource(7000))
using (var resp = await _client.GetStreamAsync(photo))
{
var msg = await _bot.SendPhotoAsync(
chatId: ChannelId,
photo: new InputOnlineFile(resp, "lol-community.jpg"),
replyMarkup: new InlineKeyboardMarkup(new[] {
new[] {
new InlineKeyboardButton {Text = "๐Ÿ‘ 0", CallbackData = "post_id like"},
new InlineKeyboardButton {Text = "๐Ÿ‘Ž 0", CallbackData = "post_id dislike"},
},
}),
caption: descr,
cancellationToken: new CancellationTokenSource().Token);
var postData = new PostData() {
Post_url = photo,
PostId = instance.PostId,
MessageId = msg.MessageId,
Likes = 0,
DisLikes = 0
};
_context.Posts.Add(postData);
_context.SaveChanges();
}
}
}
catch (Exception e)
{
_logger.LogError(e.ToString());
}
}
internal async Task PostVideoAsync(Video instance)
{
try
{
var descrs = _parser.GetVideoDescriptionList();
var urls = _parser.GetVideoUrl();
for (int i = 0; i < urls.Count; i++)
{
var descr = descrs[i];
var video = urls[i];
var uri = new Uri(video);
if (uri.Host == "www.youtube.com" || uri.Host == "www.youtu.be")
{
var newUri =
$"https://youtube.com/watch?v={uri.LocalPath.Split('/')[2]}";
var msg = await _bot.SendTextMessageAsync(
chatId: ChannelId,
parseMode: ParseMode.Html,
text: "๐ŸŽฌ " + $"<b>{descr}*</b>" + Environment.NewLine +
newUri, // made escape
replyMarkup: new InlineKeyboardMarkup(new[] {
new[] {
new InlineKeyboardButton {
Text = "๐Ÿ‘", CallbackData = "post_id like"
},
new InlineKeyboardButton {
Text = "๐Ÿ‘Ž", CallbackData = "post_id dislike"
},
},
}),
cancellationToken: new CancellationTokenSource().Token);
var videoData = new PostData()
{
Post_url = video,
PostId = instance.Id,
MessageId = msg.MessageId,
Likes = 0,
DisLikes = 0
};
using (_context)
{
_context.Posts.Add(videoData);
_context.SaveChanges();
}
}
}
} catch (Exception e)
{
_logger.LogError(e.ToString());
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment