Skip to content

Commit 4cb57eb

Browse files
committed
chore: Add ads_session sample
1 parent 0c7a581 commit 4cb57eb

File tree

1 file changed

+80
-0
lines changed

1 file changed

+80
-0
lines changed

samples/ads_session.py

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
import logging
2+
3+
logging.basicConfig(level=logging.INFO)
4+
5+
import requests
6+
from io import BytesIO
7+
8+
from swibots import (
9+
BotContext,
10+
Client,
11+
CommandEvent,
12+
CallbackQueryEvent,
13+
BotCommand,
14+
InlineMarkup,
15+
InlineKeyboardButton,
16+
regexp,
17+
)
18+
from datetime import datetime
19+
20+
BOT_TOKEN = "TOKEN HERE"
21+
AD_TYPE = "VIDEO_1" # or "VIDEO_2"
22+
AD_ID = "e45206-------8516f557af"
23+
VERIFY_HRS = 12 # verify after 12 hours
24+
25+
app = Client(BOT_TOKEN, is_app=True, home_callback="page")
26+
app.set_bot_commands(
27+
[
28+
BotCommand("start", "Starts the bot", True),
29+
BotCommand("unlock", "Unlock the secret", True),
30+
]
31+
)
32+
33+
approvedUsers = {}
34+
35+
36+
@app.on_command("start")
37+
async def onStart(ctx: BotContext[CommandEvent]):
38+
await ctx.event.message.reply_text(
39+
"Hello, You can unlock the suprise by sending `/unlock` command!"
40+
)
41+
42+
43+
@app.on_command("unlock")
44+
async def unlockCommand(ctx: BotContext[CommandEvent]):
45+
m = ctx.event.message
46+
userId = m.user_id
47+
ttime = approvedUsers.get(userId)
48+
if ttime and (
49+
((datetime.now() - datetime.fromtimestamp(ttime)).total_seconds() / 3600)
50+
< VERIFY_HRS
51+
):
52+
await m.send("You are already approved!")
53+
return
54+
await m.send(
55+
"Click below button to watch ad and unlock your reward!",
56+
inline_markup=InlineMarkup(
57+
[[InlineKeyboardButton("Watch 🎁", callback_data="showAD")]]
58+
),
59+
)
60+
61+
62+
@app.on_callback_query(regexp("showAD"))
63+
async def getRewards(ctx: BotContext[CallbackQueryEvent]):
64+
"""Show ad and unlock rewards"""
65+
await ctx.event.show_ad(id=AD_ID, ad_type=AD_TYPE, success_callback="watched")
66+
67+
68+
@app.on_callback_query(regexp("watched"))
69+
async def onSuccess(ctx: BotContext[CallbackQueryEvent]):
70+
"""User watched the ad successfully"""
71+
userId = ctx.event.action_by_id
72+
approvedUsers[userId] = datetime.now().timestamp()
73+
m = ctx.event.message
74+
file = BytesIO(requests.get("https://upload.wikimedia.org/wikipedia/commons/8/8a/1000_USD_note%3B_series_of_1934%3B_obverse.jpg").content)
75+
file.name = "reward.png"
76+
await m.send("Thank you for watching the ad!\nHere is your reward!")
77+
await m.send("", document=file)
78+
79+
80+
app.run()

0 commit comments

Comments
 (0)