-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
👾 Ability to add labels when forwarding a message/post (#13)
- Loading branch information
Showing
5 changed files
with
114 additions
and
47 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,6 +8,7 @@ import { createConversation } from 'https://deno.land/x/grammy_conversations@v1. | |
import { conversations } from 'https://deno.land/x/[email protected]/mod.ts' | ||
import { | ||
askApiKey, | ||
forwardPostLabels, | ||
saveBunchUrls, | ||
setDefaultLabel, | ||
updateToken, | ||
|
@@ -37,6 +38,7 @@ bot.use(createConversation(askApiKey)) | |
bot.use(createConversation(saveBunchUrls)) | ||
bot.use(createConversation(updateToken)) | ||
bot.use(createConversation(setDefaultLabel)) | ||
bot.use(createConversation(forwardPostLabels)) | ||
|
||
// Menu | ||
bot.use(cancelMenu) | ||
|
@@ -111,6 +113,10 @@ You can get new by following this guide [Getting an API token](https://docs.omni | |
await ctx.conversation.enter('updateToken') | ||
}) | ||
|
||
bot.on('message:text', async ctx => { | ||
await ctx.conversation.enter('forwardPostLabels') | ||
}) | ||
|
||
bot.start() | ||
|
||
bot.catch(err => { | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import { getSourceLabel } from './getSourceLabel.ts'; | ||
import { getDefaultLabel } from './getDefaultLabel.ts'; | ||
import { Label } from '../types.ts'; | ||
import { type Filter } from 'https://deno.land/x/[email protected]/mod.ts'; | ||
import { MyContext } from '../sessionsHandler.ts'; | ||
|
||
export function getLabels(ctx: Filter<MyContext, 'message:entities:url'>): Label[] { | ||
const source = ctx.msg?.forward_origin; | ||
|
||
// retrieving information from session | ||
const sessionIncludeSource = ctx.session.includeSource; | ||
const sessionDefaultLabel = ctx.session.defaultLabel; | ||
|
||
const labels = []; | ||
|
||
// add default label | ||
const defaultLabel = getDefaultLabel(sessionDefaultLabel); | ||
if (defaultLabel.name) { | ||
labels.push(defaultLabel); | ||
} | ||
|
||
// add source label if includeSource is true | ||
if (source && sessionIncludeSource) { | ||
const sourceLabel = getSourceLabel(source); | ||
|
||
if (sourceLabel) { | ||
labels.push(sourceLabel); | ||
} | ||
} | ||
|
||
return labels; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,55 +1,17 @@ | ||
import { getSourceLabel } from "./getSourceLabel.ts"; | ||
import { getDefaultLabel } from "./getDefaultLabel.ts"; | ||
import { parseUrls } from "./parseUrls.ts"; | ||
import { startsWithUrl } from "./startsWithUrl.ts"; | ||
import { Label } from '../types.ts' | ||
import { type Filter } from "https://deno.land/x/[email protected]/mod.ts" | ||
import { type Filter } from 'https://deno.land/x/[email protected]/mod.ts' | ||
import { MyContext } from '../sessionsHandler.ts' | ||
import { getLabels } from "./getLabels.ts"; | ||
import { retrieveUrl } from "./retrieveUrl.ts"; | ||
|
||
export function getUrlAndLabels(ctx: Filter<MyContext, 'message:entities:url'>) { | ||
let url, labels: Label[] | ||
|
||
// retrieving information from ctx | ||
const message = ctx.message.text | ||
const source = ctx.msg?.forward_origin | ||
|
||
// retrieving information from session | ||
const sessionIncludeSource = ctx.session.includeSource | ||
const sessionDefaultLabel = ctx.session.defaultLabel | ||
|
||
// parse url from the message | ||
if (startsWithUrl(message)) { | ||
;({ url, labels } = parseUrls(message)[0]) | ||
} else if (ctx.entities('text_link').length > 0) { | ||
// handle case when user sends a message with text formatted link | ||
const linkEntity = ctx.entities('text_link')[0] | ||
if (linkEntity && linkEntity.url) { | ||
url = linkEntity.url | ||
} | ||
labels = [] | ||
} else { | ||
// retrieve the first url from the message/post | ||
const urlEntity = ctx.entities('url')[0] | ||
if (urlEntity && urlEntity.text) { | ||
url = urlEntity.text | ||
} | ||
labels = [] | ||
} | ||
const { url, labels: customLabels } = retrieveUrl(ctx) | ||
|
||
// add default label | ||
const defaultLabel = getDefaultLabel(sessionDefaultLabel); | ||
if (defaultLabel.name) { | ||
labels.push(defaultLabel); | ||
} | ||
// add default label and source label | ||
const defaultAndSourceLabels = getLabels(ctx) | ||
|
||
// add source label if includeSource is true | ||
if (source && sessionIncludeSource) { | ||
const sourceLabel = getSourceLabel(source); | ||
const labels = [...customLabels, ...defaultAndSourceLabels] | ||
|
||
if (sourceLabel) { | ||
labels.push(sourceLabel); | ||
} | ||
} | ||
|
||
return { url, labels }; | ||
return { url, labels } | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import { parseUrls } from './parseUrls.ts'; | ||
import { startsWithUrl } from './startsWithUrl.ts'; | ||
import { Label } from '../types.ts'; | ||
import { type Filter } from 'https://deno.land/x/[email protected]/mod.ts'; | ||
import { MyContext } from '../sessionsHandler.ts'; | ||
|
||
export function retrieveUrl(ctx: Filter<MyContext, 'message:entities:url'>) { | ||
const message = ctx.message.text; | ||
|
||
let url: string = ''; | ||
let labels: Label[] = []; | ||
|
||
if (startsWithUrl(message)) { | ||
; ({ url, labels } = parseUrls(message)[0]); | ||
} else if (ctx.entities('text_link').length > 0) { | ||
// handle case when user sends a message with text formatted link | ||
const linkEntity = ctx.entities('text_link')[0]; | ||
if (linkEntity && linkEntity.url) { | ||
url = linkEntity.url; | ||
} | ||
} else { | ||
// retrieve the first url from the message/post | ||
const urlEntity = ctx.entities('url')[0]; | ||
if (urlEntity && urlEntity.text) { | ||
url = urlEntity.text; | ||
} | ||
} | ||
|
||
return { url, labels }; | ||
} |